2022-06-08 14:14:57 +00:00
package cli
import (
"fmt"
2023-03-07 14:14:58 +00:00
"net/http"
2022-06-08 14:14:57 +00:00
"time"
"golang.org/x/xerrors"
2023-03-23 22:42:20 +00:00
"github.com/coder/coder/cli/clibase"
2022-06-08 14:14:57 +00:00
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
2023-03-23 22:42:20 +00:00
func ( r * RootCmd ) templateEdit ( ) * clibase . Cmd {
2022-06-08 14:14:57 +00:00
var (
2022-11-21 10:43:53 +00:00
name string
displayName string
description string
icon string
defaultTTL time . Duration
2023-03-07 14:14:58 +00:00
maxTTL time . Duration
2022-11-21 10:43:53 +00:00
allowUserCancelWorkspaceJobs bool
2022-06-08 14:14:57 +00:00
)
2023-03-23 22:42:20 +00:00
client := new ( codersdk . Client )
2022-06-08 14:14:57 +00:00
2023-03-23 22:42:20 +00:00
cmd := & clibase . Cmd {
Use : "edit <template>" ,
Middleware : clibase . Chain (
clibase . RequireNArgs ( 1 ) ,
r . InitClient ( client ) ,
) ,
2022-06-08 14:14:57 +00:00
Short : "Edit the metadata of a template by name." ,
2023-03-23 22:42:20 +00:00
Handler : func ( inv * clibase . Invocation ) error {
2023-03-07 14:14:58 +00:00
if maxTTL != 0 {
2023-03-23 22:42:20 +00:00
entitlements , err := client . Entitlements ( inv . Context ( ) )
2023-03-07 14:14:58 +00:00
var sdkErr * codersdk . Error
if xerrors . As ( err , & sdkErr ) && sdkErr . StatusCode ( ) == http . StatusNotFound {
return xerrors . Errorf ( "your deployment appears to be an AGPL deployment, so you cannot set --max-ttl" )
} else if err != nil {
return xerrors . Errorf ( "get entitlements: %w" , err )
}
if ! entitlements . Features [ codersdk . FeatureAdvancedTemplateScheduling ] . Enabled {
return xerrors . Errorf ( "your license is not entitled to use advanced template scheduling, so you cannot set --max-ttl" )
}
}
2023-03-23 22:42:20 +00:00
organization , err := CurrentOrganization ( inv , client )
2022-06-08 14:14:57 +00:00
if err != nil {
return xerrors . Errorf ( "get current organization: %w" , err )
}
2023-03-23 22:42:20 +00:00
template , err := client . TemplateByName ( inv . Context ( ) , organization . ID , inv . Args [ 0 ] )
2022-06-08 14:14:57 +00:00
if err != nil {
return xerrors . Errorf ( "get workspace template: %w" , err )
}
// NOTE: coderd will ignore empty fields.
req := codersdk . UpdateTemplateMeta {
2022-11-21 10:43:53 +00:00
Name : name ,
DisplayName : displayName ,
Description : description ,
Icon : icon ,
DefaultTTLMillis : defaultTTL . Milliseconds ( ) ,
2023-03-07 14:14:58 +00:00
MaxTTLMillis : maxTTL . Milliseconds ( ) ,
2022-11-21 10:43:53 +00:00
AllowUserCancelWorkspaceJobs : allowUserCancelWorkspaceJobs ,
2022-06-08 14:14:57 +00:00
}
2023-03-23 22:42:20 +00:00
_ , err = client . UpdateTemplateMeta ( inv . Context ( ) , template . ID , req )
2022-06-08 14:14:57 +00:00
if err != nil {
return xerrors . Errorf ( "update template metadata: %w" , err )
}
2023-03-23 22:42:20 +00:00
_ , _ = fmt . Fprintf ( inv . Stdout , "Updated template metadata at %s!\n" , cliui . Styles . DateTimeStamp . Render ( time . Now ( ) . Format ( time . Stamp ) ) )
2022-06-08 14:14:57 +00:00
return nil
} ,
}
2023-03-23 22:42:20 +00:00
cmd . Options = clibase . OptionSet {
{
Flag : "name" ,
Description : "Edit the template name." ,
Value : clibase . StringOf ( & name ) ,
} ,
{
Flag : "display-name" ,
Description : "Edit the template display name." ,
Value : clibase . StringOf ( & displayName ) ,
} ,
{
Flag : "description" ,
Description : "Edit the template description." ,
Value : clibase . StringOf ( & description ) ,
} ,
{
Flag : "icon" ,
Description : "Edit the template icon path." ,
Value : clibase . StringOf ( & icon ) ,
} ,
{
Flag : "default-ttl" ,
Description : "Edit the template default time before shutdown - workspaces created from this template default to this value." ,
Value : clibase . DurationOf ( & defaultTTL ) ,
} ,
{
Flag : "max-ttl" ,
Description : "Edit the template maximum time before shutdown - workspaces created from this template must shutdown within the given duration after starting. This is an enterprise-only feature." ,
Value : clibase . DurationOf ( & maxTTL ) ,
} ,
{
Flag : "allow-user-cancel-workspace-jobs" ,
Description : "Allow users to cancel in-progress workspace jobs." ,
Default : "true" ,
Value : clibase . BoolOf ( & allowUserCancelWorkspaceJobs ) ,
} ,
cliui . SkipPromptOption ( ) ,
}
2022-06-08 14:14:57 +00:00
return cmd
}