forked from go-jira/jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword.go
78 lines (73 loc) · 1.94 KB
/
password.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package jira
import (
"bytes"
"fmt"
"os/exec"
"strings"
"github.com/howeyc/gopass"
)
func (c *Cli) GetPass(user string) string {
passwd := ""
if source, ok := c.opts["password-source"].(string); ok {
if source == "keyring" {
var err error
passwd, err = keyringGet(user)
if err != nil {
panic(err)
}
} else if source == "pass" {
if bin, err := exec.LookPath("pass"); err == nil {
buf := bytes.NewBufferString("")
cmd := exec.Command(bin, fmt.Sprintf("GoJira/%s", user))
cmd.Stdout = buf
cmd.Stderr = buf
if err := cmd.Run(); err == nil {
passwd = strings.TrimSpace(buf.String())
}
}
} else {
log.Warningf("Unknown password-source: %s", source)
}
}
if passwd != "" {
return passwd
}
fmt.Printf("Jira Password [%s]: ", user)
pw, err := gopass.GetPasswdMasked()
if err != nil {
return ""
}
passwd = string(pw)
return passwd
}
func (c *Cli) SetPass(user, passwd string) error {
log.Debugf("SetPass called: %s => %s", user, passwd)
if source, ok := c.opts["password-source"].(string); ok {
log.Debugf("password-source: %s", source)
if source == "keyring" {
// save password in keychain so that it can be used for subsequent http requests
err := keyringSet(user, passwd)
if err != nil {
log.Errorf("Failed to set password in keyring: %s", err)
return err
}
} else if source == "pass" {
log.Debugf("processing %s", source)
if bin, err := exec.LookPath("pass"); err == nil {
log.Debugf("using %s", bin)
in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd))
out := bytes.NewBufferString("")
cmd := exec.Command(bin, "insert", "--force", fmt.Sprintf("GoJira/%s", user))
cmd.Stdin = in
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return fmt.Errorf("Failed to insert password: %s", out.String())
}
}
} else {
return fmt.Errorf("Unknown password-source: %s", source)
}
}
return nil
}