diff --git a/config.go b/config.go index 191eb93..c942409 100644 --- a/config.go +++ b/config.go @@ -26,7 +26,7 @@ type Config struct { // FilePasswordFunc is a required function used to prompt the user for a password FilePasswordFunc PromptFunc - // FileDir is the directory that keyring files are stored in, ~ is resolved to home dir + // FileDir is the directory that keyring files are stored in, ~/ is resolved to the users' home dir FileDir string // KeyCtlScope is the scope of the kernel keyring (either "user", "session", "process" or "thread") @@ -44,7 +44,7 @@ type Config struct { // LibSecretCollectionName is the name collection in secret-service LibSecretCollectionName string - // PassDir is the pass password-store directory + // PassDir is the pass password-store directory, ~/ is resolved to the users' home dir PassDir string // PassCmd is the name of the pass executable diff --git a/file.go b/file.go index fd2f6e5..a325a15 100644 --- a/file.go +++ b/file.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "os" "path/filepath" - "strings" "time" jose "github.com/dvsekhvalnov/jose2go" @@ -38,16 +37,9 @@ func (k *fileKeyring) resolveDir() (string, error) { return "", fmt.Errorf("No directory provided for file keyring") } - dir := k.dir - - // expand tilde for home directory - if strings.HasPrefix(dir, "~") { - home, err := os.UserHomeDir() - if err != nil { - return "", err - } - dir = strings.Replace(dir, "~", home, 1) - debugf("Expanded file dir to %s", dir) + dir, err := ExpandTilde(k.dir) + if err != nil { + return "", err } stat, err := os.Stat(dir) diff --git a/pass.go b/pass.go index 8ab252e..823666f 100644 --- a/pass.go +++ b/pass.go @@ -15,33 +15,37 @@ import ( func init() { supportedBackends[PassBackend] = opener(func(cfg Config) (Keyring, error) { + var err error + pass := &passKeyring{ passcmd: cfg.PassCmd, dir: cfg.PassDir, prefix: cfg.PassPrefix, } - if cfg.PassCmd == "" { + + if pass.passcmd == "" { pass.passcmd = "pass" } - if cfg.PassDir == "" { + + if pass.dir == "" { if passDir, found := os.LookupEnv("PASSWORD_STORE_DIR"); found { pass.dir = passDir } else { - pass.dir = filepath.Join(os.Getenv("HOME"), ".password-store") - } - } else if strings.HasPrefix(pass.dir, "~") { - if len(pass.dir) > 1 && pass.dir[1] != '/' { - return nil, fmt.Errorf("Cannot expand path: %s", pass.dir) + homeDir, err := os.UserHomeDir() + if err != nil { + return nil, err + } + pass.dir = filepath.Join(homeDir, ".password-store") } - home, err := os.UserHomeDir() - if err != nil { - return nil, err - } - pass.dir = filepath.Join(home, pass.dir[1:]) + } + + pass.dir, err = ExpandTilde(pass.dir) + if err != nil { + return nil, err } // fail if the pass program is not available - _, err := exec.LookPath(pass.passcmd) + _, err = exec.LookPath(pass.passcmd) if err != nil { return nil, errors.New("The pass program is not available") } diff --git a/tilde.go b/tilde.go new file mode 100644 index 0000000..d3b9d54 --- /dev/null +++ b/tilde.go @@ -0,0 +1,23 @@ +package keyring + +import ( + "os" + "path/filepath" + "strings" +) + +// ~/ or ~\ depending on OS +var tildePrefix = string([]rune{'~', filepath.Separator}) + +// expand tilde for home directory +func ExpandTilde(dir string) (string, error) { + if strings.HasPrefix(dir, tildePrefix) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + dir = strings.Replace(dir, "~", homeDir, 1) + debugf("Expanded file dir to %s", dir) + } + return dir, nil +}