feat(cli): separate python commands into own namespace
This commit is contained in:
parent
b6d8cc534d
commit
d1200b81d1
2 changed files with 65 additions and 32 deletions
59
cli/cli.go
59
cli/cli.go
|
@ -25,27 +25,40 @@ type Command struct {
|
||||||
// Must be initialized with commands via AddCommand before running
|
// Must be initialized with commands via AddCommand before running
|
||||||
// with Run.
|
// with Run.
|
||||||
type CLI struct {
|
type CLI struct {
|
||||||
// Commands in enumeration order.
|
Namespaces map[string]Namespace
|
||||||
|
Metadata map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Namespace struct {
|
||||||
|
Label string
|
||||||
|
Commands map[string]Command
|
||||||
OrderedCommands []string
|
OrderedCommands []string
|
||||||
// Command metadata entries.
|
}
|
||||||
Commands map[string]Command
|
|
||||||
Metadata map[string]string
|
func (c *CLI) AddNamespace(namespace Namespace) *CLI {
|
||||||
|
if c.Namespaces == nil {
|
||||||
|
c.Namespaces = map[string]Namespace{}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Namespaces[namespace.Label] = namespace
|
||||||
|
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Registers a command.
|
// Registers a command.
|
||||||
// This specifies a label that is used to route the user input to
|
// This specifies a label that is used to route the user input to
|
||||||
// the right command, a handler that is called when the label is used,
|
// the right command, a handler that is called when the label is used,
|
||||||
// and usage/description details that get included in autogenerated help messaging.
|
// and usage/description details that get included in autogenerated help messaging.
|
||||||
func (c CLI) AddCommand(label string, handler func([]string, Flags, state.State) error, usage string, description string) CLI {
|
func (n *Namespace) AddCommand(label string, handler func([]string, Flags, state.State) error, usage string, description string) *Namespace {
|
||||||
if c.Commands == nil {
|
if n.Commands == nil {
|
||||||
c.Commands = map[string]Command{}
|
n.Commands = map[string]Command{}
|
||||||
c.OrderedCommands = []string{}
|
n.OrderedCommands = []string{}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.OrderedCommands = append(c.OrderedCommands, label)
|
n.OrderedCommands = append(n.OrderedCommands, label)
|
||||||
c.Commands[label] = Command{Label: label, Handler: handler, Usage: usage, Description: description}
|
n.Commands[label] = Command{Label: label, Handler: handler, Usage: usage, Description: description}
|
||||||
|
|
||||||
return c
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// Executes one of the registered commands if any match the provided
|
// Executes one of the registered commands if any match the provided
|
||||||
|
@ -56,24 +69,36 @@ func (c CLI) Run(args []string, currentState state.State) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
command := args[0]
|
action := args[0]
|
||||||
|
|
||||||
if command == "help" {
|
if action == "help" {
|
||||||
c.Help()
|
c.Help()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := collectFlags(args)
|
flags := collectFlags(args)
|
||||||
return c.Commands[command].Handler(args, flags, currentState)
|
|
||||||
|
namespace, isNamespace := c.Namespaces[action]
|
||||||
|
|
||||||
|
if isNamespace {
|
||||||
|
action = args[1]
|
||||||
|
return namespace.Commands[action].Handler(args[1:], flags, currentState)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootNamespace := c.Namespaces[""]
|
||||||
|
|
||||||
|
return rootNamespace.Commands[action].Handler(args, flags, currentState)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prints autogenerated help documentation specifying command usage
|
// Prints autogenerated help documentation specifying command usage
|
||||||
// and descriptions based on registered commands (see: AddCommand).
|
// and descriptions based on registered commands (see: AddCommand).
|
||||||
func (c CLI) Help() {
|
func (c CLI) Help() {
|
||||||
logger.InfoLogger.Printf("v: A simple version manager. (v%s)\n---", c.Metadata["Version"])
|
logger.InfoLogger.Printf("v: A simple version manager. (v%s)\n---", c.Metadata["Version"])
|
||||||
for _, commandLabel := range c.OrderedCommands {
|
for _, namespace := range c.Namespaces {
|
||||||
command := c.Commands[commandLabel]
|
for _, commandLabel := range namespace.OrderedCommands {
|
||||||
logger.InfoLogger.Printf("\033[1m%-30s\033[0m%s\n", command.Usage, command.Description)
|
command := namespace.Commands[commandLabel]
|
||||||
|
logger.InfoLogger.Printf("\033[1m%-30s\033[0m%s\n", command.Usage, command.Description)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
38
v.go
38
v.go
|
@ -18,27 +18,35 @@ func main() {
|
||||||
args := os.Args[1:]
|
args := os.Args[1:]
|
||||||
currentState := state.ReadState()
|
currentState := state.ReadState()
|
||||||
|
|
||||||
|
root := cli.Namespace{Label: ""}
|
||||||
|
root.AddCommand(
|
||||||
|
"init", Initialize, "v init", "Initializes the v state.",
|
||||||
|
)
|
||||||
|
|
||||||
|
pythonCommands := cli.Namespace{Label: "python"}
|
||||||
|
pythonCommands.AddCommand(
|
||||||
|
"install", python.InstallPython, "v python install <version>", "Downloads, builds and installs a new version of Python.",
|
||||||
|
).AddCommand(
|
||||||
|
"uninstall", python.UninstallPython, "v python uninstall <version>", "Uninstalls the given Python version.",
|
||||||
|
).AddCommand(
|
||||||
|
"use", python.Use, "v python use <version>", "Selects which Python version to use.",
|
||||||
|
).AddCommand(
|
||||||
|
"ls", python.ListVersions, "v python ls", "Lists the installed Python versions.",
|
||||||
|
).AddCommand(
|
||||||
|
"version", python.CurrentVersion, "v python version", "Prints the current version and its source.",
|
||||||
|
).AddCommand(
|
||||||
|
"which", python.Which, "v python which", "Prints the path to the current Python version.",
|
||||||
|
)
|
||||||
|
|
||||||
cli := cli.CLI{
|
cli := cli.CLI{
|
||||||
Metadata: map[string]string{
|
Metadata: map[string]string{
|
||||||
"Version": Version,
|
"Version": Version,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := cli.AddCommand(
|
cli.AddNamespace(root).AddNamespace(pythonCommands)
|
||||||
"install", python.InstallPython, "v install <version>", "Downloads, builds and installs a new version of Python.",
|
|
||||||
).AddCommand(
|
err := cli.Run(args, currentState)
|
||||||
"uninstall", python.UninstallPython, "v uninstall <version>", "Uninstalls the given Python version.",
|
|
||||||
).AddCommand(
|
|
||||||
"use", python.Use, "v use <version>", "Selects which Python version to use.",
|
|
||||||
).AddCommand(
|
|
||||||
"ls", python.ListVersions, "v ls", "Lists the installed Python versions.",
|
|
||||||
).AddCommand(
|
|
||||||
"version", python.CurrentVersion, "v version", "Prints the current version and its source.",
|
|
||||||
).AddCommand(
|
|
||||||
"which", python.Which, "v which", "Prints the path to the current Python version.",
|
|
||||||
).AddCommand(
|
|
||||||
"init", Initialize, "v init", "Initializes the v state.",
|
|
||||||
).Run(args, currentState)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
|
Loading…
Reference in a new issue