-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.go
More file actions
213 lines (184 loc) · 5.09 KB
/
Copy pathview.go
File metadata and controls
213 lines (184 loc) · 5.09 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"strings"
)
func (m model) View() string {
switch m.mode {
case bwPasswordView:
return m.viewBWPassword()
case bwLoginView:
return m.viewBWLogin()
case bwSelectView:
return m.viewBWSelect()
case formView:
return m.viewForm()
case tokenView:
return m.viewToken()
case errorView:
return m.viewError()
case deleteView:
return m.viewDelete()
default:
return m.viewList()
}
}
func (m model) viewBWPassword() string {
var b strings.Builder
b.WriteString(titleStyle.Render("Unlock Bitwarden Vault"))
b.WriteString("\n\n")
if m.bwUnlocking {
b.WriteString(m.spinner.View())
if m.bwUnlocked {
b.WriteString(" Loading vault items...")
} else {
b.WriteString(" Unlocking vault...")
}
b.WriteString("\n\n")
return b.String()
}
if m.bwUnlockErr != "" {
b.WriteString(errorStyle.Render(m.bwUnlockErr))
b.WriteString("\n\n")
}
b.WriteString("Master password: ")
b.WriteString(m.bwPwInput.View())
b.WriteString("\n\n")
b.WriteString(helpStyle.Render("enter: unlock • esc: back • ctrl+c: quit"))
return b.String()
}
func (m model) viewBWLogin() string {
var b strings.Builder
b.WriteString(titleStyle.Render("Bitwarden Not Logged In"))
b.WriteString("\n\n")
if !m.bwInstalled {
b.WriteString("The Bitwarden CLI (")
b.WriteString(accentStyle.Render("bw"))
b.WriteString(") was not found.\n\n")
b.WriteString("Install it:\n\n")
b.WriteString(accentStyle.Render(" brew install bitwarden-cli"))
b.WriteString("\n\n")
} else {
b.WriteString("You need to log in to Bitwarden first.\n\n")
b.WriteString("Run in another terminal:\n\n")
b.WriteString(accentStyle.Render(" bw login"))
b.WriteString("\n\n")
b.WriteString("Then press ")
b.WriteString(accentStyle.Render("r"))
b.WriteString(" to retry.")
b.WriteString("\n\n")
}
b.WriteString(helpStyle.Render("r: retry • esc: back • q: quit"))
return b.String()
}
func (m model) viewBWSelect() string {
return m.bwSelectList.View()
}
func (m model) viewForm() string {
var b strings.Builder
title := "Add OAuth Client"
if m.editingIndex >= 0 {
title = "Edit OAuth Client"
}
b.WriteString(titleStyle.Render(title))
b.WriteString("\n\n")
if m.form != nil {
b.WriteString(m.form.View())
}
return b.String()
}
func (m model) viewToken() string {
var b strings.Builder
if m.tokenLoading {
b.WriteString(titleStyle.Render("Requesting Token"))
b.WriteString("\n\n")
b.WriteString(m.spinner.View())
b.WriteString(" Fetching token...")
b.WriteString("\n\n")
b.WriteString(helpStyle.Render("esc: cancel"))
return b.String()
}
if m.tokenResult != nil {
b.WriteString(titleStyle.Render("Token for " + m.tokenResult.Client.Name))
b.WriteString("\n\n")
truncated := m.tokenResult.Token.AccessToken
if len(truncated) > 80 {
truncated = truncated[:40] + "..." + truncated[len(truncated)-40:]
}
content := fmt.Sprintf(
"%s %s\n%s %d seconds\n%s %s",
accentStyle.Render("Type:"),
m.tokenResult.Token.TokenType,
accentStyle.Render("Expires:"),
m.tokenResult.Token.ExpiresIn,
accentStyle.Render("Token:"),
tokenStyle.Render(truncated),
)
if m.tokenResult.Token.Scope != "" {
content += fmt.Sprintf("\n%s %s",
accentStyle.Render("Scope:"),
m.tokenResult.Token.Scope,
)
}
b.WriteString(tokenBoxStyle.Render(content))
b.WriteString("\n\n")
if m.statusMsg != "" {
b.WriteString(successStyle.Render(m.statusMsg))
b.WriteString("\n\n")
}
b.WriteString(helpStyle.Render("c: copy token • h: copy as Authorization header • esc: back"))
}
return b.String()
}
func (m model) viewError() string {
var b strings.Builder
b.WriteString(errorStyle.Render("Error"))
b.WriteString("\n\n")
b.WriteString(m.viewport.View())
b.WriteString("\n\n")
b.WriteString(helpStyle.Render("esc/enter: dismiss"))
return b.String()
}
func (m model) viewDelete() string {
var b strings.Builder
if m.deleteIndex >= 0 && m.deleteIndex < len(m.clients) {
client := m.clients[m.deleteIndex]
b.WriteString(warningStyle.Render("Delete client: " + client.Name + "?"))
} else {
b.WriteString(warningStyle.Render("Delete client?"))
}
b.WriteString("\n\n")
b.WriteString(helpStyle.Render("y/enter: delete • n/esc: cancel"))
return b.String()
}
func (m model) viewList() string {
if m.bwChecking {
var b strings.Builder
b.WriteString(titleStyle.Render("tkz"))
b.WriteString("\n\n")
b.WriteString(m.spinner.View())
b.WriteString(" Connecting to Bitwarden...")
b.WriteString("\n\n")
b.WriteString(helpStyle.Render("ctrl+c: quit"))
return b.String()
}
var b strings.Builder
b.WriteString(m.list.View())
b.WriteString("\n")
if m.statusMsg != "" {
b.WriteString(dimStyle.Render(m.statusMsg))
b.WriteString(" ")
}
bwIndicator := ""
if !m.bwInstalled {
bwIndicator = errorStyle.Render("[bw not found]")
} else if !m.bwUnlocked {
bwIndicator = warningStyle.Render("[vault locked]")
} else {
bwIndicator = successStyle.Render("[vault unlocked]")
}
b.WriteString(bwIndicator)
b.WriteString("\n")
b.WriteString(helpStyle.Render("enter: get token • a: add • e: edit • d: delete • /: filter • q: quit"))
return b.String()
}