-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.go
More file actions
170 lines (144 loc) · 3.85 KB
/
Copy pathmodel.go
File metadata and controls
170 lines (144 loc) · 3.85 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
package main
import (
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
)
type model struct {
list list.Model
spinner spinner.Model
viewport viewport.Model
width int
height int
mode viewMode
prevMode viewMode
clients []Client
statusMsg string
errorMsg string
bwSession string
bwInstalled bool
bwUnlocked bool
bwChecking bool
bwStatus string // "unlocked", "locked", "unauthenticated"
bwItems []BWItem
bwSelectList list.Model
bwPwInput textinput.Model
bwUnlocking bool
bwUnlockErr string
form *huh.Form
editingIndex int
formClient *Client
pendingAction string
tokenResult *TokenResult
tokenLoading bool
deleteIndex int
}
func initialModel(bwSession string) model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = spinnerStyle
clients, _ := loadClients()
delegate := list.NewDefaultDelegate()
l := list.New(clientsToItems(clients), delegate, 0, 0)
l.Title = "tkz"
l.SetShowStatusBar(true)
l.SetFilteringEnabled(true)
l.SetShowHelp(false)
l.Styles.Title = titleStyle
vp := viewport.New(80, 20)
pwInput := textinput.New()
pwInput.Placeholder = "Master password"
pwInput.EchoMode = textinput.EchoPassword
pwInput.EchoCharacter = '*'
pwInput.Width = 40
bwList := list.New([]list.Item{}, list.NewDefaultDelegate(), 0, 0)
bwList.Title = "Select Bitwarden Item"
bwList.SetShowStatusBar(true)
bwList.SetFilteringEnabled(true)
bwList.SetShowHelp(false)
bwList.Styles.Title = titleStyle
return model{
list: l,
spinner: s,
viewport: vp,
bwPwInput: pwInput,
bwSelectList: bwList,
bwChecking: true,
mode: listView,
clients: clients,
bwSession: bwSession,
editingIndex: -1,
}
}
func (m model) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
checkBWStatus(m.bwSession),
)
}
func clientsToItems(clients []Client) []list.Item {
items := make([]list.Item, len(clients))
for i, c := range clients {
items[i] = c
}
return items
}
func bwItemsToListItems(items []BWItem) []list.Item {
listItems := make([]list.Item, len(items))
for i, item := range items {
listItems[i] = item
}
return listItems
}
func (m *model) updateList() {
m.list.SetItems(clientsToItems(m.clients))
}
func (m *model) updateBWSelectList() {
m.bwSelectList.SetItems(bwItemsToListItems(m.bwItems))
}
func (m *model) setErrorContent(errMsg string) {
width := m.viewport.Width
if width <= 0 {
width = 76
}
wrapped := lipgloss.NewStyle().Width(width).Render(errMsg)
m.viewport.SetContent(wrapped)
m.viewport.GotoTop()
}
func buildClientForm(client *Client) *huh.Form {
return huh.NewForm(
huh.NewGroup(
huh.NewInput().
Title("Display Name").
Value(&client.Name).
Placeholder("e.g., keycloak-dev"),
huh.NewInput().
Title("Client ID").
Value(&client.ClientID).
Placeholder("leave empty to pull from Bitwarden").
Description("Manual override — skips BW lookup for client_id"),
huh.NewInput().
Title("Client ID Field").
Value(&client.ClientIDField).
Placeholder("login.username").
Description("BW field: login.username, login.password, fields.<name>, notes"),
huh.NewInput().
Title("Client Secret Field").
Value(&client.ClientSecretField).
Placeholder("login.password").
Description("BW field: login.username, login.password, fields.<name>, notes"),
huh.NewInput().
Title("Issuer URL").
Value(&client.Issuer).
Placeholder("https://auth.example.com/realms/myrealm"),
huh.NewInput().
Title("Scopes (space-separated)").
Value(&client.Scopes).
Placeholder("openid profile email"),
),
).WithTheme(huh.ThemeDracula()).WithWidth(60)
}