2019-06-17 09:48:05 +00:00
|
|
|
// Package cli provides tools to create commands that support advanced configuration features,
|
|
|
|
// sub-commands, and allowing configuration from command-line flags, configuration files, and environment variables.
|
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-02 16:34:06 +00:00
|
|
|
"io"
|
2019-06-17 09:48:05 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Command structure contains program/command information (command name and description).
|
|
|
|
type Command struct {
|
2019-12-02 16:34:06 +00:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Configuration interface{}
|
|
|
|
Resources []ResourceLoader
|
|
|
|
Run func([]string) error
|
|
|
|
CustomHelpFunc func(io.Writer, *Command) error
|
|
|
|
Hidden bool
|
2019-06-18 10:10:06 +00:00
|
|
|
// AllowArg if not set, disallows any argument that is not a known command or a sub-command.
|
|
|
|
AllowArg bool
|
|
|
|
subCommands []*Command
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddCommand Adds a sub command.
|
|
|
|
func (c *Command) AddCommand(cmd *Command) error {
|
|
|
|
if c == nil || cmd == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Name == cmd.Name {
|
|
|
|
return fmt.Errorf("child command cannot have the same name as their parent: %s", cmd.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.subCommands = append(c.subCommands, cmd)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:34:06 +00:00
|
|
|
// PrintHelp calls the custom help function of the command if it's set.
|
|
|
|
// Otherwise, it calls the default help function.
|
|
|
|
func (c *Command) PrintHelp(w io.Writer) error {
|
|
|
|
if c.CustomHelpFunc != nil {
|
|
|
|
return c.CustomHelpFunc(w, c)
|
|
|
|
}
|
|
|
|
return PrintHelp(w, c)
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
// Execute Executes a command.
|
|
|
|
func Execute(cmd *Command) error {
|
|
|
|
return execute(cmd, os.Args, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func execute(cmd *Command, args []string, root bool) error {
|
2019-06-18 10:10:06 +00:00
|
|
|
// Calls command without args.
|
2019-06-17 09:48:05 +00:00
|
|
|
if len(args) == 1 {
|
2019-06-18 10:10:06 +00:00
|
|
|
if err := run(cmd, args[1:]); err != nil {
|
2019-06-17 09:48:05 +00:00
|
|
|
return fmt.Errorf("command %s error: %v", args[0], err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:10:06 +00:00
|
|
|
// Special case: if the command is the top level one,
|
|
|
|
// and the first arg (`args[1]`) is not the command name or a known sub-command,
|
|
|
|
// then we run the top level command itself.
|
2019-06-17 09:48:05 +00:00
|
|
|
if root && cmd.Name != args[1] && !contains(cmd.subCommands, args[1]) {
|
|
|
|
if err := run(cmd, args[1:]); err != nil {
|
|
|
|
return fmt.Errorf("command %s error: %v", filepath.Base(args[0]), err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:10:06 +00:00
|
|
|
// Calls command by its name.
|
2019-06-17 09:48:05 +00:00
|
|
|
if len(args) >= 2 && cmd.Name == args[1] {
|
2019-11-27 09:32:06 +00:00
|
|
|
if len(args) < 3 || !contains(cmd.subCommands, args[2]) {
|
|
|
|
if err := run(cmd, args[2:]); err != nil {
|
|
|
|
return fmt.Errorf("command %s error: %v", cmd.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:10:06 +00:00
|
|
|
// No sub-command, calls the current command.
|
2019-06-17 09:48:05 +00:00
|
|
|
if len(cmd.subCommands) == 0 {
|
|
|
|
if err := run(cmd, args[1:]); err != nil {
|
|
|
|
return fmt.Errorf("command %s error: %v", cmd.Name, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 10:10:06 +00:00
|
|
|
// Trying to find the sub-command.
|
2019-06-17 09:48:05 +00:00
|
|
|
for _, subCmd := range cmd.subCommands {
|
|
|
|
if len(args) >= 2 && subCmd.Name == args[1] {
|
2019-11-27 09:32:06 +00:00
|
|
|
return execute(subCmd, args, false)
|
|
|
|
}
|
|
|
|
if len(args) >= 3 && subCmd.Name == args[2] {
|
2019-06-17 09:48:05 +00:00
|
|
|
return execute(subCmd, args[1:], false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("command not found: %v", args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(cmd *Command, args []string) error {
|
2019-06-19 12:08:05 +00:00
|
|
|
if len(args) > 0 && !isFlag(args[0]) && !cmd.AllowArg {
|
2019-12-02 16:34:06 +00:00
|
|
|
_ = cmd.PrintHelp(os.Stdout)
|
2019-06-19 12:08:05 +00:00
|
|
|
return fmt.Errorf("command not found: %s", args[0])
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if isHelp(args) {
|
2019-12-02 16:34:06 +00:00
|
|
|
return cmd.PrintHelp(os.Stdout)
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cmd.Run == nil {
|
2019-12-02 16:34:06 +00:00
|
|
|
_ = cmd.PrintHelp(os.Stdout)
|
2019-06-18 10:10:06 +00:00
|
|
|
return fmt.Errorf("command %s is not runnable", cmd.Name)
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if cmd.Configuration == nil {
|
|
|
|
return cmd.Run(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range cmd.Resources {
|
|
|
|
done, err := resource.Load(args, cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if done {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmd.Run(args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(cmds []*Command, name string) bool {
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
if cmd.Name == name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2019-06-18 10:10:06 +00:00
|
|
|
|
|
|
|
func isFlag(arg string) bool {
|
2019-06-21 07:40:04 +00:00
|
|
|
return len(arg) > 0 && arg[0] == '-'
|
2019-06-18 10:10:06 +00:00
|
|
|
}
|