- Notifications
You must be signed in to change notification settings - Fork1k
chore(cli): re-order CLI create command#19658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
3071ef8
319eee2
55080ff
b699de7
4889c47
5377bb3
0e75596
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2,6 +2,7 @@ package cli | ||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"github.com/google/uuid" | ||
@@ -20,43 +21,49 @@ func (r *RootCmd) taskCreate() *serpent.Command { | ||
templateName string | ||
templateVersionName string | ||
presetName string | ||
stdin bool | ||
) | ||
cmd := &serpent.Command{ | ||
Use: "create [input]", | ||
Short: "Create an experimental task", | ||
Middleware: serpent.Chain( | ||
serpent.RequireRangeArgs(0, 1), | ||
r.InitClient(client), | ||
), | ||
Options: serpent.OptionSet{ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Does it mean we can start a task without an input? i.e., an empty prompt? Should we allow that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think this may end up happening in a prebuild scenario? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Maybe, yes. Prebuilds are tricky. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think we should start tasks without input necessarily, but who's to say there isn't a default input that's only controlled by e.g. template parameters. This was one motivation that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Have added a check to disallow empty task input | ||
Name: "template", | ||
Flag: "template", | ||
Env: "CODER_TASK_TEMPLATE_NAME", | ||
Value: serpent.StringOf(&templateName), | ||
}, | ||
{ | ||
Name: "template-version", | ||
Flag: "template-version", | ||
Env: "CODER_TASK_TEMPLATE_VERSION", | ||
Value: serpent.StringOf(&templateVersionName), | ||
}, | ||
{ | ||
Name: "preset", | ||
Flag: "preset", | ||
Env: "CODER_TASK_PRESET_NAME", | ||
Value: serpent.StringOf(&presetName), | ||
Default: PresetNone, | ||
}, | ||
{ | ||
Name: "stdin", | ||
Flag: "stdin", | ||
Description: "Reads from stdin for the task input.", | ||
Value: serpent.BoolOf(&stdin), | ||
}, | ||
}, | ||
Handler: func(inv *serpent.Invocation) error { | ||
var ( | ||
ctx = inv.Context() | ||
expClient = codersdk.NewExperimentalClient(client) | ||
taskInput string | ||
templateVersionID uuid.UUID | ||
templateVersionPresetID uuid.UUID | ||
) | ||
@@ -66,22 +73,68 @@ func (r *RootCmd) taskCreate() *serpent.Command { | ||
return xerrors.Errorf("get current organization: %w", err) | ||
} | ||
if stdin { | ||
bytes, err := io.ReadAll(inv.Stdin) | ||
if err != nil { | ||
return xerrors.Errorf("reading stdin: %w", err) | ||
} | ||
taskInput = string(bytes) | ||
} else { | ||
if len(inv.Args) != 1 { | ||
return xerrors.Errorf("expected an input for task") | ||
} | ||
taskInput = inv.Args[0] | ||
} | ||
iftaskInput == "" { | ||
return xerrors.Errorf("a task cannot be started with an empty input") | ||
} | ||
switch { | ||
case templateName == "": | ||
templates, err := client.Templates(ctx, codersdk.TemplateFilter{SearchQuery: "has-ai-task:true", OrganizationID: organization.ID}) | ||
if err != nil { | ||
return xerrors.Errorf("list templates: %w", err) | ||
} | ||
if len(templates) == 0 { | ||
return xerrors.Errorf("no task templates configured") | ||
} | ||
// When a deployment has only 1 AI task template, we will | ||
// allow omitting the template. Otherwise we will require | ||
// the user to be explicit with their choice of template. | ||
if len(templates) > 1 { | ||
templateNames := make([]string, 0, len(templates)) | ||
for _, template := range templates { | ||
templateNames = append(templateNames, template.Name) | ||
} | ||
return xerrors.Errorf("template name not provided, available templates: %s", strings.Join(templateNames, ", ")) | ||
} | ||
if templateVersionName != "" { | ||
templateVersion, err := client.TemplateVersionByOrganizationAndName(ctx, organization.ID, templates[0].Name, templateVersionName) | ||
if err != nil { | ||
return xerrors.Errorf("get template version: %w", err) | ||
} | ||
templateVersionID = templateVersion.ID | ||
} else { | ||
templateVersionID = templates[0].ActiveVersionID | ||
} | ||
case templateVersionName != "": | ||
templateVersion, err := client.TemplateVersionByOrganizationAndName(ctx, organization.ID, templateName, templateVersionName) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
if err != nil { | ||
return xerrors.Errorf("get template version: %w", err) | ||
} | ||
templateVersionID = templateVersion.ID | ||
default: | ||
template, err := client.TemplateByName(ctx, organization.ID, templateName) | ||
if err != nil { | ||
return xerrors.Errorf("get template: %w", err) | ||
Uh oh!
There was an error while loading.Please reload this page.