feat: a Fields pane for anything but a secret, first for Azure accounts: fields and groups to move through, filter and copy (#7)

This commit was merged in pull request #7.
This commit is contained in:
keb committed 2026-10-11 10:37:40 +02:00
1 parent f6c4f5f6f6
commit 71396f1e02
13 files changed
+404 -37

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
package app
import (
"context"
"errors"
"fmt"
"strings"
tea "charm.land/bubbletea/v2"
"git.kebcloud.no/keb/lazysecrets/internal/provider"
"git.kebcloud.no/keb/lazysecrets/internal/ui/panes"
)
// The fourth pane shows Fields for anything but a secret. On a provider's
// row: its account, from what the provider knows locally, then the
// groups the account is in, loaded once a session when first shown.
type groupsState struct {
names []string
err error
loading bool
}
type groupsMsg struct {
provider string
names []string
err error
}
// syncFields fills the Fields pane for what's selected, loading an
// account's groups the first time.
func (m *Model) syncFields() tea.Cmd {
acct, ok := m.selectedAccount()
if !ok {
m.fields.Set("", nil)
return nil
}
r, _ := m.sources.Selected()
var fields []panes.Field
for _, f := range acct.Fields {
fields = append(fields, panes.Field{Label: f.Label, Value: f.Value})
}
var cmd tea.Cmd
if g, ok := m.byID[r.provider].(provider.AccountGroups); ok {
st, seen := m.groups[r.provider]
if !seen {
st = groupsState{loading: true}
m.groups[r.provider] = st
id, timeout := r.provider, m.timeout
cmd = func() tea.Msg {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
names, err := g.Groups(ctx)
return groupsMsg{provider: id, names: names, err: err}
}
}
fields = append(fields, groupFields(st)...)
}
m.fields.Set(m.byID[r.provider].Label(), fields)
return cmd
}
// fieldName names a copied field: its label, or for a group, the group.
func fieldName(f panes.Field) string {
if f.Label == "" || strings.HasPrefix(f.Label, "groups") {
return "group " + f.Value
}
return f.Label
}
// groupFields are the groups' rows: labelled on the first.
func groupFields(st groupsState) []panes.Field {
switch {
case st.loading:
return []panes.Field{{Label: "groups", Value: "loading…", Info: true}}
case errors.Is(st.err, provider.ErrUnsupported):
return nil
case st.err != nil:
return []panes.Field{{Label: "groups", Value: "couldn't list them: " + st.err.Error(), Info: true}}
case len(st.names) == 0:
return []panes.Field{{Label: "groups", Value: "none", Info: true}}
}
out := make([]panes.Field, len(st.names))
for i, n := range st.names {
out[i] = panes.Field{Value: n}
}
out[0].Label = fmt.Sprintf("groups %d", len(st.names))
return out
}