wrapspec.go

  1package cmd
  2
  3import (
  4	"slices"
  5
  6	"git.secluded.site/keld/internal/ui/screens"
  7)
  8
  9// wrappedCommand describes a restic command that keld exposes in its
 10// interactive menu and shell completions.
 11type wrappedCommand struct {
 12	Name   string
 13	Hotkey rune
 14}
 15
 16// wrappedCommands lists the restic commands keld actively wraps. Adding
 17// an entry here automatically surfaces it in the TUI menu and shell
 18// completions. The order determines menu display order.
 19var wrappedCommands = []wrappedCommand{
 20	{Name: "backup", Hotkey: 'b'},
 21	{Name: "restore", Hotkey: 'r'},
 22	{Name: "snapshots", Hotkey: 's'},
 23	{Name: "forget", Hotkey: 'f'},
 24	{Name: "check", Hotkey: 'c'},
 25	{Name: "init", Hotkey: 'i'},
 26}
 27
 28// menuItems is derived from wrappedCommands for the interactive menu.
 29var menuItems = func() []screens.MenuItem {
 30	items := make([]screens.MenuItem, len(wrappedCommands))
 31	for i, wc := range wrappedCommands {
 32		items[i] = screens.MenuItem{Label: wc.Name, Hotkey: wc.Hotkey}
 33	}
 34	return items
 35}()
 36
 37// knownCommands is derived from wrappedCommands for shell completion.
 38var knownCommands = func() []string {
 39	names := make([]string, len(wrappedCommands))
 40	for i, wc := range wrappedCommands {
 41		names[i] = wc.Name
 42	}
 43	return names
 44}()
 45
 46// isWrappedCommand reports whether the given command name is one of
 47// the commands keld actively wraps with TUI screens.
 48func isWrappedCommand(name string) bool {
 49	return slices.Contains(knownCommands, name)
 50}
 51
 52// flagContract describes a restic flag that keld semantically depends on
 53// (e.g. used in HasFlag checks or keyAliases).
 54type flagContract struct {
 55	Command    string
 56	Name       string
 57	IsBool     bool
 58	Repeatable bool
 59}
 60
 61// flagContracts lists flags keld's wrapper logic hardcodes assumptions
 62// about. Contract tests verify these still match the generated data.
 63var flagContracts = []flagContract{
 64	{Command: "restore", Name: "target", IsBool: false, Repeatable: false},
 65	{Command: "global", Name: "repo", IsBool: false, Repeatable: false},
 66}
 67
 68// passthroughCommands lists restic commands that keld does not actively wrap
 69// but should still allow through without error. Contract tests verify that
 70// every command in restic.Commands is either wrapped or listed here.
 71var passthroughCommands = map[string]struct{}{
 72	"cache":            {},
 73	"cat":              {},
 74	"copy":             {},
 75	"diff":             {},
 76	"dump":             {},
 77	"features":         {},
 78	"find":             {},
 79	"generate":         {},
 80	"key":              {},
 81	"key-add":          {},
 82	"key-list":         {},
 83	"key-passwd":       {},
 84	"key-remove":       {},
 85	"list":             {},
 86	"ls":               {},
 87	"migrate":          {},
 88	"mount":            {},
 89	"options":          {},
 90	"prune":            {},
 91	"recover":          {},
 92	"repair":           {},
 93	"repair-index":     {},
 94	"repair-packs":     {},
 95	"repair-snapshots": {},
 96	"rewrite":          {},
 97	"stats":            {},
 98	"tag":              {},
 99	"unlock":           {},
100	"version":          {},
101}