diff --git a/internal/config/config.go b/internal/config/config.go index a64bc3b..a9c3631 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,9 +4,10 @@ package config import ( "encoding/json" + "maps" "os" "path/filepath" - "sort" + "slices" "strings" "gopkg.in/yaml.v3" @@ -293,7 +294,7 @@ func (c *Config) Missing() []string { } } } - for _, k := range sortedKeys(c.Overrides) { + for _, k := range slices.Sorted(maps.Keys(c.Overrides)) { if err := yaml.Unmarshal([]byte(c.Overrides[k]), new(any)); err != nil { out = append(out, "Fine-tune: "+k+" isn't valid YAML ("+yamlError(err)+")") } @@ -314,12 +315,3 @@ func yamlError(err error) string { } return strings.TrimPrefix(err.Error(), "yaml: ") } - -func sortedKeys(m map[string]string) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - sort.Strings(keys) - return keys -} diff --git a/internal/config/hosts.go b/internal/config/hosts.go index 64110eb..a0f00e1 100644 --- a/internal/config/hosts.go +++ b/internal/config/hosts.go @@ -1,9 +1,11 @@ package config import ( + "maps" "os" "path/filepath" "regexp" + "slices" "strings" "sync" "time" @@ -48,10 +50,10 @@ func (c *Config) hostTable() ([]string, map[string][]string) { if h == "" { return } - if !contains(recs, h) { + if !slices.Contains(recs, h) { recs = append(recs, h) } - if !contains(on[h], what) { + if !slices.Contains(on[h], what) { on[h] = append(on[h], what) } } @@ -90,20 +92,11 @@ func (c *Config) hostTable() ([]string, map[string][]string) { return recs, on } -func contains(list []string, s string) bool { - for _, x := range list { - if x == s { - return true - } - } - return false -} - // hostVars are the variables that may hold a component's hostname: one per // role it switches on. func hostVars(comp catalog.Component) []string { var out []string - for _, v := range append([]string{comp.EnableVar}, keys(comp.Extra)...) { + for _, v := range append([]string{comp.EnableVar}, slices.Collect(maps.Keys(comp.Extra))...) { if p, ok := strings.CutSuffix(v, "_enabled"); ok && p != "" { out = append(out, p+"_hostname") } @@ -111,14 +104,6 @@ func hostVars(comp catalog.Component) []string { return out } -func keys(m map[string]string) []string { - var out []string - for k := range m { - out = append(out, k) - } - return out -} - type hostResolver struct { vars map[string]any // vars.yml as interzone writes it defaults map[string]string // the playbook's, as raw YAML scalars diff --git a/internal/config/importer.go b/internal/config/importer.go index cd686ff..1995b0d 100644 --- a/internal/config/importer.go +++ b/internal/config/importer.go @@ -2,11 +2,13 @@ package config import ( "fmt" + "maps" "os" "os/exec" "path/filepath" "reflect" "regexp" + "slices" "sort" "strings" @@ -104,7 +106,7 @@ func ImportMDAD(path string, base *Config) (*Import, error) { // locate finds vars.yml and the inventory around path. func locate(path string) (*Import, error) { - path, err := filepath.Abs(expandHome(path)) + path, err := filepath.Abs(ExpandHome(path)) if err != nil { return nil, err } @@ -150,7 +152,8 @@ func locate(path string) (*Import, error) { return im, nil } -func expandHome(p string) string { +// ExpandHome turns a leading ~/ into the home directory. +func ExpandHome(p string) string { if rest, ok := strings.CutPrefix(p, "~/"); ok { if h, err := os.UserHomeDir(); err == nil { return filepath.Join(h, rest) @@ -822,7 +825,7 @@ func VarChanges(old, cur string) []VarChange { return fmt.Sprint(v) } var out []VarChange - for _, k := range sortedKeys(mergeKeys(o, n)) { + for _, k := range slices.Sorted(maps.Keys(mergeKeys(o, n))) { if !reflect.DeepEqual(o[k], n[k]) { out = append(out, VarChange{k, flat(o, k), flat(n, k)}) } diff --git a/internal/config/render.go b/internal/config/render.go index fd5093b..54e42e2 100644 --- a/internal/config/render.go +++ b/internal/config/render.go @@ -2,8 +2,10 @@ package config import ( "fmt" + "maps" "os" "regexp" + "slices" "sort" "strconv" "strings" @@ -311,7 +313,7 @@ tls: } var rest []string - for _, k := range sortedKeys(c.Overrides) { + for _, k := range slices.Sorted(maps.Keys(c.Overrides)) { if !w.seen[k] { rest = append(rest, k) } diff --git a/internal/deploy/backups.go b/internal/deploy/backups.go index 2e30092..974955c 100644 --- a/internal/deploy/backups.go +++ b/internal/deploy/backups.go @@ -303,7 +303,3 @@ func HumanSize(n int64) string { return fmt.Sprintf("%.1f %cB", float64(n)/float64(div), "KMGTPE"[exp]) } -// BackupLabel describes a dump for a list: when, how big. -func BackupLabel(b LocalBackup) string { - return b.Time.Local().Format("2006-01-02 15:04") + " " + HumanSize(b.Size) + " " + filepath.Base(b.Path) -} diff --git a/internal/deploy/migrate.go b/internal/deploy/migrate.go index 06942b6..0653706 100644 --- a/internal/deploy/migrate.go +++ b/internal/deploy/migrate.go @@ -29,7 +29,7 @@ func sshArgs(t config.Target) []string { args = append(args, "-p", t.Port) } if t.SSHKey != "" { - args = append(args, "-i", expandHome(t.SSHKey)) + args = append(args, "-i", config.ExpandHome(t.SSHKey)) } return append(args, t.User+"@"+t.Host) } diff --git a/internal/deploy/mirrors_test.go b/internal/deploy/mirrors_test.go index b2c2ffb..b5687ba 100644 --- a/internal/deploy/mirrors_test.go +++ b/internal/deploy/mirrors_test.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path/filepath" + "strconv" "strings" "testing" "time" @@ -144,7 +145,7 @@ func TestProbeDistro(t *testing.T) { } } -func itoa(n int64) string { b, _ := json.Marshal(n); return string(b) } +func itoa(n int64) string { return strconv.FormatInt(n, 10) } func TestServerNeeds(t *testing.T) { for _, tc := range []struct { diff --git a/internal/deploy/server.go b/internal/deploy/server.go index 95f7773..88410f8 100644 --- a/internal/deploy/server.go +++ b/internal/deploy/server.go @@ -5,9 +5,7 @@ import ( "context" "fmt" "net" - "os" "os/exec" - "path/filepath" "sort" "strconv" "strings" @@ -64,15 +62,6 @@ else echo "sudo=fail"; fi func shellQuote(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" } -func expandHome(p string) string { - if strings.HasPrefix(p, "~/") { - if h, err := os.UserHomeDir(); err == nil { - return filepath.Join(h, p[2:]) - } - } - return p -} - // probe runs probeScript on the target, over SSH or locally. func probe(ctx context.Context, c *config.Config) (map[string]string, error) { t := c.Target diff --git a/internal/deploy/users.go b/internal/deploy/users.go index 05dfc98..f3ed125 100644 --- a/internal/deploy/users.go +++ b/internal/deploy/users.go @@ -109,9 +109,6 @@ def appservices(): pass return out def run(tok): - if req["op"] == "logout": - call("POST", "/_matrix/client/v3/logout", {}, token=tok) - return {"ok": True} if req["op"] == "list": users, frm = [], "0" while frm is not None: @@ -172,7 +169,7 @@ try: try: out = run(tok) except Failed as e: - if e.args[0]["errcode"] != "M_UNKNOWN_TOKEN" or not req.get("token") or req["op"] == "logout": + if e.args[0]["errcode"] != "M_UNKNOWN_TOKEN" or not req.get("token"): raise tok = login() out = run(tok) @@ -364,14 +361,6 @@ func UpdateUser(ctx context.Context, c *config.Config, login *AdminLogin, userID return err } -// Logout ends the admin session, removing interzone's device. -func Logout(ctx context.Context, c *config.Config, login *AdminLogin) { - if login.Token != "" && !UsesMAS(c) { - _, _ = adminCall(ctx, c, login, map[string]any{"op": "logout"}) - login.Token = "" - } -} - func truthy(v any) bool { switch x := v.(type) { case bool: diff --git a/internal/ui/dashboard.go b/internal/ui/dashboard.go index d8349ff..85901b3 100644 --- a/internal/ui/dashboard.go +++ b/internal/ui/dashboard.go @@ -155,17 +155,7 @@ func (s *dashboardScreen) update(msg tea.Msg) tea.Cmd { } // meter draws a usage bar, amber above 75% and red above 90%. -func meter(frac float64, w int) string { - frac = max(0, min(1, frac)) - style := sGood - switch { - case frac > 0.9: - style = sBad - case frac > 0.75: - style = sWarn - } - return bar(frac, w, style) -} +func meter(frac float64, w int) string { return bar(frac, w, usageStyle(frac)) } func bar(frac float64, w int, style lipgloss.Style) string { frac = max(0, min(1, frac)) diff --git a/internal/ui/tokens_test.go b/internal/ui/tokens_test.go index 613018c..c20df78 100644 --- a/internal/ui/tokens_test.go +++ b/internal/ui/tokens_test.go @@ -7,34 +7,29 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -func tkey(name string) tea.KeyMsg { - k, _ := keyMsg(name) - return k -} - // The screen's commands reach MAS, so these tests only look at what the // keys set up, not run what they return. func TestTokensRevokeConfirms(t *testing.T) { a := demoApp(t, 120, 36) openTokens(a) ts := a.cur().(*tokensScreen) - ts.update(tkey("down")) - ts.update(tkey("x")) + ts.update(tea.KeyMsg{Type: tea.KeyDown}) + ts.update(runes("x")) if !ts.confirm || !strings.Contains(ts.footer(100), "book-club") { t.Fatalf("confirm %v: %q", ts.confirm, ts.footer(100)) } // Moving away cancels, as does esc. - ts.update(tkey("down")) + ts.update(tea.KeyMsg{Type: tea.KeyDown}) if ts.confirm || ts.cursor != 2 { t.Fatalf("confirm %v cursor %d", ts.confirm, ts.cursor) } - ts.update(tkey("x")) - ts.update(tkey("esc")) + ts.update(runes("x")) + ts.update(tea.KeyMsg{Type: tea.KeyEsc}) if ts.confirm || ts.busy { t.Fatal("esc didn't cancel") } - ts.update(tkey("x")) - if cmd := ts.update(tkey("y")); cmd == nil || !ts.busy || ts.confirm { + ts.update(runes("x")) + if cmd := ts.update(runes("y")); cmd == nil || !ts.busy || ts.confirm { t.Error("y didn't revoke") } } @@ -43,7 +38,7 @@ func TestTokensFormChecksNumbers(t *testing.T) { a := demoApp(t, 120, 36) openTokens(a) ts := a.cur().(*tokensScreen) - ts.update(tkey("n")) + ts.update(runes("n")) if ts.form == nil || len(ts.form.value("token")) != 12 { t.Fatal("no form with a token") }