-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (128 loc) · 3.55 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/miquella/opvault"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
)
var vaultPath = flag.String("vault", "~/.1pw", "path to vault")
var revealPasswords = flag.Bool("reveal", false, "reveal passwords in output")
var profileName = flag.String("profile", "default", "profile name")
var noCopyPassword = flag.Bool("no-copy", false, "don't copy password to clipboard")
func main() {
flag.Parse()
vault, err := opvault.Open(expandHome(*vaultPath))
if err != nil {
log.Fatalf("open error: %s", err)
}
profile, err := vault.Profile(*profileName)
if err != nil {
log.Fatalf("profile error: %s", err)
}
fmt.Print("Password: ")
password, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatalf("password input error: %s", err)
}
if err := profile.Unlock(string(password)); err != nil {
log.Fatalf("unlock error: %s", err)
}
fmt.Println("\n")
items, err := profile.Items()
if err != nil {
log.Fatalf("items error: %s", err)
}
fzfArgs := []string{"--height=20%", "--min-height=15", "--header=search items"}
if len(flag.Args()) > 0 {
fzfArgs = append(fzfArgs, []string{"-1", "--query=" + strings.Join(flag.Args(), " ")}...)
}
fzf := exec.Command("fzf", fzfArgs...)
fzfIn, err := fzf.StdinPipe()
fzf.Stderr = os.Stderr
fzfOut, err := fzf.StdoutPipe()
done := make(chan struct{})
go func() {
defer close(done)
if err := fzf.Run(); err != nil {
log.Fatalf("fzf error: %s", err)
}
}()
for _, item := range items {
fmt.Fprintln(fzfIn, item.Title())
}
fzfIn.Close()
sel, err := ioutil.ReadAll(fzfOut)
if err != nil {
log.Fatalf("fzf read error: %s", err)
}
selected := strings.Trim(string(sel), "\n")
<-done
var didClipboard bool
for _, item := range items {
if item.Title() != selected {
continue
}
detail, err := item.Detail()
if err != nil {
log.Fatalf("item detail error: %s", err)
}
fmt.Printf("%s\n\tCategory:%s\n\tTags:%s\n", item.Title(), item.Category(), item.Tags())
for _, field := range detail.Fields() {
printValue := field.Value()
if !*revealPasswords && field.Type() == opvault.PasswordFieldType {
printValue = "********"
}
if !didClipboard && !*noCopyPassword && field.Type() == opvault.PasswordFieldType {
setClipboard(field.Value())
printValue += " [copied]"
didClipboard = true
}
fmt.Printf("\t%s (%s) = %s\n", field.Name(), field.Type(), printValue)
}
for _, section := range detail.Sections() {
fmt.Printf("\t%s / %s\n", section.Name(), section.Title())
for _, field := range section.Fields() {
printValue := field.Value()
if !*revealPasswords && field.Kind() == opvault.ConcealedFieldKind {
printValue = "********"
}
if !didClipboard && !*noCopyPassword && field.Kind() == opvault.ConcealedFieldKind {
setClipboard(field.Value())
printValue += " [copied]"
didClipboard = true
}
fmt.Printf("\t\t%s / %s (%s) = %s\n", field.Name(), field.Title(), field.Kind(), printValue)
}
}
}
}
func expandHome(path string) string {
if strings.HasPrefix(path, "~/") {
u, err := user.Current()
if err != nil {
log.Fatalf("user error: %s", err)
}
path = filepath.Join(u.HomeDir, path[2:])
}
return path
}
func setClipboard(str string) error {
cmd := exec.Command("xclip", "-selection", "clipboard")
in, _ := cmd.StdinPipe()
done := make(chan error)
go func() {
done <- cmd.Run()
}()
if _, err := in.Write([]byte(str)); err != nil {
log.Fatalf("copy error: %s", err)
}
in.Close()
return <-done
}