| 242 | } |
| 243 | |
| 244 | func (r *RootCmd) Command(subcommands []*serpent.Command) (*serpent.Command, error) { |
| 245 | if r.clock == nil { |
| 246 | r.clock = quartz.NewReal() |
| 247 | } |
| 248 | |
| 249 | fmtLong := `Coder %s — A tool for provisioning self-hosted development environments with Terraform. |
| 250 | ` |
| 251 | hiddenAgentAuth := &AgentAuth{} |
| 252 | cmd := &serpent.Command{ |
| 253 | Use: "coder [global-flags] <subcommand>", |
| 254 | Long: fmt.Sprintf(fmtLong, buildinfo.Version()) + FormatExamples( |
| 255 | Example{ |
| 256 | Description: "Start a Coder server", |
| 257 | Command: "coder server", |
| 258 | }, |
| 259 | Example{ |
| 260 | Description: "Get started by creating a template from an example", |
| 261 | Command: "coder templates init", |
| 262 | }, |
| 263 | ), |
| 264 | Handler: func(i *serpent.Invocation) error { |
| 265 | if r.versionFlag { |
| 266 | return r.version(defaultVersionInfo).Handler(i) |
| 267 | } |
| 268 | // The GIT_ASKPASS environment variable must point at |
| 269 | // a binary with no arguments. To prevent writing |
| 270 | // cross-platform scripts to invoke the Coder binary |
| 271 | // with a `gitaskpass` subcommand, we override the entrypoint |
| 272 | // to check if the command was invoked. |
| 273 | if gitauth.CheckCommand(i.Args, i.Environ.ToOS()) { |
| 274 | return gitAskpass(hiddenAgentAuth).Handler(i) |
| 275 | } |
| 276 | return i.Command.HelpHandler(i) |
| 277 | }, |
| 278 | } |
| 279 | |
| 280 | cmd.AddSubcommands(subcommands...) |
| 281 | |
| 282 | // Set default help handler for all commands. |
| 283 | cmd.Walk(func(c *serpent.Command) { |
| 284 | if c.HelpHandler == nil { |
| 285 | c.HelpHandler = helpFn() |
| 286 | } |
| 287 | }) |
| 288 | |
| 289 | var merr error |
| 290 | // Add [flags] to usage when appropriate. |
| 291 | cmd.Walk(func(cmd *serpent.Command) { |
| 292 | const flags = "[flags]" |
| 293 | if strings.Contains(cmd.Use, flags) { |
| 294 | merr = errors.Join( |
| 295 | merr, |
| 296 | xerrors.Errorf( |
| 297 | "command %q shouldn't have %q in usage since it's automatically populated", |
| 298 | cmd.FullUsage(), |
| 299 | flags, |
| 300 | ), |
| 301 | ) |