forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
120 lines (107 loc) · 2.85 KB
/
action.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package action
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"golang.org/x/crypto/ssh/terminal"
"github.com/fatih/color"
"github.com/ghodss/yaml"
"github.com/justwatchcom/gopass/fsutil"
"github.com/justwatchcom/gopass/gpg"
"github.com/justwatchcom/gopass/password"
)
// Action knows everything to run gopass CLI actions
type Action struct {
Name string
Store *password.RootStore
}
// New returns a new Action wrapper
func New(v string) *Action {
name := "gopass"
if len(os.Args) > 0 {
name = filepath.Base(os.Args[0])
}
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
gpg.Debug = true
}
pwDir := pwStoreDir("")
// try to read config (if it exists)
for _, l := range configLocations() {
if cfg, err := newFromFile(l); err == nil && cfg != nil {
cfg.ImportFunc = askForKeyImport
cfg.FsckFunc = askForConfirmation
cfg.Version = v
color.NoColor = cfg.NoColor
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
color.NoColor = true
}
// only emit color codes when stdout is a terminal
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
color.NoColor = true
}
return &Action{
Name: name,
Store: cfg,
}
}
}
cfg, err := password.NewRootStore(pwDir)
if err != nil {
panic(err)
}
cfg.ImportFunc = askForKeyImport
cfg.FsckFunc = askForConfirmation
cfg.Version = v
color.NoColor = cfg.NoColor
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
color.NoColor = true
}
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
color.NoColor = true
}
return &Action{
Name: name,
Store: cfg,
}
}
// newFromFile creates a new RootStore instance by unmarsahling a config file.
// If the file doesn't exist or fails to unmarshal an error is returned
func newFromFile(cf string) (*password.RootStore, error) {
// deliberately using os.Stat here, a symlinked
// config is OK
if _, err := os.Stat(cf); err != nil {
return nil, err
}
buf, err := ioutil.ReadFile(cf)
if err != nil {
fmt.Printf("Error reading config from %s: %s\n", cf, err)
return nil, err
}
cfg := &password.RootStore{}
err = yaml.Unmarshal(buf, &cfg)
if err != nil {
fmt.Printf("Error reading config from %s: %s\n", cf, err)
return nil, err
}
return cfg, nil
}
// String implement fmt.Stringer
func (s *Action) String() string {
return s.Store.String()
}
// pwStoreDir reads the password store dir from the environment
// or returns the default location ~/.password-store if the env is
// not set
func pwStoreDir(mount string) string {
if mount != "" {
return fsutil.CleanPath(filepath.Join(os.Getenv("HOME"), ".password-store-"+strings.Replace(mount, string(filepath.Separator), "-", -1)))
}
if d := os.Getenv("PASSWORD_STORE_DIR"); d != "" {
return fsutil.CleanPath(d)
}
return os.Getenv("HOME") + "/.password-store"
}