github.com/tcnksm/gcli@v0.2.4-0.20170129033839-7eb950507e5a/skeleton/resource/tmpl/mitchellh_cli/cli.go.tmpl (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/mitchellh/cli"
     8  	"{{ .VCSHost }}/{{.Owner}}/{{.Name}}/command"
     9  )
    10  
    11  func Run(args []string) int {
    12  
    13  	// Meta-option for executables.
    14  	// It defines output color and its stdout/stderr stream.
    15  	meta := &command.Meta{
    16  		Ui: &cli.ColoredUi{
    17  			InfoColor:  cli.UiColorBlue,
    18  			ErrorColor: cli.UiColorRed,
    19  			Ui: &cli.BasicUi{
    20  				Writer:      os.Stdout,
    21  				ErrorWriter: os.Stderr,
    22  				Reader:      os.Stdin,
    23  			},
    24  		}}
    25  
    26  	return RunCustom(args, Commands(meta))
    27  }
    28  
    29  func RunCustom(args []string, commands map[string]cli.CommandFactory) int {
    30  
    31  	// Get the command line args. We shortcut "--version" and "-v" to
    32  	// just show the version.
    33  	for _, arg := range args {
    34  		if arg == "-v" || arg == "-version" || arg == "--version" {
    35  			newArgs := make([]string, len(args)+1)
    36  			newArgs[0] = "version"
    37  			copy(newArgs[1:], args)
    38  			args = newArgs
    39  			break
    40  		}
    41  	}
    42  
    43  	cli := &cli.CLI{
    44  		Args:       args,
    45  		Commands:   commands,
    46  		Version:    Version,
    47  		HelpFunc:   cli.BasicHelpFunc(Name),
    48  		HelpWriter: os.Stdout,
    49  	}
    50  
    51  	exitCode, err := cli.Run()
    52  	if err != nil {
    53  		fmt.Fprintf(os.Stderr, "Failed to execute: %s\n", err.Error())
    54  	}
    55  
    56  	return exitCode
    57  }