forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
104 lines (88 loc) · 2.19 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
package action
import (
"fmt"
"os"
"path/filepath"
"golang.org/x/crypto/ssh/terminal"
"github.com/blang/semver"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/gpg"
gpgcli "github.com/justwatchcom/gopass/gpg/cli"
"github.com/justwatchcom/gopass/store/root"
)
type gpger interface {
FindPublicKeys(...string) (gpg.KeyList, error)
FindPrivateKeys(...string) (gpg.KeyList, error)
ListPublicKeys() (gpg.KeyList, error)
ListPrivateKeys() (gpg.KeyList, error)
}
// Action knows everything to run gopass CLI actions
type Action struct {
Name string
Store *root.Store
gpg gpger
isTerm bool
version semver.Version
}
// New returns a new Action wrapper
func New(sv semver.Version) *Action {
name := "gopass"
if len(os.Args) > 0 {
name = filepath.Base(os.Args[0])
}
// try to read config (if it exists)
cfg := config.Load()
// only update version field in config, if it's older than this build
csv, err := semver.Parse(cfg.Version)
if err != nil || csv.LT(sv) {
cfg.Version = sv.String()
if err := cfg.Save(); err != nil {
fmt.Println(color.RedString("Failed to save config: %s", err))
}
}
act := &Action{
Name: name,
version: sv,
isTerm: true,
}
cfg.ImportFunc = act.askForKeyImport
cfg.FsckFunc = act.askForConfirmation
// debug flag
if gdb := os.Getenv("GOPASS_DEBUG"); gdb == "true" {
cfg.Debug = true
}
// need this override for our integration tests
if nc := os.Getenv("GOPASS_NOCOLOR"); nc == "true" {
cfg.NoColor = true
color.NoColor = true
}
// only emit color codes when stdout is a terminal
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
cfg.NoColor = true
color.NoColor = true
cfg.ImportFunc = nil
cfg.FsckFunc = nil
act.isTerm = false
}
// reading from stdin?
if info, err := os.Stdin.Stat(); err == nil && info.Mode()&os.ModeCharDevice == 0 {
cfg.ImportFunc = nil
cfg.FsckFunc = nil
act.isTerm = false
}
store, err := root.New(cfg)
if err != nil {
panic(err)
}
act.Store = store
act.gpg = gpgcli.New(gpgcli.Config{
Debug: cfg.Debug,
AlwaysTrust: true,
})
return act
}
// String implement fmt.Stringer
func (s *Action) String() string {
return s.Store.String()
}