forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
show.go
102 lines (88 loc) · 2.07 KB
/
show.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
package action
import (
"fmt"
"os"
"github.com/atotto/clipboard"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/qrcon"
"github.com/justwatchcom/gopass/store"
"github.com/urfave/cli"
)
// Show the content of a secret file
func (s *Action) Show(c *cli.Context) error {
name := c.Args().First()
key := c.Args().Get(1)
clip := c.Bool("clip")
force := c.Bool("force")
qr := c.Bool("qr")
if name == "" {
return fmt.Errorf("provide a secret name")
}
if s.Store.IsDir(name) {
return s.List(c)
}
// auto-fallback to binary files with b64 suffix, if unique
if !s.Store.Exists(name) && s.Store.Exists(name+BinarySuffix) {
name += BinarySuffix
}
var content []byte
var err error
switch {
case key != "":
content, err = s.Store.GetKey(name, key)
if err != nil {
if err == store.ErrYAMLValueUnsupported {
return fmt.Errorf("Can not show nested key directly. Use 'gopass show %s'", name)
}
return err
}
if clip {
return s.copyToClipboard(name, content)
}
case qr:
content, err = s.Store.GetFirstLine(name)
if err != nil {
return err
}
qr, err := qrcon.QRCode(string(content))
if err != nil {
return err
}
fmt.Println(qr)
return nil
case clip:
content, err = s.Store.GetFirstLine(name)
if err != nil {
return err
}
return s.copyToClipboard(name, content)
default:
if s.Store.SafeContent() && !force {
content, err = s.Store.GetBody(name)
} else {
content, err = s.Store.Get(name)
}
if err != nil {
if err != store.ErrNotFound {
return err
}
color.Yellow("Entry '%s' not found. Starting search...", name)
if err := s.Find(c); err != nil {
return err
}
os.Exit(1)
}
}
color.Yellow(string(content))
return nil
}
func (s *Action) copyToClipboard(name string, content []byte) error {
if err := clipboard.WriteAll(string(content)); err != nil {
return err
}
if err := clearClipboard(content, s.Store.ClipTimeout()); err != nil {
return err
}
fmt.Printf("Copied %s to clipboard. Will clear in %d seconds.\n", color.YellowString(name), s.Store.ClipTimeout())
return nil
}