refactor: the account pane becomes Fields, the pane for anything but a secret
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
0a80073aad
commit
e64748e85d
5 files changed
+46
-44
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user