()
| 306 | } |
| 307 | |
| 308 | func (r *RootCmd) openApp() *serpent.Command { |
| 309 | var ( |
| 310 | regionArg string |
| 311 | testOpenError bool |
| 312 | ) |
| 313 | |
| 314 | cmd := &serpent.Command{ |
| 315 | Annotations: workspaceCommand, |
| 316 | Use: "app <workspace> <app slug>", |
| 317 | Short: "Open a workspace application.", |
| 318 | Handler: func(inv *serpent.Invocation) error { |
| 319 | client, err := r.InitClient(inv) |
| 320 | if err != nil { |
| 321 | return err |
| 322 | } |
| 323 | |
| 324 | ctx, cancel := context.WithCancel(inv.Context()) |
| 325 | defer cancel() |
| 326 | |
| 327 | if len(inv.Args) == 0 || len(inv.Args) > 2 { |
| 328 | return inv.Command.HelpHandler(inv) |
| 329 | } |
| 330 | |
| 331 | workspaceName := inv.Args[0] |
| 332 | ws, agt, _, err := GetWorkspaceAndAgent(ctx, inv, client, false, workspaceName) |
| 333 | if err != nil { |
| 334 | var sdkErr *codersdk.Error |
| 335 | if errors.As(err, &sdkErr) && sdkErr.StatusCode() == http.StatusNotFound { |
| 336 | cliui.Errorf(inv.Stderr, "Workspace %q not found!", workspaceName) |
| 337 | return sdkErr |
| 338 | } |
| 339 | cliui.Errorf(inv.Stderr, "Failed to get workspace and agent: %s", err) |
| 340 | return err |
| 341 | } |
| 342 | |
| 343 | allAppSlugs := make([]string, len(agt.Apps)) |
| 344 | for i, app := range agt.Apps { |
| 345 | allAppSlugs[i] = app.Slug |
| 346 | } |
| 347 | slices.Sort(allAppSlugs) |
| 348 | |
| 349 | // If a user doesn't specify an app slug, we'll just list the available |
| 350 | // apps and exit. |
| 351 | if len(inv.Args) == 1 { |
| 352 | cliui.Infof(inv.Stderr, "Available apps in %q: %v", workspaceName, allAppSlugs) |
| 353 | return nil |
| 354 | } |
| 355 | |
| 356 | appSlug := inv.Args[1] |
| 357 | var foundApp codersdk.WorkspaceApp |
| 358 | appIdx := slices.IndexFunc(agt.Apps, func(a codersdk.WorkspaceApp) bool { |
| 359 | return a.Slug == appSlug |
| 360 | }) |
| 361 | if appIdx == -1 { |
| 362 | cliui.Errorf(inv.Stderr, "App %q not found in workspace %q!\nAvailable apps: %v", appSlug, workspaceName, allAppSlugs) |
| 363 | return xerrors.Errorf("app not found") |
| 364 | } |
| 365 | foundApp = agt.Apps[appIdx] |
no test coverage detected