From 09e536a128174d93811b06138c6a1f6e60236bdf Mon Sep 17 00:00:00 2001 From: Tycho Andersen Date: Wed, 16 Aug 2017 17:51:40 -0600 Subject: [PATCH 1/2] return "" instead of an error when pass isn't present It turns out the cred helpers protocol is to return "" and not an error when a credential isn't present, so let's do that. Signed-off-by: Tycho Andersen --- pass/pass_linux.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pass/pass_linux.go b/pass/pass_linux.go index 2ddfa4a6..94032365 100644 --- a/pass/pass_linux.go +++ b/pass/pass_linux.go @@ -111,10 +111,7 @@ func (h Pass) Delete(serverURL string) error { return err } -// listPassDir lists all the contents of a directory in the password store. -// Pass uses fancy unicode to emit stuff to stdout, so rather than try -// and parse this, let's just look at the directory structure instead. -func listPassDir(args ...string) ([]os.FileInfo, error) { +func getPassDir() string { passDir := os.ExpandEnv("$HOME/.password-store") for _, e := range os.Environ() { parts := strings.SplitN(e, "=", 2) @@ -130,6 +127,14 @@ func listPassDir(args ...string) ([]os.FileInfo, error) { break } + return passDir +} + +// listPassDir lists all the contents of a directory in the password store. +// Pass uses fancy unicode to emit stuff to stdout, so rather than try +// and parse this, let's just look at the directory structure instead. +func listPassDir(args ...string) ([]os.FileInfo, error) { + passDir := getPassDir() p := path.Join(append([]string{passDir, PASS_FOLDER}, args...)...) contents, err := ioutil.ReadDir(p) if err != nil { @@ -155,6 +160,14 @@ func (h Pass) Get(serverURL string) (string, string, error) { encoded := base64.URLEncoding.EncodeToString([]byte(serverURL)) + if _, err := os.Stat(path.Join(getPassDir(), PASS_FOLDER, encoded)); err != nil { + if os.IsNotExist(err) { + return "", "", nil; + } + + return "", "", err + } + usernames, err := listPassDir(encoded) if err != nil { return "", "", err From f212ea17df81a1d986a84c7293e25c23ba1befe3 Mon Sep 17 00:00:00 2001 From: Tycho Andersen Date: Thu, 17 Aug 2017 10:36:48 -0600 Subject: [PATCH 2/2] fix test Signed-off-by: Tycho Andersen --- pass/pass_linux_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pass/pass_linux_test.go b/pass/pass_linux_test.go index 1e6f0f8b..d342b611 100644 --- a/pass/pass_linux_test.go +++ b/pass/pass_linux_test.go @@ -54,9 +54,13 @@ func TestPassHelper(t *testing.T) { t.Fatal(err) } - _, _, err = helper.Get(server) - if err == nil { - t.Fatalf("%s shuldn't exist any more", server) + username, _, err = helper.Get(server) + if err != nil { + t.Fatal(err) + } + + if username != "" { + t.Fatalf("%s shouldn't exist any more", username) } }