2022-06-08 15:14:57 +01:00
package cli
import (
"fmt"
"time"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
func templateEdit () * cobra . Command {
var (
2022-11-21 11:43:53 +01:00
name string
displayName string
description string
icon string
defaultTTL time . Duration
allowUserCancelWorkspaceJobs bool
2022-06-08 15:14:57 +01:00
)
cmd := & cobra . Command {
Use : "edit <template> [flags]" ,
Args : cobra . ExactArgs ( 1 ),
Short : "Edit the metadata of a template by name." ,
RunE : func ( cmd * cobra . Command , args [] string ) error {
2022-08-23 13:55:39 -07:00
client , err := CreateClient ( cmd )
2022-06-08 15:14:57 +01:00
if err != nil {
return xerrors . Errorf ( "create client: %w" , err )
}
2022-10-27 16:49:35 -05:00
organization , err := CurrentOrganization ( cmd , client )
2022-06-08 15:14:57 +01:00
if err != nil {
return xerrors . Errorf ( "get current organization: %w" , err )
}
template , err := client . TemplateByName ( cmd . Context (), organization . ID , args [ 0 ])
if err != nil {
return xerrors . Errorf ( "get workspace template: %w" , err )
}
// NOTE: coderd will ignore empty fields.
req := codersdk . UpdateTemplateMeta {
2022-11-21 11:43:53 +01:00
Name : name ,
DisplayName : displayName ,
Description : description ,
Icon : icon ,
DefaultTTLMillis : defaultTTL . Milliseconds (),
AllowUserCancelWorkspaceJobs : allowUserCancelWorkspaceJobs ,
2022-06-08 15:14:57 +01:00
}
_ , err = client . UpdateTemplateMeta ( cmd . Context (), template . ID , req )
if err != nil {
return xerrors . Errorf ( "update template metadata: %w" , err )
}
2022-07-26 17:23:32 +03:00
_ , _ = fmt . Fprintf ( cmd . OutOrStdout (), "Updated template metadata at %s!\n" , cliui . Styles . DateTimeStamp . Render ( time . Now (). Format ( time . Stamp )))
2022-06-08 15:14:57 +01:00
return nil
},
}
2022-08-17 16:04:00 -03:00
cmd . Flags (). StringVarP ( & name , "name" , "" , "" , "Edit the template name" )
2022-11-10 21:51:09 +01:00
cmd . Flags (). StringVarP ( & displayName , "display-name" , "" , "" , "Edit the template display name" )
2022-06-08 15:14:57 +01:00
cmd . Flags (). StringVarP ( & description , "description" , "" , "" , "Edit the template description" )
2022-08-19 10:17:35 -03:00
cmd . Flags (). StringVarP ( & icon , "icon" , "" , "" , "Edit the template icon path" )
2022-11-09 14:36:25 -05:00
cmd . Flags (). DurationVarP ( & defaultTTL , "default-ttl" , "" , 0 , "Edit the template default time before shutdown - workspaces created from this template to this value." )
2022-11-21 11:43:53 +01:00
cmd . Flags (). BoolVarP ( & allowUserCancelWorkspaceJobs , "allow-user-cancel-workspace-jobs" , "" , true , "Allow users to cancel in-progress workspace jobs." )
2022-06-08 15:14:57 +01:00
cliui . AllowSkipPrompt ( cmd )
return cmd
}