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
3 changes: 3 additions & 0 deletions cmd/select/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func AccountCmd() *cobra.Command {
cmd.Flags().StringVarP(&opts.Account, "account", "a", "", "Account to select")
cmd.Flags().StringVar(&opts.Host, "host", "", "Pangolin host where account is located")

_ = cmd.RegisterFlagCompletionFunc("account", completeAccountFlag)
_ = cmd.RegisterFlagCompletionFunc("host", completeHostFlag)

return cmd
}

Expand Down
38 changes: 38 additions & 0 deletions cmd/select/account/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package account

import (
"maps"
"slices"
"strings"

"github.com/fosrl/cli/internal/config"
"github.com/spf13/cobra"
)

func completeAccountFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
accountStore := config.AccountStoreFromContext(cmd.Context())

candidateSet := make(map[string]struct{})

for _, v := range accountStore.Accounts {
if strings.HasPrefix(v.Email, toComplete) {
candidateSet[v.Email] = struct{}{}
}
}

return slices.Collect(maps.Keys(candidateSet)), cobra.ShellCompDirectiveNoFileComp
}

func completeHostFlag(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
accountStore := config.AccountStoreFromContext(cmd.Context())

candidateSet := make(map[string]struct{})

for _, v := range accountStore.Accounts {
if strings.HasPrefix(v.Host, toComplete) {
candidateSet[v.Host] = struct{}{}
}
}

return slices.Collect(maps.Keys(candidateSet)), cobra.ShellCompDirectiveNoFileComp
}
34 changes: 34 additions & 0 deletions cmd/select/org/completion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org

import (
"fmt"
"strings"

"github.com/fosrl/cli/internal/api"
"github.com/fosrl/cli/internal/config"
"github.com/spf13/cobra"
)

func completeOrgID(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
apiClient := api.FromContext(cmd.Context())
accountStore := config.AccountStoreFromContext(cmd.Context())

activeAccount, err := accountStore.ActiveAccount()
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

orgsResponse, err := apiClient.ListUserOrgs(activeAccount.UserID)
if err != nil {
return []string{}, cobra.ShellCompDirectiveNoFileComp
}

var candidates []string
for _, org := range orgsResponse.Orgs {
if strings.HasPrefix(org.OrgID, toComplete) {
candidates = append(candidates, fmt.Sprintf("%s\t%s", org.OrgID, org.Name))
}
}

return candidates, cobra.ShellCompDirectiveNoFileComp
}
2 changes: 2 additions & 0 deletions cmd/select/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func OrgCmd() *cobra.Command {

cmd.Flags().StringVar(&opts.OrgID, "org", "", "Organization `ID` to select")

_ = cmd.RegisterFlagCompletionFunc("org", completeOrgID)

return cmd
}

Expand Down