package cli import ( "io" "os" "github.com/spf13/afero" ) var NO_COLOR bool func init() { NO_COLOR = false if _, exists := os.LookupEnv("NO_COLOR"); exists { NO_COLOR = true } } // RunOptions contains all configuration for running the CLI application. type RunOptions struct { Appname string Version string Gitrev string Args []string Stdin io.Reader Stdout io.Writer Stderr io.Writer Fs afero.Fs } // DefaultRunOptions returns RunOptions configured for normal CLI execution. func DefaultRunOptions(appname, version, gitrev string) *RunOptions { return &RunOptions{ Appname: appname, Version: version, Gitrev: gitrev, Args: os.Args, Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr, Fs: afero.NewOsFs(), } } // Run creates and runs the CLI application with default options. func Run(appname, version, gitrev string) int { return RunWithOptions(DefaultRunOptions(appname, version, gitrev)) } // RunWithOptions creates and runs the CLI application with the given options. func RunWithOptions(opts *RunOptions) int { m := &CLIApp{ appname: opts.Appname, version: opts.Version, gitrev: opts.Gitrev, exitCode: 0, Stdin: opts.Stdin, Stdout: opts.Stdout, Stderr: opts.Stderr, Fs: opts.Fs, } m.run(opts.Args) return m.exitCode }