-
Notifications
You must be signed in to change notification settings - Fork 10
/
write.go
109 lines (96 loc) · 3.2 KB
/
write.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
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) Joakim Kennedy, 2018
*/
package clinote
import (
"io"
"strconv"
"time"
"github.com/olekukonko/tablewriter"
)
const timeFormat = "2006-01-02"
var (
noteListingHeader = []string{"#", "Title", "Notebook", "Modified", "Created"}
notebookListingHeader = []string{"#", "Name"}
credentialHeader = append(notebookListingHeader, "Type")
settingsHeader = []string{"Setting", "Arguments", "Description"}
)
// WriteNoteListing creates and writes a note listing table using the writer.
func WriteNoteListing(w io.Writer, ns []*Note, nbs []*Notebook) {
table := tablewriter.NewWriter(w)
table.SetHeader(noteListingHeader)
for i, n := range ns {
index := strconv.Itoa(i + 1)
created := time.Unix(int64(n.Created)/1000, 0).Format(timeFormat)
modified := time.Unix(int64(n.Updated)/1000, 0).Format(timeFormat)
notebook := ""
for _, nb := range nbs {
if nb.GUID == n.Notebook.GUID {
notebook = nb.Name
break
}
}
table.Append([]string{index, n.Title, notebook, modified, created})
}
table.Render()
}
// WriteNotebookListing creates and writes a notebook listing table using the writer.
func WriteNotebookListing(w io.Writer, nbs []*Notebook) {
table := tablewriter.NewWriter(w)
table.SetHeader(notebookListingHeader)
for i, nb := range nbs {
index := strconv.Itoa(i + 1)
table.Append([]string{index, nb.Name})
}
table.Render()
}
// WriteCredentialListing creates and writes a credential listing table using the writer.
func WriteCredentialListing(w io.Writer, creds []*Credential) {
writeCredentialList(w, creds, false)
}
// WriteCredentialListingWithSecret creates and writes a credential listing table using the writer.
func WriteCredentialListingWithSecret(w io.Writer, creds []*Credential) {
writeCredentialList(w, creds, true)
}
func writeCredentialList(w io.Writer, creds []*Credential, includeToken bool) {
table := tablewriter.NewWriter(w)
header := credentialHeader
if includeToken {
header = append(header, "Secret")
}
table.SetHeader(header)
for i, cred := range creds {
index := strconv.Itoa(i + 1)
line := []string{index, cred.Name, cred.CredType.String()}
if includeToken {
line = append(line, cred.Secret)
}
table.Append(line)
}
table.Render()
}
// WriteSettingsListing writes the settings table to writer.
func WriteSettingsListing(w io.Writer, vals, args, desc []string) {
if len(vals) != len(args) || len(vals) != len(desc) {
return
}
table := tablewriter.NewWriter(w)
table.SetHeader(settingsHeader)
for i, val := range vals {
table.Append([]string{val, args[i], desc[i]})
}
table.Render()
}