Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf872b51

Browse files
committed
feat: addtemplates delete command
1 parent19335df commitf872b51

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

‎cli/templatedelete.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
"golang.org/x/xerrors"
8+
9+
"github.com/coder/coder/cli/cliui"
10+
"github.com/coder/coder/codersdk"
11+
)
12+
13+
functemplateDelete()*cobra.Command {
14+
return&cobra.Command{
15+
Use:"delete [name]",
16+
Args:cobra.MaximumNArgs(1),
17+
Short:"Delete a template.",
18+
RunE:func(cmd*cobra.Command,args []string)error {
19+
var (
20+
ctx=cmd.Context()
21+
templateNames= []string{}
22+
templates= []codersdk.Template{}
23+
)
24+
25+
client,err:=createClient(cmd)
26+
iferr!=nil {
27+
returnerr
28+
}
29+
organization,err:=currentOrganization(cmd,client)
30+
iferr!=nil {
31+
returnerr
32+
}
33+
34+
iflen(args)>0 {
35+
templateNames=args
36+
}else {
37+
allTemplates,err:=client.TemplatesByOrganization(ctx,organization.ID)
38+
iferr!=nil {
39+
returnxerrors.Errorf("get templates by organization: %w",err)
40+
}
41+
42+
iflen(allTemplates)==0 {
43+
returnxerrors.Errorf("no templates exist in the current organization %q",organization.Name)
44+
}
45+
46+
opts:=make([]string,0,len(allTemplates))
47+
for_,template:=rangeallTemplates {
48+
opts=append(opts,template.Name)
49+
}
50+
51+
selection,err:=cliui.Select(cmd, cliui.SelectOptions{
52+
Options:opts,
53+
})
54+
iferr!=nil {
55+
returnxerrors.Errorf("select template: %w",err)
56+
}
57+
58+
for_,template:=rangeallTemplates {
59+
iftemplate.Name==selection {
60+
templates=append(templates,template)
61+
}
62+
}
63+
}
64+
65+
for_,templateName:=rangetemplateNames {
66+
template,err:=client.TemplateByName(ctx,organization.ID,templateName)
67+
iferr!=nil {
68+
returnxerrors.Errorf("get template by name: %w",err)
69+
}
70+
71+
templates=append(templates,template)
72+
}
73+
74+
for_,template:=rangetemplates {
75+
err:=client.DeleteTemplate(ctx,template.ID)
76+
iferr!=nil {
77+
returnxerrors.Errorf("delete template %q: %w",template.Name,err)
78+
}
79+
80+
_,_=fmt.Fprintln(cmd.ErrOrStderr(),"Deleted template "+cliui.Styles.Code.Render(template.Name)+"!")
81+
}
82+
83+
returnnil
84+
},
85+
}
86+
}

‎cli/templatedelete_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package cli_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/coder/coder/cli/clitest"
8+
"github.com/coder/coder/coderd/coderdtest"
9+
"github.com/coder/coder/pty/ptytest"
10+
"github.com/davecgh/go-spew/spew"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
funcTestTemplateDelete(t*testing.T) {
15+
t.Parallel()
16+
17+
t.Run("Ok",func(t*testing.T) {
18+
t.Parallel()
19+
20+
client:=coderdtest.New(t,nil)
21+
user:=coderdtest.CreateFirstUser(t,client)
22+
_=coderdtest.NewProvisionerDaemon(t,client)
23+
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
24+
_=coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
25+
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
26+
27+
cmd,root:=clitest.New(t,"templates","delete",template.Name)
28+
clitest.SetupConfig(t,client,root)
29+
require.NoError(t,cmd.Execute())
30+
31+
template,err:=client.Template(context.Background(),template.ID)
32+
spew.Dump(template,err)
33+
require.Error(t,err,"template should not exist")
34+
})
35+
36+
t.Run("Selector",func(t*testing.T) {
37+
t.Parallel()
38+
39+
client:=coderdtest.New(t,nil)
40+
user:=coderdtest.CreateFirstUser(t,client)
41+
_=coderdtest.NewProvisionerDaemon(t,client)
42+
version:=coderdtest.CreateTemplateVersion(t,client,user.OrganizationID,nil)
43+
_=coderdtest.AwaitTemplateVersionJob(t,client,version.ID)
44+
template:=coderdtest.CreateTemplate(t,client,user.OrganizationID,version.ID)
45+
46+
cmd,root:=clitest.New(t,"templates","delete")
47+
clitest.SetupConfig(t,client,root)
48+
49+
pty:=ptytest.New(t)
50+
cmd.SetIn(pty.Input())
51+
cmd.SetOut(pty.Output())
52+
53+
execDone:=make(chanerror)
54+
gofunc() {
55+
execDone<-cmd.Execute()
56+
}()
57+
58+
pty.WriteLine("docker-local")
59+
require.NoError(t,<-execDone)
60+
61+
_,err:=client.Template(context.Background(),template.ID)
62+
require.Error(t,err,"template should not exist")
63+
})
64+
}

‎cli/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func templates() *cobra.Command {
3030
templatePlan(),
3131
templateUpdate(),
3232
templateVersions(),
33+
templateDelete(),
3334
)
3435

3536
returncmd

‎coderd/httpmw/templateparam.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ func ExtractTemplateParam(db database.Store) func(http.Handler) http.Handler {
4545
return
4646
}
4747

48+
iftemplate.Deleted {
49+
httpapi.Write(rw,http.StatusNotFound, httpapi.Response{
50+
Message:fmt.Sprintf("template %q does not exist",templateID),
51+
})
52+
}
53+
4854
ctx:=context.WithValue(r.Context(),templateParamContextKey{},template)
4955
chi.RouteContext(ctx).URLParams.Add("organization",template.OrganizationID.String())
5056
next.ServeHTTP(rw,r.WithContext(ctx))

‎coderd/templates.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
// Returns a single template.
1919
func (api*api)template(rw http.ResponseWriter,r*http.Request) {
2020
template:=httpmw.TemplateParam(r)
21+
2122
workspaceCounts,err:=api.Database.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), []uuid.UUID{template.ID})
2223
iferrors.Is(err,sql.ErrNoRows) {
2324
err=nil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp