Skip to content

Commit

Permalink
improvement: don't ask for projectId when serviceId is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
l-hellmann committed Sep 8, 2024
1 parent debb068 commit 5deca2e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 38 deletions.
4 changes: 0 additions & 4 deletions src/cmd/scope/scopeProject.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type project struct {

const ProjectArgName = "projectId"

func (p *project) GetParent() cmdBuilder.ScopeLevel {
return nil
}

func (p *project) AddCommandFlags(cmd *cmdBuilder.Cmd) {
cmd.StringFlag(ProjectArgName, "", i18n.T(i18n.ProjectIdFlag))
}
Expand Down
28 changes: 16 additions & 12 deletions src/cmd/scope/scopeService.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,19 @@ type service struct {
parent cmdBuilder.ScopeLevel
}

func (s *service) GetParent() cmdBuilder.ScopeLevel {
return s.parent
}

const ServiceArgName = "serviceIdOrName"
const serviceFlagName = "serviceId"

func (s *service) AddCommandFlags(cmd *cmdBuilder.Cmd) {
cmd.StringFlag(serviceFlagName, "", i18n.T(i18n.ServiceIdFlag))
s.parent.AddCommandFlags(cmd)
}

func (s *service) LoadSelectedScope(ctx context.Context, _ *cmdBuilder.Cmd, cmdData *cmdBuilder.LoggedUserCmdData) error {
func (s *service) LoadSelectedScope(ctx context.Context, cmd *cmdBuilder.Cmd, cmdData *cmdBuilder.LoggedUserCmdData) error {
infoText := i18n.SelectedService
var service *entity.Service
var err error

if serviceIdOrName, exists := cmdData.Args[ServiceArgName]; exists {
service, err = repository.GetServiceByIdOrName(ctx, cmdData.RestApiClient, cmdData.Project.ID, serviceIdOrName[0])
if err != nil {
return err
}
}

// service id is passed as a flag
if serviceId := cmdData.Params.GetString(serviceFlagName); serviceId != "" {
service, err = repository.GetServiceById(
Expand All @@ -63,6 +53,20 @@ func (s *service) LoadSelectedScope(ctx context.Context, _ *cmdBuilder.Cmd, cmdD
}
}

// now we have to load project, because we need projectId going forwards
if service == nil {
if err := s.parent.LoadSelectedScope(ctx, cmd, cmdData); err != nil {
return err
}
}

if serviceIdOrName, exists := cmdData.Args[ServiceArgName]; exists && service == nil {
service, err = repository.GetServiceByIdOrName(ctx, cmdData.RestApiClient, cmdData.Project.ID, serviceIdOrName[0])
if err != nil {
return err
}
}

// interactive selector of service
if service == nil {
service, err = uxHelpers.PrintServiceSelector(ctx, cmdData.UxBlocks, cmdData.RestApiClient, *cmdData.Project)
Expand Down
4 changes: 2 additions & 2 deletions src/cmdBuilder/buildCobraCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func buildCobraCmd(
}
cobraCmd.Use = strings.Join(append([]string{cmd.use}, argNames...), " ")

for _, dep := range getScopeListFromRoot(cmd.scopeLevel) {
dep.AddCommandFlags(cmd)
if cmd.scopeLevel != nil {
cmd.scopeLevel.AddCommandFlags(cmd)
}

for _, flag := range cmd.flags {
Expand Down
1 change: 0 additions & 1 deletion src/cmdBuilder/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type guestRunFunc func(ctx context.Context, cmdData *GuestCmdData) error
type ScopeLevel interface {
AddCommandFlags(*Cmd)
LoadSelectedScope(ctx context.Context, cmd *Cmd, cmdData *LoggedUserCmdData) error
GetParent() ScopeLevel
}

type Cmd struct {
Expand Down
22 changes: 3 additions & 19 deletions src/cmdBuilder/createRunFunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmdBuilder
import (
"fmt"
"os"
"slices"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -122,12 +121,12 @@ func createCmdRunFunc(

cmdData.RestApiClient = zeropsRestApiClient.NewAuthorizedClient(token, "https://"+storedData.RegionData.Address)

for _, dep := range getScopeListFromRoot(cmd.scopeLevel) {
err := dep.LoadSelectedScope(ctx, cmd, cmdData)
if err != nil {
if cmd.scopeLevel != nil {
if err := cmd.scopeLevel.LoadSelectedScope(ctx, cmd, cmdData); err != nil {
return err
}
}

return cmd.loggedUserRunFunc(ctx, cmdData)
}
}
Expand Down Expand Up @@ -170,18 +169,3 @@ func convertArgs(cmd *Cmd, args []string) (map[string][]string, error) {

return argsMap, nil
}

func getScopeListFromRoot(dep ScopeLevel) []ScopeLevel {
var list []ScopeLevel
for {
if dep == nil {
break
}
list = append(list, dep)
dep = dep.GetParent()
}

slices.Reverse(list)

return list
}

0 comments on commit 5deca2e

Please sign in to comment.