externalWorkspaceCreate extends `coder create` to create an external workspace.
()
| 45 | |
| 46 | // externalWorkspaceCreate extends `coder create` to create an external workspace. |
| 47 | func (r *RootCmd) externalWorkspaceCreate() *serpent.Command { |
| 48 | opts := agpl.CreateOptions{ |
| 49 | BeforeCreate: func(ctx context.Context, client *codersdk.Client, _ codersdk.Template, templateVersionID uuid.UUID) error { |
| 50 | version, err := client.TemplateVersion(ctx, templateVersionID) |
| 51 | if err != nil { |
| 52 | return xerrors.Errorf("get template version: %w", err) |
| 53 | } |
| 54 | if !version.HasExternalAgent { |
| 55 | return xerrors.Errorf("template version %q does not have an external agent. Only templates with external agents can be used for external workspace creation", templateVersionID) |
| 56 | } |
| 57 | |
| 58 | return nil |
| 59 | }, |
| 60 | AfterCreate: func(ctx context.Context, inv *serpent.Invocation, client *codersdk.Client, workspace codersdk.Workspace) error { |
| 61 | workspace, err := client.WorkspaceByOwnerAndName(ctx, codersdk.Me, workspace.Name, codersdk.WorkspaceOptions{}) |
| 62 | if err != nil { |
| 63 | return xerrors.Errorf("get workspace by name: %w", err) |
| 64 | } |
| 65 | |
| 66 | externalAgents, err := fetchExternalAgents(inv, client, workspace, workspace.LatestBuild.Resources) |
| 67 | if err != nil { |
| 68 | return xerrors.Errorf("fetch external agents: %w", err) |
| 69 | } |
| 70 | |
| 71 | formatted := formatExternalAgent(workspace.Name, externalAgents) |
| 72 | _, err = fmt.Fprintln(inv.Stdout, formatted) |
| 73 | return err |
| 74 | }, |
| 75 | } |
| 76 | |
| 77 | cmd := r.Create(opts) |
| 78 | cmd.Use = "create [workspace]" |
| 79 | cmd.Short = "Create a new external workspace" |
| 80 | newMiddlewares := []serpent.MiddlewareFunc{} |
| 81 | if cmd.Middleware != nil { |
| 82 | newMiddlewares = append(newMiddlewares, cmd.Middleware) |
| 83 | } |
| 84 | newMiddlewares = append(newMiddlewares, serpent.RequireNArgs(1)) |
| 85 | cmd.Middleware = serpent.Chain(newMiddlewares...) |
| 86 | |
| 87 | for i := range cmd.Options { |
| 88 | if cmd.Options[i].Flag == "template" { |
| 89 | cmd.Options[i].Required = true |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return cmd |
| 94 | } |
| 95 | |
| 96 | // externalWorkspaceAgentInstructions prints the instructions for an external agent. |
| 97 | func (r *RootCmd) externalWorkspaceAgentInstructions() *serpent.Command { |
no test coverage detected