Skip to content

Commit fd9275b

Browse files
authored
Merge pull request #52 from zeabur/xiayuhang1106/zea-1524-template-queries-and-mutations
Xiayuhang1106/zea 1524 template queries and mutations
2 parents cab9077 + 8d3ac03 commit fd9275b

File tree

11 files changed

+507
-1
lines changed

11 files changed

+507
-1
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ linters-settings:
1616
govet:
1717
check-shadowing: true
1818
enable-all: true
19+
disable: ["fieldalignment"]
1920
errcheck:
2021
exclude-functions:

internal/cmd/project/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/briandowns/spinner"
88
"github.com/spf13/cobra"
9+
910
"github.com/zeabur/cli/internal/cmdutil"
1011
)
1112

@@ -41,7 +42,6 @@ func runCreate(f *cmdutil.Factory, opts *Options) error {
4142
}
4243

4344
return runCreateNonInteractive(f, opts)
44-
4545
}
4646

4747
func runCreateInteractive(f *cmdutil.Factory, opts *Options) error {

internal/cmd/root/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
deploymentCmd "github.com/zeabur/cli/internal/cmd/deployment"
2121
projectCmd "github.com/zeabur/cli/internal/cmd/project"
2222
serviceCmd "github.com/zeabur/cli/internal/cmd/service"
23+
templateCmd "github.com/zeabur/cli/internal/cmd/template"
2324
versionCmd "github.com/zeabur/cli/internal/cmd/version"
2425
"github.com/zeabur/cli/internal/cmdutil"
2526
"github.com/zeabur/cli/pkg/api"
@@ -108,6 +109,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, commit, date string) (*cobra.Comman
108109
cmd.AddCommand(projectCmd.NewCmdProject(f))
109110
cmd.AddCommand(serviceCmd.NewCmdService(f))
110111
cmd.AddCommand(deploymentCmd.NewCmdDeployment(f))
112+
cmd.AddCommand(templateCmd.NewCmdTemplate(f))
111113

112114
cmd.AddCommand(contextCmd.NewCmdContext(f))
113115

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package delete
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/briandowns/spinner"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/zeabur/cli/internal/cmdutil"
11+
)
12+
13+
type Options struct {
14+
code string
15+
}
16+
17+
func NewCmdDelete(f *cmdutil.Factory) *cobra.Command {
18+
opts := Options{}
19+
20+
cmd := &cobra.Command{
21+
Use: "delete",
22+
Short: "Delete template by code",
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return runDelete(f, opts)
25+
},
26+
}
27+
28+
cmd.Flags().StringVarP(&opts.code, "code", "c", "", "Template code")
29+
30+
return cmd
31+
}
32+
33+
func runDelete(f *cmdutil.Factory, opts Options) error {
34+
if f.Interactive {
35+
return runDeleteInteractive(f, opts)
36+
}
37+
return runDeleteNonInteractive(f, opts)
38+
39+
}
40+
41+
func runDeleteInteractive(f *cmdutil.Factory, opts Options) error {
42+
code, err := f.Prompter.Input("Template Code: ", "")
43+
if err != nil {
44+
return err
45+
}
46+
47+
opts.code = code
48+
49+
err = deleteTemplate(f, opts)
50+
if err != nil {
51+
return err
52+
}
53+
54+
return nil
55+
}
56+
57+
func runDeleteNonInteractive(f *cmdutil.Factory, opts Options) error {
58+
err := paramCheck(opts)
59+
if err != nil {
60+
return err
61+
}
62+
63+
err = deleteTemplate(f, opts)
64+
if err != nil {
65+
return err
66+
}
67+
68+
return nil
69+
}
70+
71+
func deleteTemplate(f *cmdutil.Factory, opts Options) error {
72+
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
73+
spinner.WithColor(cmdutil.SpinnerColor),
74+
spinner.WithSuffix(" Deleting template..."),
75+
)
76+
s.Start()
77+
err := f.ApiClient.DeleteTemplate(context.Background(), opts.code)
78+
if err != nil {
79+
return err
80+
}
81+
s.Stop()
82+
83+
f.Log.Info("Delete template successfully")
84+
85+
return nil
86+
}
87+
88+
func paramCheck(opts Options) error {
89+
if opts.code == "" {
90+
return fmt.Errorf("template code is required")
91+
}
92+
93+
return nil
94+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package deploy
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/briandowns/spinner"
8+
"github.com/spf13/cobra"
9+
"github.com/zeabur/cli/internal/cmdutil"
10+
)
11+
12+
type Options struct {
13+
code string
14+
}
15+
16+
func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
17+
opts := &Options{}
18+
19+
cmd := &cobra.Command{
20+
Use: "deploy",
21+
Short: "Deploy a template",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
return runDeploy(f, opts)
24+
},
25+
}
26+
27+
cmd.Flags().StringVarP(&opts.code, "code", "c", "", "Template code")
28+
29+
return cmd
30+
}
31+
32+
func runDeploy(f *cmdutil.Factory, opts *Options) error {
33+
var err error
34+
35+
err = paramCheck(opts)
36+
if err != nil {
37+
return err
38+
}
39+
40+
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
41+
spinner.WithColor(cmdutil.SpinnerColor),
42+
spinner.WithSuffix(" Fetching template..."),
43+
)
44+
s.Start()
45+
46+
template, err := f.ApiClient.GetTemplate(context.Background(), opts.code)
47+
if err != nil {
48+
return err
49+
}
50+
51+
fmt.Printf("Template: %s\n", template.Name)
52+
53+
s.Stop()
54+
55+
return nil
56+
}
57+
58+
func paramCheck(opts *Options) error {
59+
if opts.code == "" {
60+
return fmt.Errorf("code is required")
61+
}
62+
63+
return nil
64+
}

internal/cmd/template/get/get.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package get
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/briandowns/spinner"
8+
"github.com/spf13/cobra"
9+
10+
"github.com/zeabur/cli/internal/cmdutil"
11+
)
12+
13+
type Options struct {
14+
code string
15+
}
16+
17+
func NewCmdGet(f *cmdutil.Factory) *cobra.Command {
18+
opts := Options{}
19+
20+
cmd := &cobra.Command{
21+
Use: "get",
22+
Short: "Get template by code",
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return runGet(f, opts)
25+
},
26+
}
27+
28+
cmd.Flags().StringVarP(&opts.code, "code", "c", "", "Template code")
29+
30+
return cmd
31+
}
32+
33+
func runGet(f *cmdutil.Factory, opts Options) error {
34+
if f.Interactive {
35+
return runGetInteractive(f, opts)
36+
}
37+
return runGetNonInteractive(f, opts)
38+
39+
}
40+
41+
func runGetInteractive(f *cmdutil.Factory, opts Options) error {
42+
code, err := f.Prompter.Input("Template Code: ", "")
43+
if err != nil {
44+
return err
45+
}
46+
47+
opts.code = code
48+
49+
err = getTemplate(f, opts)
50+
if err != nil {
51+
return err
52+
}
53+
54+
return nil
55+
}
56+
57+
func runGetNonInteractive(f *cmdutil.Factory, opts Options) error {
58+
err := paramCheck(opts)
59+
if err != nil {
60+
return err
61+
}
62+
63+
err = getTemplate(f, opts)
64+
if err != nil {
65+
return err
66+
}
67+
68+
return nil
69+
}
70+
71+
func getTemplate(f *cmdutil.Factory, opts Options) error {
72+
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
73+
spinner.WithColor(cmdutil.SpinnerColor),
74+
spinner.WithSuffix(" Fetching template..."),
75+
)
76+
s.Start()
77+
template, err := f.ApiClient.GetTemplate(context.Background(), opts.code)
78+
if err != nil {
79+
return err
80+
}
81+
s.Stop()
82+
83+
if template == nil || template.Code == "" {
84+
fmt.Println("Template not found")
85+
} else {
86+
f.Printer.Table([]string{"Code", "Name", "Description"}, [][]string{{template.Code, template.Name, template.Description}})
87+
}
88+
89+
return nil
90+
}
91+
92+
func paramCheck(opts Options) error {
93+
if opts.code == "" {
94+
return fmt.Errorf("template code is required")
95+
}
96+
97+
return nil
98+
}

internal/cmd/template/list/list.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package list
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/zeabur/cli/internal/cmdutil"
8+
)
9+
10+
type Options struct {
11+
}
12+
13+
func NewCmdList(f *cmdutil.Factory) *cobra.Command {
14+
opts := Options{}
15+
16+
cmd := &cobra.Command{
17+
Use: "list",
18+
Short: "List templates",
19+
Aliases: []string{"ls"},
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
return runList(f, opts)
22+
},
23+
}
24+
25+
return cmd
26+
}
27+
28+
func runList(f *cmdutil.Factory, opts Options) error {
29+
templates, err := f.ApiClient.ListAllTemplates(context.Background())
30+
if err != nil {
31+
return err
32+
}
33+
34+
f.Printer.Table(templates.Header(), templates.Rows())
35+
36+
return nil
37+
}

internal/cmd/template/template.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package template
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
6+
"github.com/zeabur/cli/internal/cmdutil"
7+
8+
templateDeleteCmd "github.com/zeabur/cli/internal/cmd/template/delete"
9+
templateDeployCmd "github.com/zeabur/cli/internal/cmd/template/deploy"
10+
templateGetCmd "github.com/zeabur/cli/internal/cmd/template/get"
11+
templateListCmd "github.com/zeabur/cli/internal/cmd/template/list"
12+
)
13+
14+
func NewCmdTemplate(f *cmdutil.Factory) *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "template",
17+
Short: "Manage templates",
18+
}
19+
20+
cmd.AddCommand(templateListCmd.NewCmdList(f))
21+
cmd.AddCommand(templateDeployCmd.NewCmdDeploy(f))
22+
cmd.AddCommand(templateGetCmd.NewCmdGet(f))
23+
cmd.AddCommand(templateDeleteCmd.NewCmdDelete(f))
24+
25+
return cmd
26+
}

pkg/api/interface.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Client interface {
1616
DeploymentAPI
1717
LogAPI
1818
GitAPI
19+
TemplateAPI
1920
}
2021

2122
type (
@@ -81,4 +82,13 @@ type (
8182
GetRepoInfo() (string, string, error)
8283
GetRepoBranchesByRepoID(repoID int) ([]string, error)
8384
}
85+
86+
TemplateAPI interface {
87+
ListTemplates(ctx context.Context, skip, limit int) (*model.TemplateConnection, error)
88+
ListAllTemplates(ctx context.Context) (model.Templates, error)
89+
GetTemplate(ctx context.Context, code string) (*model.Template, error)
90+
91+
DeployTemplate(ctx context.Context, code string, variables map[string]interface{}, repoConfigs model.RepoConfigs) (*model.Project, error)
92+
DeleteTemplate(ctx context.Context, code string) error
93+
}
8494
)

0 commit comments

Comments
 (0)