Skip to content
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

feat(deploy): Allow specifying service ID and environment ID #113

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 85 additions & 55 deletions internal/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ type Options struct {

// create will create a new service instead of selecting one if true
create bool

// specify a service ID and environment ID to deploy on

serviceID string
environmentID string
}

func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
Expand All @@ -33,6 +38,8 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
},
}

cmd.Flags().StringVar(&opts.serviceID, "service-id", "", "Service ID to redeploy on")
cmd.Flags().StringVar(&opts.environmentID, "environment-id", "", "Environment ID to redeploy on")
cmd.Flags().StringVar(&opts.name, "name", "", "Service name")
cmd.Flags().StringVar(&opts.domainName, "domain", "", "Domain name")
cmd.Flags().BoolVar(&opts.create, "create", false, "Create a new service")
Expand All @@ -41,47 +48,113 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
}

func runDeploy(f *cmdutil.Factory, opts *Options) error {
var environment *model.Environment
var service *model.Service
var err error

bytes, fileName, err := util.PackZip()
if err != nil {
return fmt.Errorf("packing zip: %w", err)
}
opts.name = fileName

if opts.serviceID == "" {
service, environment, err = selectInteractively(f, opts)
if err != nil {
return err
}
} else {
service, err = f.ApiClient.GetService(context.Background(), opts.serviceID, "", "", "")
if err != nil {
return err
}

environment, err = f.ApiClient.GetEnvironment(context.Background(), opts.environmentID)
if err != nil {
return err
}
}

s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Uploading codes to Zeabur ..."),
)
s.Start()

_, err = f.ApiClient.UploadZipToService(context.Background(), service.Project.ID, service.ID, environment.ID, bytes)
if err != nil {
return err
}
s.Stop()

domainName := opts.domainName

if domainName == "" {
fmt.Println("Service deployed successfully, you can access it via:")
fmt.Println("https://dash.zeabur.com/projects/" + service.Project.ID + "/services/" + service.ID + "?envID=" + environment.ID)
return nil
}

s = spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Creating domain ..."),
)
s.Start()

domain, err := f.ApiClient.AddDomain(context.Background(), service.ID, environment.ID, false, domainName)
if err != nil {
return err
}

fmt.Println("Domain created: ", "https://"+*domain)

s.Stop()

return nil
}

func selectInteractively(f *cmdutil.Factory, opts *Options) (*model.Service, *model.Environment, error) {
s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Fetching projects ..."),
)
s.Start()
projects, err := f.ApiClient.ListAllProjects(context.Background())
if err != nil {
return err
return nil, nil, err
}
s.Stop()

if len(projects) == 0 {
confirm, err := f.Prompter.Confirm("No projects found, would you like to create one now?", true)
confirm, err := f.Prompter.Confirm("No projects found. Would you like to create one now?", true)
if err != nil {
return err
return nil, nil, err
}
if confirm {
project, err := f.ApiClient.CreateProject(context.Background(), "default", nil)
if err != nil {
f.Log.Error("Failed to create project: ", err)
return err
return nil, nil, err
}
f.Log.Infof("Project %s created", project.Name)
f.Log.Infof("Project %s created. Run this command again to deploy on it.", project.Name)
f.Config.GetContext().SetProject(zcontext.NewBasicInfo(project.ID, project.Name))

return nil
return nil, nil, nil
}
}

f.Log.Info("Select one project to deploy your service.")

_, project, err := f.Selector.SelectProject()
if err != nil {
return err
return nil, nil, err
}

f.Config.GetContext().SetProject(zcontext.NewBasicInfo(project.ID, project.Name))

_, environment, err := f.Selector.SelectEnvironment(project.ID)
if err != nil {
return err
return nil, nil, err
}

f.Log.Info("Select one service to deploy or create a new one.")
Expand All @@ -97,15 +170,10 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {
},
})
if err != nil {
return err
return nil, nil, err
}
}

bytes, fileName, err := util.PackZip()
if err != nil {
return err
}

if service == nil {
f.Log.Info("No service found, create a new one.")

Expand All @@ -115,53 +183,15 @@ func runDeploy(f *cmdutil.Factory, opts *Options) error {
)
s.Start()

name := fileName
if opts.name != "" {
name = opts.name
}
name := opts.name

service, err = f.ApiClient.CreateEmptyService(context.Background(), project.ID, name)
if err != nil {
return err
return nil, nil, err
}

s.Stop()
}

s = spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Uploading codes to Zeabur ..."),
)
s.Start()

_, err = f.ApiClient.UploadZipToService(context.Background(), project.ID, service.ID, environment.ID, bytes)
if err != nil {
return err
}
s.Stop()

domainName := opts.domainName

if domainName == "" {
fmt.Println("Service deployed successfully, you can access it via:")
fmt.Println("https://dash.zeabur.com/projects/" + project.ID + "/services/" + service.ID + "?envID=" + environment.ID)
return nil
}

s = spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
spinner.WithColor(cmdutil.SpinnerColor),
spinner.WithSuffix(" Creating domain ..."),
)
s.Start()

domain, err := f.ApiClient.AddDomain(context.Background(), service.ID, environment.ID, false, domainName)
if err != nil {
return err
}

fmt.Println("Domain created: ", "https://"+*domain)

s.Stop()

return nil
return service, environment, nil
}
6 changes: 4 additions & 2 deletions pkg/model/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
type Service struct {
ID string `graphql:"_id"`
Name string `graphql:"name"`
//Template ServiceTemplate `graphql:"template"`
//Project *Project `graphql:"project"`
// Template ServiceTemplate `graphql:"template"`
Project *struct {
ID string `graphql:"_id"`
} `graphql:"project"`

CreatedAt time.Time `graphql:"createdAt"`

Expand Down
Loading