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

improvement: don't ask for projectId when serviceId is provided #169

Merged
merged 1 commit into from
Sep 8, 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
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
}
Loading