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

task: clean up unused om org select #2735

Merged
merged 2 commits into from
Mar 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
14 changes: 7 additions & 7 deletions internal/cli/default_setter_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (opts *DefaultSetterOpts) projects() (ids, names []string, err error) {
}

// Orgs fetches organizations, filtering by name.
func (opts *DefaultSetterOpts) orgs(filter string) (results *[]atlasv2.AtlasOrganization, err error) {
func (opts *DefaultSetterOpts) orgs(filter string) (results []atlasv2.AtlasOrganization, err error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reduce pointer operations

spin := newSpinner()
spin.Start()
defer spin.Stop()
Expand All @@ -134,7 +134,7 @@ func (opts *DefaultSetterOpts) orgs(filter string) (results *[]atlasv2.AtlasOrga
if orgs.GetTotalCount() > resultsLimit {
return nil, errTooManyResults
}
return orgs.Results, nil
return orgs.GetResults(), nil
}

// ProjectExists checks if the project exists and the current user has access to it.
Expand All @@ -146,7 +146,7 @@ func (opts *DefaultSetterOpts) ProjectExists(id string) bool {
}

// AskProject will try to construct a select based on fetched projects.
// If it fails or there are no projects to show we fallback to ask for project by ID.
// If it fails or there are no projects to show we fall back to ask for project by ID.
// If only one project, select it by default without prompting the user.
func (opts *DefaultSetterOpts) AskProject() error {
ids, names, err := opts.projects()
Expand Down Expand Up @@ -203,7 +203,7 @@ func (opts *DefaultSetterOpts) OrgExists(id string) bool {
}

// AskOrg will try to construct a select based on fetched organizations.
// If it fails or there are no organizations to show we fallback to ask for org by ID.
// If it fails or there are no organizations to show we fall back to ask for org by ID.
// If only one organization, select it by default without prompting the user.
func (opts *DefaultSetterOpts) AskOrg() error {
return opts.askOrgWithFilter("")
Expand Down Expand Up @@ -274,9 +274,9 @@ func (opts *DefaultSetterOpts) manualOrgID() error {
return nil
}

func (opts *DefaultSetterOpts) selectOrg(orgs *[]atlasv2.AtlasOrganization) error {
if orgs != nil && len(*orgs) == 1 {
opts.OrgID = *(*orgs)[0].Id
func (opts *DefaultSetterOpts) selectOrg(orgs []atlasv2.AtlasOrganization) error {
if len(orgs) == 1 {
opts.OrgID = orgs[0].GetId()
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/default_setter_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestDefaultOpts_Orgs(t *testing.T) {
mockStore.EXPECT().Organizations(gomock.Any()).Return(expectedOrgs, nil).Times(1)
gotOrgs, err := opts.orgs("")
require.NoError(t, err)
assert.Equal(t, expectedOrgs.Results, gotOrgs)
assert.Equal(t, expectedOrgs.GetResults(), gotOrgs)
})

t.Run("with no org", func(t *testing.T) {
Expand Down
29 changes: 4 additions & 25 deletions internal/prompt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/validate"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115007/admin"
atlas "go.mongodb.org/atlas/mongodbatlas"
)

func NewOrgIDInput() survey.Prompt {
Expand Down Expand Up @@ -88,41 +87,21 @@ func NewProfileReplaceConfirm(entry string) survey.Prompt {
}

// NewOrgSelect create a prompt to choice the organization.
func NewOrgSelect(options *[]atlasv2.AtlasOrganization) survey.Prompt {
opt := make([]string, len(*options))
for i, o := range *options {
opt[i] = *o.Id
}

return &survey.Select{
Message: "Choose a default organization:",
Options: opt,
Description: func(_ string, i int) string {
return (*options)[i].Name
},
Filter: func(filter string, _ string, i int) bool {
filter = strings.ToLower(filter)
return strings.HasPrefix(strings.ToLower((*options)[i].Name), filter) || strings.HasPrefix(*(*options)[i].Id, filter)
},
}
}

// NewOnPremOrgSelect create a prompt to choice the organization.
func NewOnPremOrgSelect(options []*atlas.Organization) survey.Prompt {
func NewOrgSelect(options []atlasv2.AtlasOrganization) survey.Prompt {
opt := make([]string, len(options))
for i, o := range options {
opt[i] = o.ID
opt[i] = o.GetId()
}

return &survey.Select{
Message: "Choose a default organization:",
Options: opt,
Description: func(_ string, i int) string {
return options[i].Name
return options[i].GetName()
},
Filter: func(filter string, _ string, i int) bool {
filter = strings.ToLower(filter)
return strings.HasPrefix(strings.ToLower(options[i].Name), filter) || strings.HasPrefix(options[i].ID, filter)
return strings.HasPrefix(strings.ToLower(options[i].GetName()), filter) || strings.HasPrefix(options[i].GetId(), filter)
},
}
}
Expand Down