2022-05-13 22:54:32 +00:00
package cli
import (
"fmt"
2022-07-11 18:13:56 +00:00
"strings"
2022-07-08 19:27:56 +00:00
"time"
2022-05-13 22:54:32 +00:00
"golang.org/x/xerrors"
2023-03-23 22:42:20 +00:00
"github.com/coder/coder/cli/clibase"
2022-05-13 22:54:32 +00:00
"github.com/coder/coder/cli/cliui"
"github.com/coder/coder/codersdk"
)
2023-03-23 22:42:20 +00:00
func ( r * RootCmd ) templateDelete ( ) * clibase . Cmd {
client := new ( codersdk . Client )
cmd := & clibase . Cmd {
2022-05-13 22:54:32 +00:00
Use : "delete [name...]" ,
Short : "Delete templates" ,
2023-03-23 22:42:20 +00:00
Middleware : clibase . Chain (
r . InitClient ( client ) ,
) ,
Options : clibase . OptionSet {
cliui . SkipPromptOption ( ) ,
} ,
Handler : func ( inv * clibase . Invocation ) error {
2022-05-13 22:54:32 +00:00
var (
2023-03-23 22:42:20 +00:00
ctx = inv . Context ( )
2022-05-13 22:54:32 +00:00
templateNames = [ ] string { }
templates = [ ] codersdk . Template { }
)
2023-03-23 22:42:20 +00:00
organization , err := CurrentOrganization ( inv , client )
2022-05-13 22:54:32 +00:00
if err != nil {
return err
}
2023-03-23 22:42:20 +00:00
if len ( inv . Args ) > 0 {
templateNames = inv . Args
2022-07-11 18:13:56 +00:00
for _ , templateName := range templateNames {
template , err := client . TemplateByName ( ctx , organization . ID , templateName )
if err != nil {
return xerrors . Errorf ( "get template by name: %w" , err )
}
templates = append ( templates , template )
}
2022-05-13 22:54:32 +00:00
} else {
allTemplates , err := client . TemplatesByOrganization ( ctx , organization . ID )
if err != nil {
return xerrors . Errorf ( "get templates by organization: %w" , err )
}
if len ( allTemplates ) == 0 {
return xerrors . Errorf ( "no templates exist in the current organization %q" , organization . Name )
}
opts := make ( [ ] string , 0 , len ( allTemplates ) )
for _ , template := range allTemplates {
opts = append ( opts , template . Name )
}
2023-03-23 22:42:20 +00:00
selection , err := cliui . Select ( inv , cliui . SelectOptions {
2022-05-13 22:54:32 +00:00
Options : opts ,
} )
if err != nil {
return xerrors . Errorf ( "select template: %w" , err )
}
for _ , template := range allTemplates {
if template . Name == selection {
templates = append ( templates , template )
2022-07-11 18:13:56 +00:00
templateNames = append ( templateNames , template . Name )
2022-05-13 22:54:32 +00:00
}
}
}
2022-07-11 18:13:56 +00:00
// Confirm deletion of the template.
2023-03-23 22:42:20 +00:00
_ , err = cliui . Prompt ( inv , cliui . PromptOptions {
2022-07-11 18:13:56 +00:00
Text : fmt . Sprintf ( "Delete these templates: %s?" , cliui . Styles . Code . Render ( strings . Join ( templateNames , ", " ) ) ) ,
IsConfirm : true ,
2022-07-12 17:36:07 +00:00
Default : cliui . ConfirmNo ,
2022-07-11 18:13:56 +00:00
} )
if err != nil {
return err
2022-05-13 22:54:32 +00:00
}
for _ , template := range templates {
err := client . DeleteTemplate ( ctx , template . ID )
if err != nil {
return xerrors . Errorf ( "delete template %q: %w" , template . Name , err )
}
2023-03-23 22:42:20 +00:00
_ , _ = fmt . Fprintln ( inv . Stdout , "Deleted template " + cliui . Styles . Code . Render ( template . Name ) + " at " + cliui . Styles . DateTimeStamp . Render ( time . Now ( ) . Format ( time . Stamp ) ) + "!" )
2022-05-13 22:54:32 +00:00
}
return nil
} ,
}
2022-07-11 18:13:56 +00:00
return cmd
2022-05-13 22:54:32 +00:00
}