-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_copy.go
58 lines (52 loc) · 1.13 KB
/
cmd_copy.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
package main
import (
"errors"
"fmt"
"github.com/atotto/clipboard"
)
var cmdCopy = &Command{
Run: runCopy,
UsageLine: "copy NAME",
Short: "Copy password to clipboard",
Long: `Find password and copy to clipboard.`,
}
func runCopy(ctx context, args []string) error {
if len(args) == 0 {
return errors.New("item name is required")
}
cfg, err := GetConfig()
if err != nil {
return err
}
Initialize(cfg)
is, err := LoadItemsWithConfig(cfg)
if err != nil {
return err
}
if is.HasMaster() {
if err = confirmMasterPassword(is.Master()); err != nil {
return err
}
}
findAndCopy(ctx, is, args[0])
PrintSuccess(ctx.out, "password of '%s' is copied to clipboard successfully", args[0])
return nil
}
func confirmMasterPassword(it *Item) error {
pwd, err := scanPassword("Master password: ")
if err != nil {
return err
}
if it.Password != pwd {
return errMasterPasswordNotMatch
}
return nil
}
func findAndCopy(ctx context, is Items, name string) error {
it := is.Find(name)
if it == nil || it.Master {
return fmt.Errorf("item not found: %s", name)
}
clipboard.WriteAll(it.Password)
return nil
}