registry.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package commands
  2. import (
  3. "cmp"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "maps"
  8. "slices"
  9. "strings"
  10. "sync"
  11. "github.com/anmitsu/go-shlex"
  12. )
  13. // Registry holds a set of commands.
  14. //
  15. // It is safe for concurrent use. Commands may be registered while surfaces are serving
  16. // from the registry: the ssh server clones it per session and the control socket filters
  17. // it per request, and either may overlap a Register.
  18. type Registry struct {
  19. mu sync.RWMutex
  20. commands map[Name]*Command
  21. // full is set on a filtered registry and points at the registry it was filtered
  22. // from, so a command that's filtered can be reported as such.
  23. full *Registry
  24. // reason explains, in the terms an operator cares about, why this registry withholds
  25. // a command they can name. It is supplied by whoever built the filter, because why a
  26. // surface declines a command is that surface's business, not the registry's.
  27. reason string
  28. // DefaultFormat decides how a result is rendered when the command line named no
  29. // format. It sees the command and its Result, and applies to values and refusals
  30. // alike: the registry does not special-case either. The renderer is shared between
  31. // surfaces; what each asks for by default is not, so the surface that owns this
  32. // registry sets it. Nil means text.
  33. DefaultFormat func(c *Command, r Result) Format
  34. }
  35. // defaultFormatFor resolves the surface's default for c's Result.
  36. func (r *Registry) defaultFormatFor(c *Command, res Result) Format {
  37. if r.DefaultFormat == nil {
  38. return FormatText
  39. }
  40. return r.DefaultFormat(c, res)
  41. }
  42. // New returns an empty Registry with only `help` available.
  43. func New() *Registry {
  44. r := &Registry{commands: map[Name]*Command{}}
  45. r.registerHelp()
  46. return r
  47. }
  48. func (r *Registry) registerHelp() {
  49. r.Register(&Command{
  50. Name: Help,
  51. ShortDescription: "prints available commands or help <command> for specific usage info",
  52. Run: func(a any, args []string) (Result, error) {
  53. if len(args) == 0 {
  54. return Value(r.Schema()), nil
  55. }
  56. if c := r.Get(Name(args[0])); c != nil {
  57. return Value(r.schemaFor(c)), nil
  58. }
  59. if err := r.unavailable(Name(args[0])); err != nil {
  60. return Failed("%s", err), nil
  61. }
  62. return Failed("Command not available %s", args[0]), nil
  63. },
  64. Text: renderHelp,
  65. })
  66. }
  67. // renderHelp is help's human rendering: the command list, or one command's usage.
  68. func renderHelp(v any, w io.Writer) error {
  69. switch h := v.(type) {
  70. case []CommandSchema:
  71. return writeCommandList(h, w)
  72. case CommandSchema:
  73. return WriteCommandHelp(h, w)
  74. default:
  75. return fmt.Errorf("expected a command schema, got %T", v)
  76. }
  77. }
  78. func writeCommandList(cmds []CommandSchema, w io.Writer) error {
  79. if _, err := io.WriteString(w, "Available commands:\n"); err != nil {
  80. return err
  81. }
  82. // In the order the schema came, which is the display order; see Registry.All.
  83. lines := make([]string, 0, len(cmds))
  84. for _, c := range cmds {
  85. lines = append(lines, fmt.Sprintf("%s - %s", c.Name, c.Short))
  86. }
  87. _, err := io.WriteString(w, strings.Join(lines, "\n")+"\n\n")
  88. return err
  89. }
  90. // WriteCommandHelp renders one command's usage. It is the single rendering of a
  91. // CommandSchema for a person: the daemon uses it for `help <command>` on both surfaces,
  92. // and a client that already holds the schema uses it for `-h`, so the two cannot drift.
  93. func WriteCommandHelp(c CommandSchema, w io.Writer) error {
  94. if _, err := fmt.Fprintf(w, "%s - %s\n", c.Name, c.Short); err != nil {
  95. return err
  96. }
  97. if c.Help != "" {
  98. if _, err := fmt.Fprintf(w, " %s\n", c.Help); err != nil {
  99. return err
  100. }
  101. }
  102. // Same layout as flag.PrintDefaults, which is what this used to be, including its
  103. // habit of leaving out a default that is the zero value.
  104. for _, f := range c.Flags {
  105. kind := ""
  106. if !f.Bool {
  107. kind = " string"
  108. }
  109. def := ""
  110. if f.Default != "" && f.Default != "false" {
  111. def = fmt.Sprintf(" (default %s)", f.Default)
  112. }
  113. if _, err := fmt.Fprintf(w, " -%s%s\n \t%s%s\n", f.Name, kind, f.Usage, def); err != nil {
  114. return err
  115. }
  116. }
  117. return nil
  118. }
  119. // Register adds a command that can be run by a user.
  120. //
  121. // It panics if the command declares a reserved flag. That is a programming error, and
  122. // surfacing it here, at startup, is better than at the moment an operator first types
  123. // the command.
  124. func (r *Registry) Register(c *Command) {
  125. if c.Run == nil {
  126. panic(fmt.Sprintf("commands: %s has no Run", c.Name))
  127. }
  128. if c.Flags != nil {
  129. if fl, _ := c.Flags(); fl != nil {
  130. for _, name := range reservedFlags {
  131. if fl.Lookup(name) != nil {
  132. panic(fmt.Sprintf("commands: %s declares reserved flag -%s", c.Name, name))
  133. }
  134. }
  135. }
  136. }
  137. r.insert(c)
  138. }
  139. // insert adds c without the reserved flag check, for commands that have already passed
  140. // it on another registry.
  141. func (r *Registry) insert(c *Command) {
  142. r.mu.Lock()
  143. defer r.mu.Unlock()
  144. r.commands[c.Name] = c
  145. }
  146. // Get returns the command registered under name, or nil if there is no such command.
  147. func (r *Registry) Get(name Name) *Command {
  148. r.mu.RLock()
  149. defer r.mu.RUnlock()
  150. return r.commands[name]
  151. }
  152. // Match returns the names of every command starting with prefix, sorted, as strings for
  153. // a prompt to complete and print.
  154. func (r *Registry) Match(prefix string) []string {
  155. cmds := make([]string, 0)
  156. r.mu.RLock()
  157. for n := range r.commands {
  158. if strings.HasPrefix(string(n), prefix) {
  159. cmds = append(cmds, string(n))
  160. }
  161. }
  162. r.mu.RUnlock()
  163. slices.Sort(cmds)
  164. return cmds
  165. }
  166. // All returns every registered command sorted by name, which is the order help lists
  167. // them and the schema describes them.
  168. func (r *Registry) All() []*Command {
  169. r.mu.RLock()
  170. defer r.mu.RUnlock()
  171. cmds := slices.Collect(maps.Values(r.commands))
  172. slices.SortFunc(cmds, func(a, b *Command) int { return cmp.Compare(a.Name, b.Name) })
  173. return cmds
  174. }
  175. // Clone returns a copy holding the same commands.
  176. //
  177. // help is re-registered on the copy rather than carried over, because the source's help
  178. // is bound to the source and would list its commands, not the copy's, once a caller
  179. // registers something on the copy.
  180. func (r *Registry) Clone() *Registry {
  181. r.mu.RLock()
  182. out := &Registry{
  183. commands: maps.Clone(r.commands),
  184. full: r.full,
  185. reason: r.reason,
  186. DefaultFormat: r.DefaultFormat,
  187. }
  188. r.mu.RUnlock()
  189. out.registerHelp()
  190. return out
  191. }
  192. // Filter returns a filtered copy of the registry.
  193. //
  194. // Keep should return true for commands that should be included in the copy.
  195. //
  196. // reason says, to an operator who names a command the copy left out, why this surface
  197. // does not serve it; "" leaves the registry to say only that it does not.
  198. func (r *Registry) Filter(keep func(*Command) bool, reason string) *Registry {
  199. out := &Registry{commands: map[Name]*Command{}, full: r, reason: reason, DefaultFormat: r.DefaultFormat}
  200. for _, cmd := range r.All() {
  201. if keep(cmd) {
  202. out.insert(cmd)
  203. }
  204. }
  205. // Bind help to the filtered set so it only advertises what this surface serves.
  206. out.registerHelp()
  207. return out
  208. }
  209. // Dispatch parses a command line and runs it, reporting problems to w. The error is for
  210. // the caller to log, not to show; see RunFunc and Failure.
  211. func (r *Registry) Dispatch(line string, w Output) error {
  212. // A line shlex cannot split, an unbalanced quote. Reported on Err so the output
  213. // stream stays parseable.
  214. args, err := shlex.Split(line, true)
  215. if err != nil {
  216. msg := "could not parse the command line: " + err.Error()
  217. if _, err := fmt.Fprintln(w.Err, msg); err != nil {
  218. return err
  219. }
  220. return &Failure{Msg: msg}
  221. }
  222. return r.DispatchArgs(args, w)
  223. }
  224. // DispatchArgs runs an already tokenized command line. The error is as for Dispatch.
  225. //
  226. // An empty line and an unknown command both end up running `help`, the first with the
  227. // arguments help itself would take; `<command> -h` is answered by the command's own
  228. // FlagSet, with the same rendering, in execLine.
  229. func (r *Registry) DispatchArgs(args []string, w Output) error {
  230. if len(args) == 0 {
  231. args = []string{"help"}
  232. }
  233. c := r.Get(Name(args[0]))
  234. if c == nil {
  235. if err := r.unavailable(Name(args[0])); err != nil {
  236. // Saying why, rather than "did not understand", keeps the rule legible and
  237. // points at a surface that will actually run the command.
  238. _, _ = fmt.Fprintln(w.Err, err)
  239. return err
  240. }
  241. // Only the first word was looked up, so only it is named.
  242. msg := fmt.Sprintf("did not understand: %s", args[0])
  243. if _, err := fmt.Fprintln(w.Out, msg); err != nil {
  244. return err
  245. }
  246. if err := r.DispatchArgs([]string{"help"}, w); err != nil {
  247. return err
  248. }
  249. return &Failure{Msg: msg}
  250. }
  251. return r.execLine(c, args[1:], w)
  252. }
  253. // unavailable explains why name cannot be run here when this registry was filtered from
  254. // one that holds it: the command exists, and this surface withholds it, for the reason
  255. // whoever built the filter gave. It is nil when name is simply unknown, so the two cases
  256. // read differently to an operator.
  257. func (r *Registry) unavailable(name Name) error {
  258. if r.full == nil {
  259. return nil
  260. }
  261. c := r.full.Get(name)
  262. if c == nil {
  263. return nil
  264. }
  265. reason := r.reason
  266. if reason == "" {
  267. reason = "this surface does not serve it"
  268. }
  269. return fmt.Errorf("%s is not available on this surface: %s", c.Name, reason)
  270. }
  271. // Resolve returns the command registered under name, or says why there is none. A
  272. // command this surface withholds is reported with the filter's reason, and one that
  273. // does not exist at all as unknown, so a client that misspells and one that asks for a
  274. // real command it cannot have here are told different things. It is the edge where a
  275. // name from the wire becomes a Command for a Request.
  276. func (r *Registry) Resolve(name Name) (*Command, error) {
  277. if c := r.Get(name); c != nil {
  278. return c, nil
  279. }
  280. if err := r.unavailable(name); err != nil {
  281. return nil, err
  282. }
  283. return nil, fmt.Errorf("unknown command %q", name)
  284. }
  285. // setParams sets each named parameter on fl, in the order of names, and stops at the
  286. // first that is unknown or does not parse. own says whether the command declared flags
  287. // of its own, which decides between "takes no parameters" and "has no parameter".
  288. //
  289. // The presentation flags are on fl, since commandFlags put them there, but they are not
  290. // parameters: a request has one place to choose its rendering, the envelope's format
  291. // field, and a client that sends them here is told where that is rather than having a
  292. // second, weaker way to say the same thing.
  293. func setParams(fl *flag.FlagSet, name string, names []string, params map[string]string, own bool) error {
  294. for _, k := range names {
  295. if isPresentationFlag(k) {
  296. return fmt.Errorf("%s is a command line flag, not a parameter; choose the rendering with the request's format field", k)
  297. }
  298. f := fl.Lookup(k)
  299. if f == nil {
  300. if !own {
  301. return fmt.Errorf("%s takes no parameters", name)
  302. }
  303. return fmt.Errorf("%s has no parameter %q", name, k)
  304. }
  305. v := params[k]
  306. if err := fl.Set(k, v); err != nil {
  307. if IsBoolFlag(f) {
  308. // The flag package's own words for this are "parse error".
  309. return fmt.Errorf("%s: expected a boolean, got %q", k, v)
  310. }
  311. return fmt.Errorf("%s: %w", k, err)
  312. }
  313. }
  314. return nil
  315. }
  316. // Schema describes every command in the registry, so a client can offer them without
  317. // having been built against this daemon.
  318. func (r *Registry) Schema() []CommandSchema {
  319. all := r.All()
  320. out := make([]CommandSchema, 0, len(all))
  321. for _, c := range all {
  322. out = append(out, r.schemaFor(c))
  323. }
  324. return out
  325. }
  326. // schemaFor describes one command. It is the one derivation, for the whole schema and
  327. // for `help <command>` alike.
  328. func (r *Registry) schemaFor(c *Command) CommandSchema {
  329. fl, _, _ := commandFlags(c)
  330. return schemaOf(c, fl)
  331. }
  332. // schemaOf describes c as driven through fl, the FlagSet commandFlags built for it. The
  333. // presentation flags are included because a command line client parses its line
  334. // against this and help renders from it; they are listed after the command's own,
  335. // which is where readers expect them. Invoke refuses them as parameters.
  336. func schemaOf(c *Command, fl *flag.FlagSet) CommandSchema {
  337. cs := CommandSchema{Name: c.Name, Short: c.ShortDescription, Help: c.Help}
  338. var own, shared []FlagSchema
  339. fl.VisitAll(func(f *flag.Flag) {
  340. fs := FlagSchema{
  341. Name: f.Name,
  342. Bool: IsBoolFlag(f),
  343. Default: f.DefValue,
  344. Usage: f.Usage,
  345. }
  346. if isPresentationFlag(f.Name) {
  347. shared = append(shared, fs)
  348. return
  349. }
  350. own = append(own, fs)
  351. })
  352. cs.Flags = append(own, shared...)
  353. return cs
  354. }
  355. // CommandSchema describes one command well enough for a client to offer it.
  356. type CommandSchema struct {
  357. Name Name `json:"name"`
  358. Short string `json:"short,omitempty"`
  359. Help string `json:"help,omitempty"`
  360. Flags []FlagSchema `json:"flags,omitempty"`
  361. }
  362. // FlagSchema describes one of a command's named parameters.
  363. type FlagSchema struct {
  364. Name string `json:"name"`
  365. // Bool reports a parameter that takes no value, which is what tells a command line
  366. // client whether `-pretty` swallows the word after it.
  367. Bool bool `json:"bool,omitempty"`
  368. Default string `json:"default,omitempty"`
  369. Usage string `json:"usage,omitempty"`
  370. }