|
| 1 | +// Package cli provides the command dispatch framework for tfenv. |
| 2 | +// |
| 3 | +// Subcommands are registered in a map of name to handler function. |
| 4 | +// Other packages plug in by adding entries to the registry. |
| 5 | +package cli |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "sort" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// Handler is the function signature for a subcommand handler. |
| 15 | +// It receives the remaining command-line arguments and returns an exit code. |
| 16 | +type Handler func(args []string) int |
| 17 | + |
| 18 | +// command holds metadata for a registered subcommand. |
| 19 | +type command struct { |
| 20 | + handler Handler |
| 21 | + description string |
| 22 | +} |
| 23 | + |
| 24 | +// registry maps subcommand names to their handlers and descriptions. |
| 25 | +var registry = map[string]command{} |
| 26 | + |
| 27 | +// Register adds a subcommand to the dispatch registry. |
| 28 | +func Register(name string, description string, handler Handler) { |
| 29 | + registry[name] = command{ |
| 30 | + handler: handler, |
| 31 | + description: description, |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// Run dispatches to the appropriate subcommand based on args. |
| 36 | +// It returns an exit code suitable for os.Exit. |
| 37 | +func Run(version string, args []string) int { |
| 38 | + if len(args) == 0 { |
| 39 | + printUsage(version) |
| 40 | + return 0 |
| 41 | + } |
| 42 | + |
| 43 | + subcmd := args[0] |
| 44 | + |
| 45 | + // Handle --version and version as special cases. |
| 46 | + if subcmd == "--version" || subcmd == "version" { |
| 47 | + fmt.Fprintf(os.Stdout, "tfenv %s\n", version) |
| 48 | + return 0 |
| 49 | + } |
| 50 | + |
| 51 | + // Handle help as a special case. |
| 52 | + if subcmd == "help" || subcmd == "--help" || subcmd == "-h" { |
| 53 | + printUsage(version) |
| 54 | + return 0 |
| 55 | + } |
| 56 | + |
| 57 | + // Look up the subcommand in the registry. |
| 58 | + cmd, ok := registry[subcmd] |
| 59 | + if !ok { |
| 60 | + fmt.Fprintf(os.Stderr, "tfenv: unknown command %q\n", subcmd) |
| 61 | + fmt.Fprintf(os.Stderr, "Run 'tfenv help' for usage.\n") |
| 62 | + return 1 |
| 63 | + } |
| 64 | + |
| 65 | + return cmd.handler(args[1:]) |
| 66 | +} |
| 67 | + |
| 68 | +// printUsage prints the help text listing all registered subcommands. |
| 69 | +func printUsage(version string) { |
| 70 | + fmt.Fprintf(os.Stdout, "tfenv %s\n\n", version) |
| 71 | + fmt.Fprintf(os.Stdout, "Usage: tfenv <command> [args]\n\n") |
| 72 | + |
| 73 | + // Always include the built-in commands. |
| 74 | + builtins := []struct { |
| 75 | + name string |
| 76 | + desc string |
| 77 | + }{ |
| 78 | + {"help", "Show this help output"}, |
| 79 | + {"version", "Print tfenv version"}, |
| 80 | + } |
| 81 | + |
| 82 | + // Collect registered commands and sort them. |
| 83 | + var names []string |
| 84 | + for name := range registry { |
| 85 | + names = append(names, name) |
| 86 | + } |
| 87 | + sort.Strings(names) |
| 88 | + |
| 89 | + fmt.Fprintf(os.Stdout, "Commands:\n") |
| 90 | + |
| 91 | + // Print built-in commands first. |
| 92 | + for _, b := range builtins { |
| 93 | + fmt.Fprintf(os.Stdout, " %-16s %s\n", b.name, b.desc) |
| 94 | + } |
| 95 | + |
| 96 | + // Print registered commands. |
| 97 | + for _, name := range names { |
| 98 | + cmd := registry[name] |
| 99 | + fmt.Fprintf(os.Stdout, " %-16s %s\n", name, cmd.description) |
| 100 | + } |
| 101 | + |
| 102 | + // Build the full list for "Available commands" summary. |
| 103 | + var all []string |
| 104 | + for _, b := range builtins { |
| 105 | + all = append(all, b.name) |
| 106 | + } |
| 107 | + all = append(all, names...) |
| 108 | + sort.Strings(all) |
| 109 | + |
| 110 | + fmt.Fprintf(os.Stdout, "\nAvailable commands: %s\n", strings.Join(all, ", ")) |
| 111 | +} |
0 commit comments