Drop dead code and hand-rolled stdlib helpers

- Logout and BackupLabel had no callers; the admin script's logout op
  only served Logout.
- contains, keys and sortedKeys become slices.Contains and
  slices.Sorted(maps.Keys(...)).
- One ExpandHome, in config, instead of a copy in deploy.
- meter reuses usageStyle; tests reuse runes and strconv.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vvvomitandClaude Opus 5.5 committed 2026-10-27 02:08:02 +03:30
1 parent ae491ea858
commit 8fd05fa513
11 files changed
+30 -88

No files matched your search

+3 -11
View File
@@ -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
}
+5 -20
View File
@@ -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
+6 -3
View File
@@ -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)})
}
+3 -1
View File
@@ -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)
}
-4
View File
@@ -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)
}
+1 -1
View File
@@ -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)
}
+2 -1
View File
@@ -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 {
-11
View File
@@ -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
+1 -12
View File
@@ -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:
+1 -11
View File
@@ -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))
+8 -13
View File
@@ -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")
}