-
Notifications
You must be signed in to change notification settings - Fork 13
/
copy.go
71 lines (57 loc) · 1.46 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
package copy
import (
"strings"
"time"
"github.com/GGP1/kure/auth"
cmdutil "github.com/GGP1/kure/commands"
"github.com/GGP1/kure/db/entry"
"github.com/spf13/cobra"
bolt "go.etcd.io/bbolt"
)
var example = `
* Copy password and clean after 15m
kure copy Sample -t 15m
* Copy username
kure copy Sample -u`
type copyOptions struct {
timeout time.Duration
username bool
}
// NewCmd returns a new command.
func NewCmd(db *bolt.DB) *cobra.Command {
opts := copyOptions{}
cmd := &cobra.Command{
Use: "copy <name>",
Short: "Copy entry credentials to the clipboard",
Aliases: []string{"cp"},
Example: example,
Args: cmdutil.MustExist(db, cmdutil.Entry),
PreRunE: auth.Login(db),
RunE: runCopy(db, &opts),
PostRun: func(cmd *cobra.Command, args []string) {
// Reset variables (session)
opts = copyOptions{}
},
}
f := cmd.Flags()
f.DurationVarP(&opts.timeout, "timeout", "t", 0, "clipboard clearing timeout")
f.BoolVarP(&opts.username, "username", "u", false, "copy entry username")
return cmd
}
func runCopy(db *bolt.DB, opts *copyOptions) cmdutil.RunEFunc {
return func(cmd *cobra.Command, args []string) error {
name := strings.Join(args, " ")
name = cmdutil.NormalizeName(name)
e, err := entry.Get(db, name)
if err != nil {
return err
}
field := "Password"
copy := e.Password
if opts.username {
field = "Username"
copy = e.Username
}
return cmdutil.WriteClipboard(cmd, opts.timeout, field, copy)
}
}