Skip to content

Commit 23f725d

Browse files
authored
Merge pull request #57 from zeabur/feat/prebuilt
feat: deploy prebuilt services
2 parents 93eadae + b3fc588 commit 23f725d

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

internal/cmd/service/deploy/deploy.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ import (
1111
)
1212

1313
type Options struct {
14-
projectID string
15-
template string
16-
itemCode string
17-
branchName string
18-
name string
19-
keyword string
20-
repoID int
14+
projectID string
15+
template string
16+
marketplaceCode string
17+
branchName string
18+
name string
19+
keyword string
20+
repoID int
2121
}
2222

2323
func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
@@ -39,7 +39,7 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
3939

4040
cmd.Flags().StringVar(&opts.name, "name", "", "Service Name")
4141
cmd.Flags().StringVar(&opts.template, "template", "", "Service template")
42-
cmd.Flags().StringVar(&opts.itemCode, "item-code", "", "Marketplace item code")
42+
cmd.Flags().StringVar(&opts.marketplaceCode, "marketplace-code", "", "Marketplace item code")
4343
cmd.Flags().IntVar(&opts.repoID, "repo-id", 0, "Git repository ID")
4444
cmd.Flags().StringVar(&opts.branchName, "branch-name", "", "Git branch name")
4545
cmd.Flags().StringVar(&opts.keyword, "keyword", "", "Git repository keyword")
@@ -63,11 +63,11 @@ func runDeployNonInteractive(f *cmdutil.Factory, opts *Options) error {
6363

6464
ctx := context.Background()
6565

66-
if opts.template == "MARKETPLACE" {
67-
opts.name = opts.itemCode
68-
service, err := f.ApiClient.CreateServiceFromMarketplace(ctx, opts.projectID, opts.name, opts.itemCode)
66+
if opts.template == "PREBUILT" {
67+
opts.name = opts.marketplaceCode
68+
service, err := f.ApiClient.CreatePrebuiltService(ctx, opts.projectID, opts.marketplaceCode)
6969
if err != nil {
70-
return fmt.Errorf("create service failed: %w", err)
70+
return fmt.Errorf("create prebuilt service failed: %w", err)
7171
}
7272

7373
f.Log.Infof("Service %s created", service.Name)
@@ -82,7 +82,7 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
8282
return err
8383
}
8484

85-
serviceTemplate, err := f.Prompter.Select("Select service template", "MARKETPLACE", []string{"MARKETPLACE", "GIT"})
85+
serviceTemplate, err := f.Prompter.Select("Select service template", "PREBUILT", []string{"PREBUILT", "GIT"})
8686
if err != nil {
8787
return err
8888
}
@@ -92,28 +92,27 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
9292
if serviceTemplate == 0 {
9393
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
9494
spinner.WithColor(cmdutil.SpinnerColor),
95-
spinner.WithSuffix(" Fetching marketplace items..."),
96-
spinner.WithFinalMSG(cmdutil.SuccessIcon+" Marketplace fetched 🌇\n"),
95+
spinner.WithSuffix(" Fetching prebuilt marketplae..."),
96+
spinner.WithFinalMSG(cmdutil.SuccessIcon+" Prebuilt marketplace fetched 🌇\n"),
9797
)
9898
s.Start()
99-
marketplaceItems, err := f.ApiClient.GetMarketplaceItems(ctx)
99+
prebuiltItems, err := f.ApiClient.GetPrebuiltItems(ctx)
100100
if err != nil {
101-
return fmt.Errorf("get marketplace items failed: %w", err)
101+
return fmt.Errorf("get prebuilt marketplace failed: %w", err)
102102
}
103103
s.Stop()
104104

105-
marketplaceItemsList := make([]string, len(marketplaceItems))
106-
for i, item := range marketplaceItems {
107-
marketplaceItemsList[i] = item.Name + " (" + item.Description + ")"
105+
prebuiltItemsList := make([]string, len(prebuiltItems))
106+
for i, item := range prebuiltItems {
107+
prebuiltItemsList[i] = item.Name + " (" + item.Description + ")"
108108
}
109109

110-
index, err := f.Prompter.Select("Select marketplace item", "", marketplaceItemsList)
110+
index, err := f.Prompter.Select("Select prebuilt item", "", prebuiltItemsList)
111111
if err != nil {
112-
return fmt.Errorf("select marketplace item failed: %w", err)
112+
return fmt.Errorf("select prebuilt item failed: %w", err)
113113
}
114114

115-
opts.itemCode = marketplaceItems[index].Code
116-
opts.name = opts.itemCode
115+
opts.marketplaceCode = prebuiltItems[index].ID
117116

118117
// use a closure to get the service name after creation
119118
serviceName := ""
@@ -131,9 +130,9 @@ func runDeployInteractive(f *cmdutil.Factory, opts *Options) error {
131130
}
132131
s.Start()
133132

134-
service, err := f.ApiClient.CreateServiceFromMarketplace(ctx, opts.projectID, opts.name, opts.itemCode)
133+
service, err := f.ApiClient.CreatePrebuiltService(ctx, opts.projectID, opts.marketplaceCode)
135134
if err != nil {
136-
return fmt.Errorf("create service failed: %w", err)
135+
return fmt.Errorf("create prebuilt service failed: %w", err)
137136
}
138137
serviceName = service.Name
139138

@@ -208,7 +207,7 @@ func paramCheck(opts *Options) error {
208207
return fmt.Errorf("please specify service template with --template")
209208
}
210209

211-
if opts.template == "MARKETPLACE" && opts.itemCode == "" {
210+
if opts.template == "PREBUILT" && opts.marketplaceCode == "" {
212211
return fmt.Errorf("please specify marketplace item code with --item-code")
213212
}
214213

pkg/api/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ type (
4747
GetService(ctx context.Context, id, ownerName, projectName, name string) (*model.Service, error)
4848
GetServiceDetailByEnvironment(ctx context.Context, id, ownerName, projectName, name, environmentID string) (*model.ServiceDetail, error)
4949
ServiceMetric(ctx context.Context, id, environmentID, metricType string, startTime, endTime time.Time) (*model.ServiceMetric, error)
50-
GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceItem, error)
50+
GetPrebuiltItems(ctx context.Context) ([]model.PrebuiltItem, error)
5151
SearchGitRepositories(ctx context.Context, keyword *string) ([]model.GitRepo, error)
5252

5353
RestartService(ctx context.Context, id string, environmentID string) error
5454
RedeployService(ctx context.Context, id string, environmentID string) error
5555
SuspendService(ctx context.Context, id string, environmentID string) error
5656
ExposeService(ctx context.Context, id string, environmentID string, projectID string, name string) (*model.TempTCPPort, error)
57-
CreateServiceFromMarketplace(ctx context.Context, projectID string, name string, itemCode string) (*model.Service, error)
57+
CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error)
5858
CreateService(ctx context.Context, projectID string, name string, repoID int, branchName string) (*model.Service, error)
5959
CreateEmptyService(ctx context.Context, projectID string, name string) (*model.Service, error)
6060
UploadZipToService(ctx context.Context, projectID string, serviceID string, environmentID string, zipBytes []byte) (*model.Service, error)

pkg/api/service.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ func (c *client) ExposeService(ctx context.Context, id string, environmentID str
243243
return &mutation.ExposeService, nil
244244
}
245245

246-
func (c *client) GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceItem, error) {
246+
func (c *client) GetPrebuiltItems(ctx context.Context) ([]model.PrebuiltItem, error) {
247247
var query struct {
248-
MarketplaceItems []model.MarketplaceItem `graphql:"marketplaceItems"`
248+
PrebuiltItems []model.PrebuiltItem `graphql:"prebuiltMarketplaceItems"`
249249
}
250250

251251
err := c.Query(ctx, &query, nil)
@@ -254,25 +254,24 @@ func (c *client) GetMarketplaceItems(ctx context.Context) ([]model.MarketplaceIt
254254
return nil, err
255255
}
256256

257-
return query.MarketplaceItems, nil
257+
return query.PrebuiltItems, nil
258258
}
259259

260-
func (c *client) CreateServiceFromMarketplace(ctx context.Context, projectID string, name string, itemCode string) (*model.Service, error) {
260+
func (c *client) CreatePrebuiltService(ctx context.Context, projectID string, marketplaceCode string) (*model.Service, error) {
261261
var mutation struct {
262-
CreateServiceFromMarketplace model.Service `graphql:"createServiceFromMarketplace(projectID: $projectID, name: $name, itemCode: $itemCode)"`
262+
CreatePrebuiltService model.Service `graphql:"createGenericService(projectID: $projectID, marketplaceCode: $marketplaceCode)"`
263263
}
264264

265265
err := c.Mutate(ctx, &mutation, V{
266-
"projectID": ObjectID(projectID),
267-
"name": name,
268-
"itemCode": itemCode,
266+
"projectID": ObjectID(projectID),
267+
"marketplaceCode": marketplaceCode,
269268
})
270269

271270
if err != nil {
272271
return nil, err
273272
}
274273

275-
return &mutation.CreateServiceFromMarketplace, nil
274+
return &mutation.CreatePrebuiltService, nil
276275
}
277276

278277
func (c *client) SearchGitRepositories(ctx context.Context, keyword *string) ([]model.GitRepo, error) {

pkg/model/service.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,12 @@ type MarketplaceItem struct {
200200
NetworkType string `graphql:"networkType"`
201201
}
202202

203+
type PrebuiltItem struct {
204+
ID string `graphql:"id"`
205+
Name string `graphql:"name"`
206+
Description string `graphql:"description"`
207+
}
208+
203209
type GitRepo struct {
204210
Name string `graphql:"name"`
205211
Owner string `graphql:"owner"`

0 commit comments

Comments
 (0)