-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
314 lines (272 loc) Β· 8.3 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"fmt"
"os"
"strings"
"github.com/atotto/clipboard"
cmd "github.com/jalpp/passdiy/cmds"
"github.com/jalpp/passdiy/extend"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
style "github.com/jalpp/passdiy/style"
)
type model struct {
list list.Model
spinner spinner.Model
textInput textinput.Model
loading bool
output string
prevOutput string
copied bool
showInput bool
inputMode string
showSublist bool
currentParent string
}
func NewModel() model {
const defaultWidth = 70
items := cmd.CreateCommandItems()
listModel := list.New(items, list.NewDefaultDelegate(), defaultWidth, len(items)+2)
spin := spinner.New(
spinner.WithSpinner(spinner.Points),
spinner.WithStyle(style.GreenStyle),
)
textInput := textinput.New()
textInput.Placeholder = "Enter input"
textInput.Focus()
textInput.CharLimit = 5000
textInput.Width = 30
return model{
list: listModel,
spinner: spin,
textInput: textInput,
loading: false,
output: "",
prevOutput: "",
copied: false,
showInput: false,
inputMode: "",
showSublist: false,
currentParent: "",
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "esc":
if m.showSublist {
m.showSublist = false
m.currentParent = ""
m.list.SetItems(cmd.CreateCommandItems())
return m, nil
}
if m.showInput {
m.showInput = false
m.currentParent = ""
m.list.SetItems(m.list.Items())
return m, nil
}
case "c":
if m.output != "" {
clipboard.WriteAll(m.output)
m.copied = true
}
case "x":
if m.output != "" {
m.output = ""
}
return m, nil
case "enter":
if m.loading {
return m, nil
}
if m.showInput {
inputValue := m.textInput.Value()
m.textInput.SetValue("")
m.showInput = false
if cmd.IsHashCommand(m.inputMode) && inputValue != "" {
return m, tea.Batch(cmd.ExecuteCommand(m.inputMode, inputValue), m.spinner.Tick)
}
if cmd.IsConfigCommand(m.inputMode) && inputValue != "" {
return m, tea.Batch(cmd.ExecuteCommand(m.inputMode, inputValue), m.spinner.Tick)
}
if m.inputMode == "1passstore" {
if inputValue != "" && strings.Contains(inputValue, "|") {
return m, tea.Batch(cmd.ExecuteCommand("1passstore", inputValue), m.spinner.Tick)
}
m.output = "Please provide input in 'user|value|url' format."
return m, nil
}
if m.inputMode == "hcpvaultstore" {
if inputValue != "" && strings.Contains(inputValue, "=") {
return m, tea.Batch(cmd.ExecuteCommand("hcpvaultstore", inputValue), m.spinner.Tick)
}
m.output = "Please provide input in 'name=value' format."
return m, nil
}
m.output = "Please provide valid input."
return m, nil
}
if item, ok := m.list.SelectedItem().(cmd.CommandItem); ok {
var selectedItem string
if item.FilterValue() != "" {
selectedItem = item.FilterValue()
} else {
selectedItem = item.Title()
}
selectedItemList := item
var subItems []list.Item
for _, subcmd := range selectedItemList.Subcmd {
subItems = append(subItems, list.Item(subcmd))
}
if len(selectedItemList.Subcmd) > 0 && !m.showSublist {
m.showSublist = true
m.currentParent = selectedItemList.Title()
m.list.SetItems(subItems)
return m, nil
}
if m.showSublist {
if cmd.IsCommandInputMode(selectedItem) {
m.inputMode = selectedItem
m.textInput.SetValue("")
m.prevOutput = ""
m.showInput = true
m.textInput.Focus()
return m, nil
}
m.loading = true
m.copied = false
selectedCommand := selectedItemList.Title()
return m, tea.Batch(cmd.ExecuteCommand(selectedCommand, m.prevOutput), m.spinner.Tick)
}
m.loading = true
m.copied = false
return m, tea.Batch(cmd.ExecuteCommand(selectedItem, m.prevOutput), m.spinner.Tick)
} else {
fmt.Printf("SelectedItem is not of type cmd.CommandItem or is nil")
}
}
case tea.WindowSizeMsg:
m.list.SetSize(msg.Width, msg.Height)
case spinner.TickMsg:
if m.loading {
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
case cmd.CommandFinishedMsg:
m.loading = false
m.output = msg.Result()
m.prevOutput = msg.Result()
}
if m.showInput {
var cmd tea.Cmd
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
if m.loading {
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
if m.loading {
return style.GreenStyle.Render(fmt.Sprintf("%s Processing command...\n\n%s", m.spinner.View(), ""))
}
if m.showInput {
maskedInput := cmd.CoverUp(m.textInput.Value())
switch m.inputMode {
case "hcpvaultstore":
return style.VaultStyle.Render(fmt.Sprintf(
"Enter the token in 'name=value' format and press Enter:\n\n%s\n\n",
maskedInput,
))
case "bcrypthash":
return style.GreenStyle.Render(fmt.Sprintf(
"Enter the token/password for hashing with bcrypthash and press Enter:\n\n%s\n\n",
maskedInput,
))
case "argonhash":
return style.GreenStyle.Render(fmt.Sprintf(
"Enter the token/password for hashing with argonhash and press Enter:\n\n%s\n\n",
maskedInput,
))
case "1passstore":
return style.OPassStyle.Render(fmt.Sprintf(
"Enter the password/token in 'username|password|url' format and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configpass":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the new password char length and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configtoken":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the new token char length and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configpin":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the new pin digit length and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configsalt":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the new salt char length and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configpwp":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the new pwp word count and press Enter: \n\n%s\n\n",
maskedInput,
))
case "configmul":
return style.ConfigStyle.Render(fmt.Sprintf(
"Enter the number of password/token/pins you want to generate and press Enter: \n\n%s\n\n",
maskedInput,
))
}
}
copyMessage := ""
if m.copied {
copyMessage = style.GreenStyle.Render("\nπ Buffer value copied to clipboard!")
}
if strings.Contains(strings.ToLower(m.output), "please") || strings.Contains(strings.ToLower(m.output), "error") {
return style.ErrorStyle.Render(fmt.Sprintf("%s\n\n β Error: %s", m.list.View(), m.output))
}
if strings.Contains(strings.ToLower(m.output), "authentication is required") || strings.Contains(strings.ToLower(m.output), "Unauthorized") {
return style.ErrorStyle.Render(fmt.Sprintf("%s\n\n β Error: %s", m.list.View(), "HCP_API_TOKEN expired, please run hcpvaultconnect to re connect to HCP Vault"))
}
if strings.Contains(strings.ToLower(m.output), "hashicorp") {
return style.VaultStyle.Render(fmt.Sprintf("%s\n\n β Vault: %s", m.list.View(), m.output))
}
if strings.Contains(strings.ToLower(m.output), "1password") {
return style.OPassStyle.Render(fmt.Sprintf("%s\n\n1Password Vault: %s", m.list.View(), m.output))
}
if strings.Contains(strings.ToLower(m.output), "passdiy") {
return style.ConfigStyle.Render(fmt.Sprintf("%s\n\n %s", m.list.View(), m.output))
}
if strings.Contains(strings.ToLower(m.output), extend.VAULT_PREFIX) {
return style.CustomStyle.Render(fmt.Sprintf("%s\n\n %s", m.list.View(), m.output))
}
return style.GreenStyle.Render(fmt.Sprintf("%s\n\n π [c] Copy [esc] Exist Sublist [x] Clear \n π Buffer: %s%s", m.list.View(), cmd.CoverUp(m.output), copyMessage))
}
func main() {
if _, err := tea.NewProgram(NewModel(), tea.WithAltScreen()).Run(); err != nil {
fmt.Printf("Uh oh, there was an error: %v\n", err)
os.Exit(1)
}
}