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 10, 2018
1 parent 93111de commit de8cd46
Show file tree
Hide file tree
Showing 9 changed files with 33 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[0] != '~' || (len(path) > 1 && path[1] != os.PathSeparator) {
return path
}
return filepath.Join(homeDir, path[1:])
}
9 changes: 9 additions & 0 deletions cmd/notary/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
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
21 changes: 0 additions & 21 deletions vendor/github.com/mitchellh/go-homedir/LICENSE

This file was deleted.

14 changes: 0 additions & 14 deletions vendor/github.com/mitchellh/go-homedir/README.md

This file was deleted.

112 changes: 0 additions & 112 deletions vendor/github.com/mitchellh/go-homedir/homedir.go

This file was deleted.

0 comments on commit de8cd46

Please sign in to comment.