Skip to content
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
1 change: 0 additions & 1 deletion internal/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewCmdDeploy(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "deploy",
Short: "Deploy local project to Zeabur with one command",
PreRunE: util.NeedProjectContextWhenNonInteractive(f),
RunE: func(cmd *cobra.Command, args []string) error {
return runDeploy(f, opts)
},
Expand Down
77 changes: 29 additions & 48 deletions internal/cmd/deployment/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ func NewCmdGet(f *cmdutil.Factory) *cobra.Command {
opts := &Options{}

cmd := &cobra.Command{
Use: "get",
Short: "Get deployment, if deployment-id is not specified, use serviceID/serviceName and environmentID to get the deployment",
PreRunE: util.NeedProjectContextWhenNonInteractive(f),
Use: "get",
Short: "Get deployment, if deployment-id is not specified, use serviceID/serviceName and environmentID to get the deployment",
RunE: func(cmd *cobra.Command, args []string) error {
return runGet(f, opts)
},
}

zctx := f.Config.GetContext()

cmd.Flags().StringVar(&opts.deploymentID, "deployment-id", "", "Deployment ID")
cmd.Flags().StringVar(&opts.serviceID, "service-id", zctx.GetService().GetID(), "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", zctx.GetService().GetName(), "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", zctx.GetEnvironment().GetID(), "Environment ID")
cmd.Flags().StringVar(&opts.serviceID, "service-id", "", "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", "", "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", "", "Environment ID")

return cmd
}
Expand Down Expand Up @@ -69,39 +66,39 @@ func runGetInteractive(f *cmdutil.Factory, opts *Options) error {
}

func runGetNonInteractive(f *cmdutil.Factory, opts *Options) (err error) {
if opts.deploymentID == "" && opts.environmentID == "" {
projectID := f.Config.GetContext().GetProject().GetID()
envID, resolveErr := util.ResolveEnvironmentID(f.ApiClient, projectID)
if resolveErr != nil {
return resolveErr
// If deployment ID is provided, just use it directly
if opts.deploymentID != "" {
deployment, err := getDeploymentByID(f, opts.deploymentID)
if err != nil {
return err
}
opts.environmentID = envID
f.Printer.Table(deployment.Header(), deployment.Rows())
return nil
}

if err = paramCheck(opts); err != nil {
return err
// Resolve service ID from name
if opts.serviceID == "" && opts.serviceName != "" {
service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.serviceName)
if err != nil {
return fmt.Errorf("failed to get service: %w", err)
}
opts.serviceID = service.ID
}

var deployment *model.Deployment
if opts.serviceID == "" {
return errors.New("--deployment-id or --service-id/--service-name is required")
}

// If deployment id is provided, get deployment by deployment id
if opts.deploymentID != "" {
deployment, err = getDeploymentByID(f, opts.deploymentID)
} else {
// or, get deployment by service id and environment id

// If service id is not provided, get service id by service name
if opts.serviceID == "" {
var service *model.Service
if service, err = util.GetServiceByName(f.Config, f.ApiClient, opts.serviceName); err != nil {
return fmt.Errorf("failed to get service: %w", err)
} else {
opts.serviceID = service.ID
}
// Resolve environment from service's project
if opts.environmentID == "" {
envID, err := util.ResolveEnvironmentIDByServiceID(f.ApiClient, opts.serviceID)
if err != nil {
return err
}
deployment, err = getDeploymentByServiceAndEnvironment(f, opts.serviceID, opts.environmentID)
opts.environmentID = envID
}

deployment, err := getDeploymentByServiceAndEnvironment(f, opts.serviceID, opts.environmentID)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,19 +129,3 @@ func getDeploymentByServiceAndEnvironment(f *cmdutil.Factory, serviceID, environ

return deployment, nil
}

func paramCheck(opts *Options) error {
if opts.deploymentID != "" {
return nil
}

if opts.serviceID == "" && opts.serviceName == "" {
return errors.New("when deployment-id is not specified, service-id or service-name is required")
}

if opts.environmentID == "" {
return errors.New("when deployment-id is not specified, env-id is required")
}

return nil
}
48 changes: 16 additions & 32 deletions internal/cmd/deployment/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

type Options struct {
// todo: support service name
serviceID string
serviceName string
environmentID string
Expand All @@ -25,17 +24,14 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
Use: "list",
Short: "List deployments",
Aliases: []string{"ls"},
PreRunE: util.NeedProjectContextWhenNonInteractive(f),
RunE: func(cmd *cobra.Command, args []string) error {
return runList(f, opts)
},
}

zctx := f.Config.GetContext()

cmd.Flags().StringVar(&opts.serviceID, "service-id", zctx.GetService().GetID(), "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", zctx.GetService().GetName(), "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", zctx.GetEnvironment().GetID(), "Environment ID")
cmd.Flags().StringVar(&opts.serviceID, "service-id", "", "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", "", "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", "", "Environment ID")

return cmd
}
Expand Down Expand Up @@ -65,26 +61,26 @@ func runListInteractive(f *cmdutil.Factory, opts *Options) error {
}

func runListNonInteractive(f *cmdutil.Factory, opts *Options) error {
if opts.environmentID == "" {
projectID := f.Config.GetContext().GetProject().GetID()
envID, err := util.ResolveEnvironmentID(f.ApiClient, projectID)
// Resolve service ID from name
if opts.serviceID == "" && opts.serviceName != "" {
service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.serviceName)
if err != nil {
return err
return fmt.Errorf("failed to get service: %w", err)
}
opts.environmentID = envID
opts.serviceID = service.ID
}

if err := paramCheck(opts); err != nil {
return err
if opts.serviceID == "" {
return errors.New("--service-id or --service-name is required")
}

// If service id is not provided, get service id by service name
if opts.serviceID == "" {
if service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.serviceName); err != nil {
return fmt.Errorf("failed to get service: %w", err)
} else {
opts.serviceID = service.ID
// Resolve environment from service's project
if opts.environmentID == "" {
envID, err := util.ResolveEnvironmentIDByServiceID(f.ApiClient, opts.serviceID)
if err != nil {
return err
}
opts.environmentID = envID
}

deployments, err := f.ApiClient.ListAllDeployments(context.Background(), opts.serviceID, opts.environmentID)
Expand All @@ -101,15 +97,3 @@ func runListNonInteractive(f *cmdutil.Factory, opts *Options) error {

return nil
}

func paramCheck(opts *Options) error {
if opts.serviceID == "" && opts.serviceName == "" {
return errors.New("service-id or service-name is required")
}

if opts.environmentID == "" {
return errors.New("environment is required")
}

return nil
}
48 changes: 14 additions & 34 deletions internal/cmd/deployment/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,18 @@ func NewCmdLog(f *cmdutil.Factory) *cobra.Command {
opts := &Options{}

cmd := &cobra.Command{
Use: "log",
Short: "Get deployment logs, if deployment-id is not specified, use serviceID/serviceName and environmentID to get the deployment",
PreRunE: util.NeedProjectContextWhenNonInteractive(f),
Use: "log",
Short: "Get deployment logs, if deployment-id is not specified, use serviceID/serviceName and environmentID to get the deployment",
RunE: func(cmd *cobra.Command, args []string) error {
return runLog(f, opts)
},
}

zctx := f.Config.GetContext()

cmd.Flags().StringVar(&opts.projectID, "project-id", zctx.GetProject().GetID(), "Project ID")
cmd.Flags().StringVar(&opts.projectID, "project-id", "", "Project ID")
cmd.Flags().StringVar(&opts.deploymentID, "deployment-id", "", "Deployment ID")
cmd.Flags().StringVar(&opts.serviceID, "service-id", zctx.GetService().GetID(), "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", zctx.GetService().GetName(), "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", zctx.GetEnvironment().GetID(), "Environment ID")
cmd.Flags().StringVar(&opts.serviceID, "service-id", "", "Service ID")
cmd.Flags().StringVar(&opts.serviceName, "service-name", "", "Service Name")
cmd.Flags().StringVar(&opts.environmentID, "env-id", "", "Environment ID")
cmd.Flags().StringVarP(&opts.logType, "type", "t", logTypeRuntime, "Log type, runtime or build")
cmd.Flags().BoolVarP(&opts.watch, "watch", "w", false, "Watch logs")

Expand All @@ -63,13 +60,8 @@ func runLog(f *cmdutil.Factory, opts *Options) error {
}

func runLogInteractive(f *cmdutil.Factory, opts *Options) error {
zctx := f.Config.GetContext()

if opts.projectID == "" {
opts.projectID = zctx.GetProject().GetID()
}

if opts.deploymentID == "" {
zctx := f.Config.GetContext()
_, err := f.ParamFiller.ServiceByNameWithEnvironment(fill.ServiceByNameWithEnvironmentOptions{
ProjectCtx: zctx,
ServiceID: &opts.serviceID,
Expand All @@ -95,8 +87,7 @@ func runLogNonInteractive(f *cmdutil.Factory, opts *Options) (err error) {
opts.serviceID = service.ID
}

// When serviceID is available, always resolve projectID and environmentID from the service
// instead of relying on context (which may point to a different project).
// When serviceID is available, resolve projectID and environmentID from the service
if opts.serviceID != "" {
service, err := f.ApiClient.GetService(context.Background(), opts.serviceID, "", "", "")
if err != nil {
Expand All @@ -105,24 +96,13 @@ func runLogNonInteractive(f *cmdutil.Factory, opts *Options) (err error) {
if service.Project != nil {
opts.projectID = service.Project.ID
}
envID, resolveErr := util.ResolveEnvironmentID(f.ApiClient, opts.projectID)
if resolveErr != nil {
return resolveErr
}
opts.environmentID = envID
}

// Fallback: resolve environmentID from context project if still empty
if opts.deploymentID == "" && opts.environmentID == "" {
projectID := opts.projectID
if projectID == "" {
projectID = f.Config.GetContext().GetProject().GetID()
}
envID, resolveErr := util.ResolveEnvironmentID(f.ApiClient, projectID)
if resolveErr != nil {
return resolveErr
if opts.environmentID == "" {
envID, resolveErr := util.ResolveEnvironmentID(f.ApiClient, opts.projectID)
if resolveErr != nil {
return resolveErr
}
opts.environmentID = envID
}
opts.environmentID = envID
}

if err = paramCheck(opts); err != nil {
Expand Down
44 changes: 28 additions & 16 deletions internal/cmd/domain/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ type Options struct {

func NewCmdCreateDomain(f *cmdutil.Factory) *cobra.Command {
opts := &Options{}
zctx := f.Config.GetContext()

cmd := &cobra.Command{
Use: "create",
Short: "create a domain",
Long: `Create a domain for a service`,
PreRunE: util.RunEChain(
util.NeedProjectContextWhenNonInteractive(f),
util.DefaultIDNameByContext(zctx.GetService(), &opts.id, &opts.name),
util.DefaultIDByContext(zctx.GetEnvironment(), &opts.environmentID),
),
RunE: func(cmd *cobra.Command, args []string) error {
return runCreateDomain(f, opts)
},
Expand Down Expand Up @@ -93,7 +87,17 @@ func runCreateDomainInteractive(f *cmdutil.Factory, opts *Options) error {
opts.domainName = domainInput
}

project, err := f.ApiClient.GetProject(context.Background(), zctx.GetProject().GetID(), "", "")
// Get project from the service to check domain availability
service, err := f.ApiClient.GetService(context.Background(), opts.id, "", "", "")
if err != nil {
return fmt.Errorf("get service failed: %w", err)
}
projectID := ""
if service.Project != nil {
projectID = service.Project.ID
}

project, err := f.ApiClient.GetProject(context.Background(), projectID, "", "")
if err != nil {
return err
}
Expand Down Expand Up @@ -146,16 +150,24 @@ func runCreateDomainInteractive(f *cmdutil.Factory, opts *Options) error {
}

func runCreateDomainNonInteractive(f *cmdutil.Factory, opts *Options) error {
zctx := f.Config.GetContext()
if opts.id == "" && opts.name != "" {
service, err := util.GetServiceByName(f.Config, f.ApiClient, opts.name)
if err != nil {
return err
}
opts.id = service.ID
}

if _, err := f.ParamFiller.ServiceByNameWithEnvironment(fill.ServiceByNameWithEnvironmentOptions{
ProjectCtx: zctx,
ServiceID: &opts.id,
ServiceName: &opts.name,
EnvironmentID: &opts.environmentID,
CreateNew: false,
}); err != nil {
return err
if opts.id == "" {
return fmt.Errorf("--id or --name is required")
}

if opts.environmentID == "" {
envID, err := util.ResolveEnvironmentIDByServiceID(f.ApiClient, opts.id)
if err != nil {
return err
}
opts.environmentID = envID
}

s := spinner.New(cmdutil.SpinnerCharSet, cmdutil.SpinnerInterval,
Expand Down
Loading