install_darwin.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2018 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build darwin
  15. package main
  16. import (
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "go.etcd.io/etcd/pkg/fileutil"
  24. )
  25. const downloadURL = `https://storage.googleapis.com/etcd/%s/etcd-%s-darwin-amd64.zip`
  26. func install(ver, dir string) (string, error) {
  27. ep := fmt.Sprintf(downloadURL, ver, ver)
  28. resp, err := http.Get(ep)
  29. if err != nil {
  30. return "", err
  31. }
  32. defer resp.Body.Close()
  33. d, err := ioutil.ReadAll(resp.Body)
  34. if err != nil {
  35. return "", err
  36. }
  37. zipPath := filepath.Join(dir, "etcd.zip")
  38. if err = ioutil.WriteFile(zipPath, d, fileutil.PrivateFileMode); err != nil {
  39. return "", err
  40. }
  41. if err = exec.Command("bash", "-c", fmt.Sprintf("unzip %s -d %s", zipPath, dir)).Run(); err != nil {
  42. return "", err
  43. }
  44. bp1 := filepath.Join(dir, fmt.Sprintf("etcd-%s-darwin-amd64", ver), "etcd")
  45. bp2 := filepath.Join(dir, "etcd")
  46. return bp2, os.Rename(bp1, bp2)
  47. }