-
Notifications
You must be signed in to change notification settings - Fork 0
/
1pwsafe.go
150 lines (132 loc) · 2.5 KB
/
1pwsafe.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
package main
import (
"bytes"
"encoding/csv"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
)
var (
version string
fVersion = flag.Bool("v", false, "print program version")
)
func main() {
flag.Parse()
if *fVersion {
fmt.Fprintf(os.Stdout, "1pwsafe %s\n", version)
os.Exit(0)
}
w := csv.NewWriter(os.Stdout)
r := csv.NewReader(os.Stdin)
r.Comma = '\t'
// Skip header.
r.Read()
// Write the header.
columns := []string{
"title",
"URL",
"username",
"password",
"notes",
}
w.Write(columns)
w.Flush()
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
entry := newEntry(record)
w.Write(entry.Record())
w.Flush()
}
err := w.Error()
if err != nil {
log.Fatal(err)
}
}
type entry struct {
title string
username string
password string
url string
createdTime string
passwordModifiedTime string
recordModifiedTime string
passwordPolicy string
passwordPolicyName string
history string
email string
symbols string
notes string
}
func newEntry(record []string) *entry {
return &entry{
title: record[0],
username: record[1],
password: record[2],
url: record[3],
createdTime: record[4],
passwordModifiedTime: record[5],
recordModifiedTime: record[6],
passwordPolicy: record[7],
passwordPolicyName: record[8],
history: record[9],
email: record[10],
symbols: record[11],
notes: record[12],
}
}
func (e *entry) Title() string {
title := e.title
fields := strings.SplitN(e.title, ".", 2)
if len(fields) == 2 {
title = fields[1]
}
return strings.Replace(title, "»", ".", -1)
}
func (e *entry) Username() string {
username := e.username
if username == "" {
username = e.email
}
return username
}
func (e *entry) URL() string {
return e.url
}
func (e *entry) Password() string {
return e.password
}
func (e *entry) Email() string {
return e.email
}
func (e *entry) Notes() string {
var buf bytes.Buffer
tNotes.Execute(&buf, e)
return buf.String()
}
func (e *entry) Record() []string {
return []string{
e.Title(),
e.URL(),
e.Username(),
e.Password(),
e.Notes(),
}
}
var tNotes = template.Must(
template.New("notes").
Parse(`Notes:
Title: {{.Title}}
Username: {{.Username}}
URL: {{.URL}}
Email: {{.Email}}
`))