Skip to content

Commit

Permalink
Remove go-homedir dependency
Browse files Browse the repository at this point in the history
This is overcomplicated for a very simple requirement. It can call
`exec`, it has a cache protected by a mutex because it is so complex.
Just use the per platform environment variables which will always be
set, if the user has a weird environment they can specify full paths.

Signed-off-by: Justin Cormack <[email protected]>
  • Loading branch information
justincormack committed Apr 7, 2018
1 parent c30d5e3 commit 36cce4f
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 157 deletions.
15 changes: 6 additions & 9 deletions cmd/notary/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions cmd/notary/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

const (
Expand Down Expand Up @@ -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[1] != '~' || (len(path) > 1 && path[1] != os.PathSeparator) {
return path
}
return filepath.Join(homeDir, path[1:])
}
5 changes: 5 additions & 0 deletions cmd/notary/util_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build !windows

package main

const homeEnv = "HOME"
3 changes: 3 additions & 0 deletions cmd/notary/util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package main

const homeEnv = "USERPROFILE"
1 change: 0 additions & 1 deletion vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 36cce4f

Please sign in to comment.