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

feat: add autocomplete of context in context use command #206

Merged
merged 4 commits into from
Jun 24, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `extensions delete` command
- `extensions activate` command
- `extensions deactivate` command
- `context use` autocomplete contexts with tab

### Changed

Expand Down
18 changes: 12 additions & 6 deletions internal/cmd/context/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"sort"

"github.com/mia-platform/miactl/internal/cliconfig"
"github.com/mia-platform/miactl/internal/cliconfig/api"
"github.com/mia-platform/miactl/internal/clioptions"
"github.com/spf13/cobra"
)
Expand All @@ -40,19 +41,24 @@ func ListCmd(opts *clioptions.CLIOptions) *cobra.Command {
return cmd
}

func listContexts(config *api.Config) []string {
contextNames := make([]string, 0, len(config.Contexts))
for name := range config.Contexts {
contextNames = append(contextNames, name)
}
sort.Strings(contextNames)

return contextNames
}

func printContexts(out io.Writer, locator *cliconfig.ConfigPathLocator) error {
config, err := locator.ReadConfig()
if err != nil {
return err
}

contextNames := listContexts(config)
currentContext := config.CurrentContext
contextNames := make([]string, 0, len(config.Contexts))
for name := range config.Contexts {
contextNames = append(contextNames, name)
}
sort.Strings(contextNames)

for _, key := range contextNames {
switch key {
case currentContext:
Expand Down
30 changes: 30 additions & 0 deletions internal/cmd/context/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/mia-platform/miactl/internal/cliconfig"
"github.com/mia-platform/miactl/internal/cliconfig/api"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -71,3 +72,32 @@ func TestPrintContexts(t *testing.T) {
})
}
}

func TestListContext(t *testing.T) {
testCases := map[string]struct {
config api.Config
expectedOutput []string
}{
"list contexts and sort names": {
config: api.Config{
Contexts: map[string]*api.ContextConfig{
"context3": {},
"context1": {},
"context2": {},
},
},
expectedOutput: []string{"context1", "context2", "context3"},
},
"list with nil contexts": {
config: api.Config{},
expectedOutput: []string{},
},
}

for testName, testCase := range testCases {
t.Run(testName, func(t *testing.T) {
contexts := listContexts(&testCase.config)
assert.Equal(t, testCase.expectedOutput, contexts)
})
}
}
11 changes: 11 additions & 0 deletions internal/cmd/context/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func UseCmd(opts *clioptions.CLIOptions) *cobra.Command {
fmt.Printf("Switched to context \"%s\"\n", newContext)
return nil
},
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
if len(args) > 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
locator := cliconfig.NewConfigPathLocator()
config, err := locator.ReadConfig()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return listContexts(config), cobra.ShellCompDirectiveNoFileComp
},
}

return cmd
Expand Down
Loading