diff --git a/cmd/notary/main.go b/cmd/notary/main.go index 754d68c38..4bd628de2 100644 --- a/cmd/notary/main.go +++ b/cmd/notary/main.go @@ -7,7 +7,6 @@ import ( "path/filepath" "strings" - homedir "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -75,9 +74,9 @@ func (n *notaryCommander) parseConfig() (*viper.Viper, error) { n.setVerbosityLevel() // Get home directory for current user - homeDir, err := homedir.Dir() - if err != nil { - logrus.Warnf("cannot get current user home directory: %v", err) + homeDir := os.Getenv(homeEnv) + if homeDir == "" { + logrus.Warn("cannot get current user home directory: environment variable not set") pwd, _ := os.Getwd() logrus.Warnf("notary will use %s to store configuration and keys", filepath.Join(pwd, configDir)) } @@ -129,11 +128,9 @@ func (n *notaryCommander) parseConfig() (*viper.Viper, error) { } // Expands all the possible ~/ that have been given, either through -d or config - // If there is no error, use it, if not, just attempt to use whatever the user gave us - expandedTrustDir, err := homedir.Expand(config.GetString("trust_dir")) - if err == nil { - config.Set("trust_dir", expandedTrustDir) - } + // Otherwise just attempt to use whatever the user gave us + expandedTrustDir := homeExpand(homeDir, config.GetString("trust_dir")) + config.Set("trust_dir", expandedTrustDir) logrus.Debugf("Using the following trust directory: %s", config.GetString("trust_dir")) return config, nil diff --git a/cmd/notary/util.go b/cmd/notary/util.go index fef9e8dbe..65e5345ff 100644 --- a/cmd/notary/util.go +++ b/cmd/notary/util.go @@ -4,6 +4,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" ) const ( @@ -52,3 +53,12 @@ func feedback(t *tufCommander, payload []byte) error { os.Stdout.Write(payload) return nil } + +// homeExpand will expand an initial ~ to the user home directory. This is supported for +// config files where the shell will not have expanded paths. +func homeExpand(homeDir, path string) string { + if path == "" || path[0] != '~' || (len(path) > 1 && path[1] != os.PathSeparator) { + return path + } + return filepath.Join(homeDir, path[1:]) +} diff --git a/cmd/notary/util_test.go b/cmd/notary/util_test.go index 8949ffd4c..764a04400 100644 --- a/cmd/notary/util_test.go +++ b/cmd/notary/util_test.go @@ -52,3 +52,12 @@ func TestFeedback(t *testing.T) { require.NoError(t, err) require.Equal(t, "", string(content)) } + +func TestHomeExpand(t *testing.T) { + require.Equal(t, homeExpand("home", ""), "") + require.Equal(t, homeExpand("home", "~"), "home") + require.Equal(t, homeExpand("home", "~"+string(os.PathSeparator)), "home") + require.Equal(t, homeExpand("home", filepath.Join("~", "test")), filepath.Join("home", "test")) + require.Equal(t, homeExpand("home", "~cyli"), "~cyli") + require.Equal(t, homeExpand(string(os.PathSeparator)+"home", filepath.Join("~", "test")), string(os.PathSeparator)+filepath.Join("home", "test")) +} diff --git a/cmd/notary/util_unix.go b/cmd/notary/util_unix.go new file mode 100644 index 000000000..415411c66 --- /dev/null +++ b/cmd/notary/util_unix.go @@ -0,0 +1,5 @@ +// +build !windows + +package main + +const homeEnv = "HOME" diff --git a/cmd/notary/util_windows.go b/cmd/notary/util_windows.go new file mode 100644 index 000000000..d6ff51e63 --- /dev/null +++ b/cmd/notary/util_windows.go @@ -0,0 +1,3 @@ +package main + +const homeEnv = "USERPROFILE" diff --git a/vendor.conf b/vendor.conf index 5e1c573e0..94db21070 100644 --- a/vendor.conf +++ b/vendor.conf @@ -16,7 +16,6 @@ github.com/jinzhu/inflection 1c35d901db3da928c72a72d8458480cc9ade058f github.com/lib/pq 0dad96c0b94f8dee039aa40467f767467392a0af github.com/mattn/go-sqlite3 6c771bb9887719704b210e87e934f08be014bdb1 # v1.6.0 github.com/miekg/pkcs11 5f6e0d0dad6f472df908c8e968a98ef00c9224bb -github.com/mitchellh/go-homedir df55a15e5ce646808815381b3db47a8c66ea62f4 github.com/prometheus/client_golang 449ccefff16c8e2b7229f6be1921ba22f62461fe github.com/prometheus/client_model fa8ad6fec33561be4280a8f0514318c79d7f6cb6 # model-0.0.2-12-gfa8ad6f github.com/prometheus/procfs b1afdc266f54247f5dc725544f5d351a8661f502 diff --git a/vendor/github.com/mitchellh/go-homedir/LICENSE b/vendor/github.com/mitchellh/go-homedir/LICENSE deleted file mode 100644 index f9c841a51..000000000 --- a/vendor/github.com/mitchellh/go-homedir/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -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/github.com/mitchellh/go-homedir/README.md b/vendor/github.com/mitchellh/go-homedir/README.md deleted file mode 100644 index d70706d5b..000000000 --- a/vendor/github.com/mitchellh/go-homedir/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# 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/github.com/mitchellh/go-homedir/homedir.go b/vendor/github.com/mitchellh/go-homedir/homedir.go deleted file mode 100644 index ed920dec2..000000000 --- a/vendor/github.com/mitchellh/go-homedir/homedir.go +++ /dev/null @@ -1,112 +0,0 @@ -package homedir - -import ( - "bytes" - "errors" - "os" - "os/exec" - "path/filepath" - "runtime" - "strings" - "sync" -) - -// DisableCache will disable caching of the home directory. Caching is enabled -// by default. -var DisableCache bool - -var homedirCache string -var cacheLock sync.RWMutex - -// 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 !DisableCache { - cacheLock.RLock() - cached := homedirCache - cacheLock.RUnlock() - if cached != "" { - return cached, nil - } - } - - cacheLock.Lock() - defer cacheLock.Unlock() - - var result string - var err error - if runtime.GOOS == "windows" { - result, err = dirWindows() - } else { - // Unix-like system, so just assume Unix - result, err = dirUnix() - } - - if err != nil { - return "", err - } - homedirCache = result - return result, nil -} - -// 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 filepath.Join(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 -}