|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"strings" |
| 6 | +"time" |
| 7 | + |
| 8 | +"github.com/coder/coder/v2/cli/cliui" |
| 9 | +"github.com/coder/coder/v2/codersdk" |
| 10 | +"github.com/coder/serpent" |
| 11 | +"github.com/google/uuid" |
| 12 | +"golang.org/x/xerrors" |
| 13 | +) |
| 14 | + |
| 15 | +func (r*RootCmd)taskCreate()*serpent.Command { |
| 16 | +var ( |
| 17 | +orgContext=NewOrganizationContext() |
| 18 | +client=new(codersdk.Client) |
| 19 | + |
| 20 | +templateNamestring |
| 21 | +templateVersionNamestring |
| 22 | +presetNamestring |
| 23 | +taskInputstring |
| 24 | +) |
| 25 | + |
| 26 | +return&serpent.Command{ |
| 27 | +Use:"create [task]", |
| 28 | +Short:"Create a task", |
| 29 | +Middleware:serpent.Chain( |
| 30 | +serpent.RequireRangeArgs(0,1), |
| 31 | +r.InitClient(client), |
| 32 | +), |
| 33 | +Options: serpent.OptionSet{ |
| 34 | +{ |
| 35 | +Flag:"input", |
| 36 | +Env:"CODER_TASK_INPUT", |
| 37 | +Value:serpent.StringOf(&taskInput), |
| 38 | +Required:true, |
| 39 | +}, |
| 40 | +{ |
| 41 | +Env:"CODER_TEMPLATE_NAME", |
| 42 | +Value:serpent.StringOf(&templateName), |
| 43 | +}, |
| 44 | +{ |
| 45 | +Env:"CODER_TEMPLATE_VERSION", |
| 46 | +Value:serpent.StringOf(&templateVersionName), |
| 47 | +}, |
| 48 | +{ |
| 49 | +Flag:"preset", |
| 50 | +Env:"CODER_PRESET_NAME", |
| 51 | +Value:serpent.StringOf(&presetName), |
| 52 | +Default:PresetNone, |
| 53 | +}, |
| 54 | +}, |
| 55 | +Handler:func(inv*serpent.Invocation)error { |
| 56 | +var ( |
| 57 | +ctx=inv.Context() |
| 58 | +expClient=codersdk.NewExperimentalClient(client) |
| 59 | + |
| 60 | +templateVersionID uuid.UUID |
| 61 | +templateVersionPresetID uuid.UUID |
| 62 | +) |
| 63 | + |
| 64 | +organization,err:=orgContext.Selected(inv,client) |
| 65 | +iferr!=nil { |
| 66 | +returnxerrors.Errorf("get current organization: %w",err) |
| 67 | +} |
| 68 | + |
| 69 | +iflen(inv.Args)>0 { |
| 70 | +templateName,templateVersionName,_=strings.Cut(inv.Args[0],"@") |
| 71 | +}elseiftemplateName=="" { |
| 72 | +returnxerrors.Errorf("template name not provided") |
| 73 | +} |
| 74 | + |
| 75 | +iftemplateVersionName!="" { |
| 76 | +templateVersion,err:=client.TemplateVersionByOrganizationAndName(ctx,organization.ID,templateName,templateVersionName) |
| 77 | +iferr!=nil { |
| 78 | +returnxerrors.Errorf("get template version: %w",err) |
| 79 | +} |
| 80 | + |
| 81 | +templateVersionID=templateVersion.ID |
| 82 | +}else { |
| 83 | +template,err:=client.TemplateByName(ctx,organization.ID,templateName) |
| 84 | +iferr!=nil { |
| 85 | +returnxerrors.Errorf("get template: %w",err) |
| 86 | +} |
| 87 | + |
| 88 | +templateVersionID=template.ActiveVersionID |
| 89 | +} |
| 90 | + |
| 91 | +ifpresetName!=PresetNone { |
| 92 | +templatePresets,err:=client.TemplateVersionPresets(ctx,templateVersionID) |
| 93 | +iferr!=nil { |
| 94 | +returnxerrors.Errorf("get template presets: %w",err) |
| 95 | +} |
| 96 | + |
| 97 | +preset,err:=resolvePreset(templatePresets,presetName) |
| 98 | +iferr!=nil { |
| 99 | +returnxerrors.Errorf("resolve preset: %w",err) |
| 100 | +} |
| 101 | + |
| 102 | +templateVersionID=preset.ID |
| 103 | +} |
| 104 | + |
| 105 | +workspace,err:=expClient.CreateTask(ctx,codersdk.Me, codersdk.CreateTaskRequest{ |
| 106 | +TemplateVersionID:templateVersionID, |
| 107 | +TemplateVersionPresetID:templateVersionPresetID, |
| 108 | +Prompt:taskInput, |
| 109 | +}) |
| 110 | + |
| 111 | +_,_=fmt.Fprintf( |
| 112 | +inv.Stdout, |
| 113 | +"The task %s has been created at %s!\n", |
| 114 | +cliui.Keyword(workspace.Name), |
| 115 | +cliui.Timestamp(time.Now()), |
| 116 | +) |
| 117 | + |
| 118 | +returnnil |
| 119 | +}, |
| 120 | +} |
| 121 | +} |