| 123456789101112131415161718192021222324252627282930313233343536373839 |
- // Package cli wires cobra subcommands onto the config/provider/runner/templates
- // layers. Each subcommand file (add.go, list.go, use.go, ...) exposes a
- // factory function that receives a shared *appState carrying the effective
- // config path, loaded config, output streams, and a verbose flag.
- //
- // Tests drive this package by calling Execute with an argv slice and inspecting
- // the (exitCode, err) result plus the stdout/stderr io.Writers they pass via
- // ExecuteWithStreams.
- package cli
- import (
- "io"
- "os"
- )
- // Execute runs the CLI against os.Stdin/Stdout/Stderr using argv.
- func Execute(argv []string) (int, error) {
- return ExecuteWithStreams(argv, os.Stdin, os.Stdout, os.Stderr)
- }
- // ExecuteWithStreams is the testable form of Execute; callers supply the
- // standard streams explicitly. The function returns the exit code cc-switch
- // should terminate with. A non-nil error is already printed to stderr by cobra
- // (or by the subcommand) before return, so main() need not print it again.
- func ExecuteWithStreams(argv []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) {
- app := &appState{
- stdin: stdin,
- stdout: stdout,
- stderr: stderr,
- }
- root := newRootCmd(app)
- root.SetArgs(argv)
- root.SetIn(stdin)
- root.SetOut(stdout)
- root.SetErr(stderr)
- err := root.Execute()
- return app.exitCode(err), err
- }
|