|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"slices" |
| 6 | +"strings" |
| 7 | +"time" |
| 8 | + |
| 9 | +"github.com/coder/coder/v2/cli/cliui" |
| 10 | +"github.com/coder/coder/v2/coderd/util/slice" |
| 11 | +"github.com/coder/coder/v2/codersdk" |
| 12 | +"github.com/coder/serpent" |
| 13 | +"github.com/google/uuid" |
| 14 | +"golang.org/x/xerrors" |
| 15 | +) |
| 16 | + |
| 17 | +func (r*RootCmd)taskCreate()*serpent.Command { |
| 18 | +var ( |
| 19 | +orgContext=NewOrganizationContext() |
| 20 | +client=new(codersdk.Client) |
| 21 | + |
| 22 | +templateNamestring |
| 23 | +templateVersionNamestring |
| 24 | +presetNamestring |
| 25 | +taskInputstring |
| 26 | +) |
| 27 | + |
| 28 | +return&serpent.Command{ |
| 29 | +Use:"create [task]", |
| 30 | +Short:"Create an experimental task", |
| 31 | +Middleware:serpent.Chain( |
| 32 | +serpent.RequireRangeArgs(0,1), |
| 33 | +r.InitClient(client), |
| 34 | +), |
| 35 | +Options: serpent.OptionSet{ |
| 36 | +{ |
| 37 | +Flag:"input", |
| 38 | +Env:"CODER_TASK_INPUT", |
| 39 | +Value:serpent.StringOf(&taskInput), |
| 40 | +Required:true, |
| 41 | +}, |
| 42 | +{ |
| 43 | +Env:"CODER_TEMPLATE_NAME", |
| 44 | +Value:serpent.StringOf(&templateName), |
| 45 | +}, |
| 46 | +{ |
| 47 | +Env:"CODER_TEMPLATE_VERSION", |
| 48 | +Value:serpent.StringOf(&templateVersionName), |
| 49 | +}, |
| 50 | +{ |
| 51 | +Flag:"preset", |
| 52 | +Env:"CODER_PRESET_NAME", |
| 53 | +Value:serpent.StringOf(&presetName), |
| 54 | +Default:PresetNone, |
| 55 | +}, |
| 56 | +}, |
| 57 | +Handler:func(inv*serpent.Invocation)error { |
| 58 | +var ( |
| 59 | +ctx=inv.Context() |
| 60 | +expClient=codersdk.NewExperimentalClient(client) |
| 61 | + |
| 62 | +templateVersionID uuid.UUID |
| 63 | +templateVersionPresetID uuid.UUID |
| 64 | +) |
| 65 | + |
| 66 | +organization,err:=orgContext.Selected(inv,client) |
| 67 | +iferr!=nil { |
| 68 | +returnxerrors.Errorf("get current organization: %w",err) |
| 69 | +} |
| 70 | + |
| 71 | +iflen(inv.Args)>0 { |
| 72 | +templateName,templateVersionName,_=strings.Cut(inv.Args[0],"@") |
| 73 | +} |
| 74 | + |
| 75 | +iftemplateName=="" { |
| 76 | +templates,err:=client.Templates(ctx, codersdk.TemplateFilter{SearchQuery:"has-ai-task:true"}) |
| 77 | +iferr!=nil { |
| 78 | +returnxerrors.Errorf("get templates: %w",err) |
| 79 | +} |
| 80 | + |
| 81 | +slices.SortFunc(templates,func(a,b codersdk.Template)int { |
| 82 | +returnslice.Descending(a.ActiveUserCount,b.ActiveUserCount) |
| 83 | +}) |
| 84 | + |
| 85 | +templateNames:=make([]string,0,len(templates)) |
| 86 | +templateByName:=make(map[string]codersdk.Template,len(templates)) |
| 87 | + |
| 88 | +// If more than 1 organization exists in the list of templates, |
| 89 | +// then include the organization name in the select options. |
| 90 | +uniqueOrganizations:=make(map[uuid.UUID]bool) |
| 91 | +for_,template:=rangetemplates { |
| 92 | +uniqueOrganizations[template.OrganizationID]=true |
| 93 | +} |
| 94 | + |
| 95 | +for_,template:=rangetemplates { |
| 96 | +templateName:=template.Name |
| 97 | +iflen(uniqueOrganizations)>1 { |
| 98 | +templateName+=cliui.Placeholder( |
| 99 | +fmt.Sprintf( |
| 100 | +" (%s)", |
| 101 | +template.OrganizationName, |
| 102 | +), |
| 103 | +) |
| 104 | +} |
| 105 | + |
| 106 | +iftemplate.ActiveUserCount>0 { |
| 107 | +templateName+=cliui.Placeholder( |
| 108 | +fmt.Sprintf( |
| 109 | +" used by %s", |
| 110 | +formatActiveDevelopers(template.ActiveUserCount), |
| 111 | +), |
| 112 | +) |
| 113 | +} |
| 114 | + |
| 115 | +templateNames=append(templateNames,templateName) |
| 116 | +templateByName[templateName]=template |
| 117 | +} |
| 118 | + |
| 119 | +option,err:=cliui.Select(inv, cliui.SelectOptions{ |
| 120 | +Options:templateNames, |
| 121 | +HideSearch:true, |
| 122 | +}) |
| 123 | + |
| 124 | +templateName=templateByName[option].Name |
| 125 | +} |
| 126 | + |
| 127 | +iftemplateVersionName!="" { |
| 128 | +templateVersion,err:=client.TemplateVersionByOrganizationAndName(ctx,organization.ID,templateName,templateVersionName) |
| 129 | +iferr!=nil { |
| 130 | +returnxerrors.Errorf("get template version: %w",err) |
| 131 | +} |
| 132 | + |
| 133 | +templateVersionID=templateVersion.ID |
| 134 | +}else { |
| 135 | +template,err:=client.TemplateByName(ctx,organization.ID,templateName) |
| 136 | +iferr!=nil { |
| 137 | +returnxerrors.Errorf("get template: %w",err) |
| 138 | +} |
| 139 | + |
| 140 | +templateVersionID=template.ActiveVersionID |
| 141 | +} |
| 142 | + |
| 143 | +ifpresetName!=PresetNone { |
| 144 | +templatePresets,err:=client.TemplateVersionPresets(ctx,templateVersionID) |
| 145 | +iferr!=nil { |
| 146 | +returnxerrors.Errorf("get template presets: %w",err) |
| 147 | +} |
| 148 | + |
| 149 | +preset,err:=resolvePreset(templatePresets,presetName) |
| 150 | +iferr!=nil { |
| 151 | +returnxerrors.Errorf("resolve preset: %w",err) |
| 152 | +} |
| 153 | + |
| 154 | +templateVersionID=preset.ID |
| 155 | +} |
| 156 | + |
| 157 | +workspace,err:=expClient.CreateTask(ctx,codersdk.Me, codersdk.CreateTaskRequest{ |
| 158 | +TemplateVersionID:templateVersionID, |
| 159 | +TemplateVersionPresetID:templateVersionPresetID, |
| 160 | +Prompt:taskInput, |
| 161 | +}) |
| 162 | + |
| 163 | +_,_=fmt.Fprintf( |
| 164 | +inv.Stdout, |
| 165 | +"The task %s has been created at %s!\n", |
| 166 | +cliui.Keyword(workspace.Name), |
| 167 | +cliui.Timestamp(time.Now()), |
| 168 | +) |
| 169 | + |
| 170 | +returnnil |
| 171 | +}, |
| 172 | +} |
| 173 | +} |