From 9c33b8deb4d6053981cd61a5f66900faa2a23a82 Mon Sep 17 00:00:00 2001 From: Peter Salvatore Date: Thu, 23 Oct 2014 12:04:09 -0700 Subject: [PATCH] replace os/user with go-homedir os/user can't be used in cross-platform builds, hence we vendor a cross-platform hack. Signed-off-by: Peter Salvatore --- hack/vendor.sh | 4 +- .../clients/imageClient/imageClient.go | 6 +- .../clients/vmClient/vmClient.go | 7 +- .../azure-sdk-for-go/publishSettings.go | 7 +- .../github.com/mitchellh/go-homedir/LICENSE | 21 +++++ .../github.com/mitchellh/go-homedir/README.md | 14 ++++ .../mitchellh/go-homedir/homedir.go | 83 +++++++++++++++++++ .../mitchellh/go-homedir/homedir_test.go | 77 +++++++++++++++++ 8 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 vendor/src/github.com/mitchellh/go-homedir/LICENSE create mode 100644 vendor/src/github.com/mitchellh/go-homedir/README.md create mode 100644 vendor/src/github.com/mitchellh/go-homedir/homedir.go create mode 100644 vendor/src/github.com/mitchellh/go-homedir/homedir_test.go diff --git a/hack/vendor.sh b/hack/vendor.sh index 9b4ca77bfe126..3a1356b8d0e33 100755 --- a/hack/vendor.sh +++ b/hack/vendor.sh @@ -61,7 +61,9 @@ clone git github.com/docker/libtrust d273ef2565ca clone hg code.google.com/p/goauth2 afe77d958c70 -clone git github.com/MSOpenTech/azure-sdk-for-go 43719165c8b42a47f6b9d13b206271842396bdaf +clone git github.com/mitchellh/go-homedir 7d2d8c8a4e078ce3c58736ab521a40b37a504c52 + +clone git github.com/MSOpenTech/azure-sdk-for-go b0f548080397ec01d2433fc12a395e3f5179c489 # get Go tip's archive/tar, for xattr support and improved performance # TODO after Go 1.4 drops, bump our minimum supported version and drop this vendored dep diff --git a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient/imageClient.go b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient/imageClient.go index 036831e5d3ef4..5c8a0d3f969aa 100644 --- a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient/imageClient.go +++ b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient/imageClient.go @@ -9,7 +9,7 @@ import ( const ( azureImageListURL = "services/images" - invalidLocationError = "Error: Can not find image %s in specified subscription, please specify another image name \n" + invalidImageError = "Error: Can not find image %s in specified subscription, please specify another image name \n" ) func GetImageList() (ImageList, error) { @@ -35,12 +35,12 @@ func ResolveImageName(imageName string) error { } for _, image := range imageList.OSImages { - if image.Name != imageName { + if image.Name != imageName && image.Label != imageName { continue } return nil } - return errors.New(fmt.Sprintf(invalidLocationError, imageName)) + return errors.New(fmt.Sprintf(invalidImageError, imageName)) } diff --git a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/vmClient/vmClient.go b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/vmClient/vmClient.go index a7500aa9e8519..b46601cac765d 100644 --- a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/vmClient/vmClient.go +++ b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/clients/vmClient/vmClient.go @@ -11,12 +11,13 @@ import ( "io" "io/ioutil" "os" - "os/user" "path" "strings" "time" "unicode" + homedir "github.com/mitchellh/go-homedir" + azure "github.com/MSOpenTech/azure-sdk-for-go" "github.com/MSOpenTech/azure-sdk-for-go/clients/imageClient" "github.com/MSOpenTech/azure-sdk-for-go/clients/locationClient" @@ -557,12 +558,12 @@ func createDockerPublicConfig(dockerPort int) string { } func createDockerPrivateConfig(dockerCertDir string) (string, error) { - usr, err := user.Current() + homeDir, err := homedir.Dir() if err != nil { return "", err } - certDir := path.Join(usr.HomeDir, dockerCertDir) + certDir := path.Join(homeDir, dockerCertDir) if _, err := os.Stat(certDir); err == nil { fmt.Println(dockerDirExistsMessage) diff --git a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/publishSettings.go b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/publishSettings.go index ae7c9e9672e00..2d7daa8f088b3 100644 --- a/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/publishSettings.go +++ b/vendor/src/github.com/MSOpenTech/azure-sdk-for-go/publishSettings.go @@ -7,8 +7,9 @@ import ( "fmt" "io/ioutil" "os" - "os/user" "path" + + homedir "github.com/mitchellh/go-homedir" ) var settings publishSettings = publishSettings{} @@ -140,12 +141,12 @@ func getActiveSubscription(publishSettingsContent []byte) (subscription, error) } func getAzureDir() (string, error) { - usr, err := user.Current() + homeDir, err := homedir.Dir() if err != nil { return "", err } - azureDir := path.Join(usr.HomeDir, ".azure") + azureDir := path.Join(homeDir, ".azure") //Create azure dir if does not exists if _, err := os.Stat(azureDir); os.IsNotExist(err) { mkdirErr := os.Mkdir(azureDir, 0644) diff --git a/vendor/src/github.com/mitchellh/go-homedir/LICENSE b/vendor/src/github.com/mitchellh/go-homedir/LICENSE new file mode 100644 index 0000000000000..f9c841a51e0d1 --- /dev/null +++ b/vendor/src/github.com/mitchellh/go-homedir/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2013 Mitchell Hashimoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/src/github.com/mitchellh/go-homedir/README.md b/vendor/src/github.com/mitchellh/go-homedir/README.md new file mode 100644 index 0000000000000..d70706d5b35b6 --- /dev/null +++ b/vendor/src/github.com/mitchellh/go-homedir/README.md @@ -0,0 +1,14 @@ +# go-homedir + +This is a Go library for detecting the user's home directory without +the use of cgo, so the library can be used in cross-compilation environments. + +Usage is incredibly simple, just call `homedir.Dir()` to get the home directory +for a user, and `homedir.Expand()` to expand the `~` in a path to the home +directory. + +**Why not just use `os/user`?** The built-in `os/user` package requires +cgo on Darwin systems. This means that any Go code that uses that package +cannot cross compile. But 99% of the time the use for `os/user` is just to +retrieve the home directory, which we can do for the current user without +cgo. This library does that, enabling cross-compilation. diff --git a/vendor/src/github.com/mitchellh/go-homedir/homedir.go b/vendor/src/github.com/mitchellh/go-homedir/homedir.go new file mode 100644 index 0000000000000..b6d35027b7412 --- /dev/null +++ b/vendor/src/github.com/mitchellh/go-homedir/homedir.go @@ -0,0 +1,83 @@ +package homedir + +import ( + "bytes" + "errors" + "os" + "os/exec" + "runtime" + "strings" +) + +// Dir returns the home directory for the executing user. +// +// This uses an OS-specific method for discovering the home directory. +// An error is returned if a home directory cannot be detected. +func Dir() (string, error) { + if runtime.GOOS == "windows" { + return dirWindows() + } + + // Unix-like system, so just assume Unix + return dirUnix() +} + +// Expand expands the path to include the home directory if the path +// is prefixed with `~`. If it isn't prefixed with `~`, the path is +// returned as-is. +func Expand(path string) (string, error) { + if len(path) == 0 { + return path, nil + } + + if path[0] != '~' { + return path, nil + } + + if len(path) > 1 && path[1] != '/' && path[1] != '\\' { + return "", errors.New("cannot expand user-specific home dir") + } + + dir, err := Dir() + if err != nil { + return "", err + } + + return dir + path[1:], nil +} + +func dirUnix() (string, error) { + // First prefer the HOME environmental variable + if home := os.Getenv("HOME"); home != "" { + return home, nil + } + + // If that fails, try the shell + var stdout bytes.Buffer + cmd := exec.Command("sh", "-c", "eval echo ~$USER") + cmd.Stdout = &stdout + if err := cmd.Run(); err != nil { + return "", err + } + + result := strings.TrimSpace(stdout.String()) + if result == "" { + return "", errors.New("blank output when reading home directory") + } + + return result, nil +} + +func dirWindows() (string, error) { + drive := os.Getenv("HOMEDRIVE") + path := os.Getenv("HOMEPATH") + home := drive + path + if drive == "" || path == "" { + home = os.Getenv("USERPROFILE") + } + if home == "" { + return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank") + } + + return home, nil +} diff --git a/vendor/src/github.com/mitchellh/go-homedir/homedir_test.go b/vendor/src/github.com/mitchellh/go-homedir/homedir_test.go new file mode 100644 index 0000000000000..89e74c3784688 --- /dev/null +++ b/vendor/src/github.com/mitchellh/go-homedir/homedir_test.go @@ -0,0 +1,77 @@ +package homedir + +import ( + "fmt" + "os/user" + "testing" +) + +func TestDir(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + dir, err := Dir() + if err != nil { + t.Fatalf("err: %s", err) + } + + if u.HomeDir != dir { + t.Fatalf("%#v != %#v", u.HomeDir, dir) + } +} + +func TestExpand(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatalf("err: %s", err) + } + + cases := []struct { + Input string + Output string + Err bool + }{ + { + "/foo", + "/foo", + false, + }, + + { + "~/foo", + fmt.Sprintf("%s/foo", u.HomeDir), + false, + }, + + { + "", + "", + false, + }, + + { + "~", + u.HomeDir, + false, + }, + + { + "~foo/foo", + "", + true, + }, + } + + for _, tc := range cases { + actual, err := Expand(tc.Input) + if (err != nil) != tc.Err { + t.Fatalf("Input: %#v\n\nErr: %s", tc.Input, err) + } + + if actual != tc.Output { + t.Fatalf("Input: %#v\n\nOutput: %#v", tc.Input, actual) + } + } +}