RunWithSubcommands runs the root command with the given subcommands. It is abstracted to enable the Enterprise code to add commands.
(subcommands []*serpent.Command)
| 196 | // RunWithSubcommands runs the root command with the given subcommands. |
| 197 | // It is abstracted to enable the Enterprise code to add commands. |
| 198 | func (r *RootCmd) RunWithSubcommands(subcommands []*serpent.Command) { |
| 199 | // This configuration is not available as a standard option because we |
| 200 | // want to trace the entire program, including Options parsing. |
| 201 | goTraceFilePath, ok := os.LookupEnv("CODER_GO_TRACE") |
| 202 | if ok { |
| 203 | traceFile, err := os.OpenFile(goTraceFilePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) |
| 204 | if err != nil { |
| 205 | panic(fmt.Sprintf("failed to open trace file: %v", err)) |
| 206 | } |
| 207 | defer traceFile.Close() |
| 208 | |
| 209 | if err := trace.Start(traceFile); err != nil { |
| 210 | panic(fmt.Sprintf("failed to start trace: %v", err)) |
| 211 | } |
| 212 | defer trace.Stop() |
| 213 | } |
| 214 | |
| 215 | cmd, err := r.Command(subcommands) |
| 216 | if err != nil { |
| 217 | panic(err) |
| 218 | } |
| 219 | err = cmd.Invoke().WithOS().Run() |
| 220 | if err != nil { |
| 221 | code := 1 |
| 222 | var exitErr *exitError |
| 223 | if errors.As(err, &exitErr) { |
| 224 | code = exitErr.code |
| 225 | err = exitErr.err |
| 226 | } |
| 227 | if errors.Is(err, cliui.ErrCanceled) { |
| 228 | //nolint:revive,gocritic |
| 229 | os.Exit(code) |
| 230 | } |
| 231 | if errors.Is(err, ErrSilent) { |
| 232 | //nolint:revive,gocritic |
| 233 | os.Exit(code) |
| 234 | } |
| 235 | f := PrettyErrorFormatter{w: os.Stderr, verbose: r.verbose} |
| 236 | if err != nil { |
| 237 | f.Format(err) |
| 238 | } |
| 239 | //nolint:revive,gocritic |
| 240 | os.Exit(code) |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, error) { |
| 245 | if r.clock == nil { |