diff --git a/CHANGELOG.md b/CHANGELOG.md index 70cfc0aa..a7be6df0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/cmd/context/list.go b/internal/cmd/context/list.go index 83465ca7..a61505f7 100644 --- a/internal/cmd/context/list.go +++ b/internal/cmd/context/list.go @@ -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" ) @@ -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: diff --git a/internal/cmd/context/list_test.go b/internal/cmd/context/list_test.go index 2216b9e3..cd3bf711 100644 --- a/internal/cmd/context/list_test.go +++ b/internal/cmd/context/list_test.go @@ -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" ) @@ -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) + }) + } +} diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index 657721ae..1665f65e 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -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