github.com/tcnksm/gcli@v0.2.4-0.20170129033839-7eb950507e5a/cli.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 7 "github.com/mitchellh/cli" 8 "github.com/tcnksm/gcli/command" 9 ) 10 11 // Run execute RunCustom() with color and output to Stdout/Stderr. 12 // It returns exit code. 13 func Run(args []string) int { 14 15 // Meta-option for executables. 16 // It defines output color and its stdout/stderr stream. 17 meta := &command.Meta{ 18 UI: &cli.ColoredUi{ 19 InfoColor: cli.UiColorBlue, 20 ErrorColor: cli.UiColorRed, 21 Ui: &cli.BasicUi{ 22 Writer: os.Stdout, 23 ErrorWriter: os.Stderr, 24 Reader: os.Stdin, 25 }, 26 }} 27 28 return RunCustom(args, Commands(meta)) 29 } 30 31 // RunCustom execute mitchellh/cli and return its exit code. 32 func RunCustom(args []string, commands map[string]cli.CommandFactory) int { 33 34 for _, arg := range args { 35 36 // If the following options are provided, 37 // then execute gcli version command 38 if arg == "-v" || arg == "-version" || arg == "--version" { 39 args[1] = "version" 40 break 41 } 42 43 // Generating godoc (doc.go). This is only for gcli developper. 44 if arg == "-godoc" { 45 return runGodoc(commands) 46 47 } 48 } 49 50 cli := &cli.CLI{ 51 Args: args[1:], 52 Commands: commands, 53 Version: Version, 54 HelpFunc: cli.BasicHelpFunc(Name), 55 HelpWriter: os.Stdout, 56 } 57 58 exitCode, err := cli.Run() 59 if err != nil { 60 fmt.Fprintf(os.Stderr, "Failed to execute: %s\n", err.Error()) 61 } 62 63 return exitCode 64 }