Skip to content

Commit 26deb29

Browse files
author
Vincent Demeester
authored
Merge pull request #109 from euank/better-exec
pass: simplify some code
2 parents 1c295f7 + a13ff50 commit 26deb29

File tree

1 file changed

+14
-59
lines changed

1 file changed

+14
-59
lines changed

pass/pass_linux.go

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package pass
66

77
import (
8+
"bytes"
89
"encoding/base64"
910
"errors"
1011
"fmt"
@@ -45,54 +46,19 @@ func init() {
4546
}
4647
}
4748

48-
func runPass(stdinContent string, args ...string) (string, error) {
49+
func runPass(stdin string, args ...string) (string, error) {
50+
var stdout, stderr bytes.Buffer
4951
cmd := exec.Command("pass", args...)
52+
cmd.Stdin = strings.NewReader(stdin)
53+
cmd.Stdout = &stdout
54+
cmd.Stderr = &stderr
5055

51-
stdin, err := cmd.StdinPipe()
56+
err := cmd.Run()
5257
if err != nil {
53-
return "", err
58+
return "", fmt.Errorf("%s: %s", err, stderr.String())
5459
}
55-
defer stdin.Close()
5660

57-
stderr, err := cmd.StderrPipe()
58-
if err != nil {
59-
return "", err
60-
}
61-
defer stderr.Close()
62-
63-
stdout, err := cmd.StdoutPipe()
64-
if err != nil {
65-
return "", err
66-
}
67-
defer stdout.Close()
68-
69-
err = cmd.Start()
70-
if err != nil {
71-
return "", err
72-
}
73-
74-
_, err = stdin.Write([]byte(stdinContent))
75-
if err != nil {
76-
return "", err
77-
}
78-
stdin.Close()
79-
80-
errContent, err := ioutil.ReadAll(stderr)
81-
if err != nil {
82-
return "", fmt.Errorf("error reading stderr: %s", err)
83-
}
84-
85-
result, err := ioutil.ReadAll(stdout)
86-
if err != nil {
87-
return "", fmt.Errorf("Error reading stdout: %s", err)
88-
}
89-
90-
cmdErr := cmd.Wait()
91-
if cmdErr != nil {
92-
return "", fmt.Errorf("%s: %s", cmdErr, errContent)
93-
}
94-
95-
return string(result), nil
61+
return stdout.String(), nil
9662
}
9763

9864
// Pass handles secrets using Linux secret-service as a store.
@@ -130,22 +96,11 @@ func (h Pass) Delete(serverURL string) error {
13096
}
13197

13298
func getPassDir() string {
133-
passDir := os.ExpandEnv("$HOME/.password-store")
134-
for _, e := range os.Environ() {
135-
parts := strings.SplitN(e, "=", 2)
136-
if len(parts) < 2 {
137-
continue
138-
}
139-
140-
if parts[0] != "PASSWORD_STORE_DIR" {
141-
continue
142-
}
143-
144-
passDir = parts[1]
145-
break
99+
passDir := "$HOME/.password-store"
100+
if envDir := os.Getenv("PASSWORD_STORE_DIR"); envDir != "" {
101+
passDir = envDir
146102
}
147-
148-
return passDir
103+
return os.ExpandEnv(passDir)
149104
}
150105

151106
// listPassDir lists all the contents of a directory in the password store.
@@ -180,7 +135,7 @@ func (h Pass) Get(serverURL string) (string, string, error) {
180135

181136
if _, err := os.Stat(path.Join(getPassDir(), PASS_FOLDER, encoded)); err != nil {
182137
if os.IsNotExist(err) {
183-
return "", "", nil;
138+
return "", "", nil
184139
}
185140

186141
return "", "", err

0 commit comments

Comments
 (0)