/
cmd_show.go
171 lines (149 loc) · 3.86 KB
/
cmd_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2022 the Paw Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package cli
import (
"context"
"fmt"
"os"
"time"
"lucor.dev/paw/internal/paw"
)
const (
clipboardWriteTimeout = 1 * time.Second
)
// Show shows an item details
type ShowCmd struct {
itemPath
clipboard bool
}
// Name returns the one word command name
func (cmd *ShowCmd) Name() string {
return "show"
}
// Description returns the command description
func (cmd *ShowCmd) Description() string {
return "Shows an item details"
}
// Usage displays the command usage
func (cmd *ShowCmd) Usage() {
template := `Usage: paw cli show [OPTION] VAULT_NAME/ITEM_TYPE/ITEM_NAME
{{ . }}
Options:
-c, --clip Do not print the password but instead copy to the clipboard
-h, --help Displays this help and exit
--session=SESSION_ID Sets a session ID to use instead of the env var
`
printUsage(template, cmd.Description())
}
// Parse parses the arguments and set the usage for the command
func (cmd *ShowCmd) Parse(args []string) error {
flags, err := newCommonFlags(flagOpts{Session: true})
if err != nil {
return err
}
flagSet.BoolVar(&cmd.clipboard, "c", false, "")
flagSet.BoolVar(&cmd.clipboard, "clip", false, "")
flags.Parse(cmd, args)
if len(flagSet.Args()) != 1 {
cmd.Usage()
os.Exit(0)
}
flags.SetEnv()
if cmd.clipboard {
err := initClipboard()
if err != nil {
return err
}
}
itemPath, err := parseItemPath(flagSet.Arg(0), itemPathOptions{fullPath: true})
if err != nil {
return err
}
cmd.itemPath = itemPath
return nil
}
// Run runs the command
func (cmd *ShowCmd) Run(s paw.Storage) error {
key, err := loadVaultKey(s, cmd.vaultName)
if err != nil {
return err
}
vault, err := s.LoadVault(cmd.vaultName, key)
if err != nil {
return err
}
item, err := paw.NewItem(cmd.itemName, cmd.itemType)
if err != nil {
return err
}
item, err = s.LoadItem(vault, item.GetMetadata())
if err != nil {
return err
}
var pclip []byte
var pclipMsg string
switch cmd.itemType {
case paw.LoginItemType:
v := item.(*paw.Login)
fmt.Printf("URL: %s\n", v.URL)
fmt.Printf("Username: %s\n", v.Username)
if !cmd.clipboard {
fmt.Printf("Password: %s\n", v.Password.Value)
} else {
pclip = []byte(v.Password.Value)
pclipMsg = "[✓] password copied to clipboard"
}
if v.Note != nil {
fmt.Printf("Note: %s\n", v.Note.Value)
}
case paw.PasswordItemType:
v := item.(*paw.Password)
if !cmd.clipboard {
fmt.Printf("Password: %s\n", v.Value)
} else {
pclip = []byte(v.Value)
pclipMsg = "[✓] password copied to clipboard"
}
if v.Note != nil {
fmt.Printf("Note: %s\n", v.Note.Value)
}
case paw.SSHKeyItemType:
v := item.(*paw.SSHKey)
if !cmd.clipboard {
fmt.Printf("Private key: %s\n", v.PrivateKey)
} else {
pclip = []byte(v.PrivateKey)
pclipMsg = "[✓] private key copied to clipboard"
}
if v.Passphrase != nil {
fmt.Printf("Passphrase: %s\n", v.Passphrase.Value)
}
fmt.Printf("Public key: %s\n", v.PublicKey)
fmt.Printf("Fingerprint: %s\n", v.Fingerprint)
addToAgent := "No"
if v.AddToAgent {
addToAgent = "Yes"
}
fmt.Printf("Add to agent: %s\n", addToAgent)
if v.Note != nil {
fmt.Printf("Note: %s\n", v.Note.Value)
}
case paw.NoteItemType:
v := item.(*paw.Note)
fmt.Printf("Note: %s\n", v.Value)
}
fmt.Printf("Modified: %s\n", item.GetMetadata().Modified.Format(time.RFC1123))
fmt.Printf("Created: %s\n", item.GetMetadata().Created.Format(time.RFC1123))
if pclip != nil {
ctx, cancel := context.WithTimeout(context.Background(), clipboardWriteTimeout)
defer cancel()
err := writeToClipboard(ctx, pclip)
if err != nil {
return nil
}
fmt.Println(pclipMsg)
}
return nil
}