diff --git a/src/cmd/scope/scopeProject.go b/src/cmd/scope/scopeProject.go index 883eee09..9aa93c7e 100644 --- a/src/cmd/scope/scopeProject.go +++ b/src/cmd/scope/scopeProject.go @@ -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)) } diff --git a/src/cmd/scope/scopeService.go b/src/cmd/scope/scopeService.go index 6c5492a2..e3cd4ecf 100644 --- a/src/cmd/scope/scopeService.go +++ b/src/cmd/scope/scopeService.go @@ -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( @@ -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) diff --git a/src/cmdBuilder/buildCobraCmd.go b/src/cmdBuilder/buildCobraCmd.go index a4dae652..99be8208 100644 --- a/src/cmdBuilder/buildCobraCmd.go +++ b/src/cmdBuilder/buildCobraCmd.go @@ -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 { diff --git a/src/cmdBuilder/cmd.go b/src/cmdBuilder/cmd.go index adcb0110..4d4a7f60 100644 --- a/src/cmdBuilder/cmd.go +++ b/src/cmdBuilder/cmd.go @@ -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 { diff --git a/src/cmdBuilder/createRunFunc.go b/src/cmdBuilder/createRunFunc.go index 486839e6..ba18eaa3 100644 --- a/src/cmdBuilder/createRunFunc.go +++ b/src/cmdBuilder/createRunFunc.go @@ -3,7 +3,6 @@ package cmdBuilder import ( "fmt" "os" - "slices" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -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) } } @@ -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 -}