-
Notifications
You must be signed in to change notification settings - Fork 906
feat: add templates delete
command
#1443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f872b51
feat: add `templates delete` command
coadler 3a4782a
add return + remove max arg limit
coadler f7aca88
add test for deleting multiple templates
coadler 5c6b745
fix use doc
coadler 6279794
sentence i guess
coadler 52b0f6d
"Delete templates"
coadler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
func templateDelete() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "delete [name...]", | ||
Short: "Delete templates", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var ( | ||
ctx = cmd.Context() | ||
templateNames = []string{} | ||
templates = []codersdk.Template{} | ||
) | ||
|
||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
organization, err := currentOrganization(cmd, client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(args) > 0 { | ||
templateNames = args | ||
} 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) | ||
} | ||
|
||
selection, err := cliui.Select(cmd, cliui.SelectOptions{ | ||
Options: opts, | ||
}) | ||
if err != nil { | ||
return xerrors.Errorf("select template: %w", err) | ||
} | ||
|
||
for _, template := range allTemplates { | ||
if template.Name == selection { | ||
templates = append(templates, template) | ||
} | ||
} | ||
} | ||
|
||
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) | ||
} | ||
|
||
for _, template := range templates { | ||
err := client.DeleteTemplate(ctx, template.ID) | ||
if err != nil { | ||
return xerrors.Errorf("delete template %q: %w", template.Name, err) | ||
} | ||
|
||
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "Deleted template "+cliui.Styles.Code.Render(template.Name)+"!") | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package cli_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/pty/ptytest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTemplateDelete(t *testing.T) { | ||
t.Parallel() | ||
|
||
t.Run("Ok", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, nil) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
_ = coderdtest.NewProvisionerDaemon(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
|
||
cmd, root := clitest.New(t, "templates", "delete", template.Name) | ||
clitest.SetupConfig(t, client, root) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
_, err := client.Template(context.Background(), template.ID) | ||
require.Error(t, err, "template should not exist") | ||
}) | ||
|
||
t.Run("Multiple", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, nil) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
_ = coderdtest.NewProvisionerDaemon(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
templates := []codersdk.Template{ | ||
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID), | ||
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID), | ||
coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID), | ||
} | ||
templateNames := []string{} | ||
for _, template := range templates { | ||
templateNames = append(templateNames, template.Name) | ||
} | ||
|
||
cmd, root := clitest.New(t, append([]string{"templates", "delete"}, templateNames...)...) | ||
clitest.SetupConfig(t, client, root) | ||
require.NoError(t, cmd.Execute()) | ||
|
||
for _, template := range templates { | ||
_, err := client.Template(context.Background(), template.ID) | ||
require.Error(t, err, "template should not exist") | ||
} | ||
}) | ||
|
||
t.Run("Selector", func(t *testing.T) { | ||
t.Parallel() | ||
|
||
client := coderdtest.New(t, nil) | ||
user := coderdtest.CreateFirstUser(t, client) | ||
_ = coderdtest.NewProvisionerDaemon(t, client) | ||
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil) | ||
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID) | ||
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID) | ||
|
||
cmd, root := clitest.New(t, "templates", "delete") | ||
clitest.SetupConfig(t, client, root) | ||
|
||
pty := ptytest.New(t) | ||
cmd.SetIn(pty.Input()) | ||
cmd.SetOut(pty.Output()) | ||
|
||
execDone := make(chan error) | ||
go func() { | ||
execDone <- cmd.Execute() | ||
}() | ||
|
||
pty.WriteLine("docker-local") | ||
require.NoError(t, <-execDone) | ||
|
||
_, err := client.Template(context.Background(), template.ID) | ||
require.Error(t, err, "template should not exist") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can only be a max of 1 right? I don't believe this needs to be an array!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot to remove the max 1 limit! I think it's nicer if we take in more than one since it plays nicer with xargs.