-
Notifications
You must be signed in to change notification settings - Fork 0
/
before.go
55 lines (49 loc) · 1.26 KB
/
before.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
package commands
import (
"fmt"
"os"
"github.com/adrianosela/padl/cli/config"
"github.com/adrianosela/padl/lib/keymgr"
"github.com/adrianosela/padl/lib/padlfile"
cli "gopkg.in/urfave/cli.v1"
)
func checkCanModifyPadlFile(ctx *cli.Context) error {
// read padlfile
pf, err := padlfile.ReadPadlfile(ctx.String(name(pathFlag)))
if err != nil {
return err
}
// check if there was a private key provided
if key := ctx.String(name(privateKeyFlag)); key == "" {
// if not, then look for one in the file system
key, kid, err := getUserKey(config.GetDefaultPath(), pf.Data.MemberKeys)
if err != nil {
return err
}
if ctx.GlobalBool(name(VerboseFlag)) {
fmt.Println(fmt.Sprintf("[info] found private key %s\n", kid))
}
return ctx.Set(name(privateKeyFlag), key)
}
return nil
}
func getUserKey(keyMgrPath string, keyIDs []string) (string, string, error) {
// init new key manager at the config path
mgr, err := keymgr.NewFSManager(keyMgrPath)
if err != nil {
return "", "", err
}
// get any private key that matches a ids given
key, id := "", ""
for _, k := range keyIDs {
if key, err = mgr.GetPriv(k); err == nil {
id = k
break
}
}
if key == "" {
fmt.Println("error: no valid decryption key found")
os.Exit(1)
}
return key, id, nil
}