package main import ( "errors" "fmt" "os" "strings" "sectorinf.com/emilis/hlctl/cmdlets" "sectorinf.com/emilis/hlctl/cmdlets/group" initcmd "sectorinf.com/emilis/hlctl/cmdlets/init" "sectorinf.com/emilis/hlctl/cmdlets/notify" "sectorinf.com/emilis/hlctl/cmdlets/save" "sectorinf.com/emilis/hlctl/ctllog" ) var log = ctllog.Logger{}.New("hlctl", ctllog.Green) var cmds = []cmdlets.Commandlet{ initcmd.Command, group.Command, notify.Command, save.Command, } var ( ErrNotFound = errors.New("not found") ) func getCmdlet(name string) (cmdlets.Commandlet, error) { for _, c := range cmds { if c.Name() == name { return c, nil } } return nil, fmt.Errorf("%s: %w", name, ErrNotFound) } func help() { commands := make([]string, len(cmds)) for i, c := range cmds { commands[i] = c.Name() } fmt.Fprint(os.Stderr, "USAGE: hlctl [command] [arguments]\n") fmt.Fprintf(os.Stderr, "\nAvailable commands: %v\n", commands) fmt.Fprint(os.Stderr, "\nSee hlctl help [command] for details\n") } func main() { // omit the program name args := os.Args[1:] if len(args) == 0 { help() os.Exit(1) } command := strings.ToLower(args[0]) if command == "help" { if len(args) == 1 { help() return } name := args[1] c, err := getCmdlet(name) if err != nil { help() os.Exit(1) } fmt.Println(c.Usage()) return } c, err := getCmdlet(command) if err != nil { help() os.Exit(1) } if err := c.Invoke(args[1:]...); err != nil { log.Fatalf(" %s > %s", c.Name(), err.Error()) } }