From ccbc3ac331a105dff17f029dc8c5e29d6acdacfe Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Mon, 24 Jun 2024 09:53:42 +0200 Subject: [PATCH 1/4] feat: add autocomplete of context in context use command --- internal/cmd/context/list.go | 18 ++++++++++++------ internal/cmd/context/list_test.go | 30 ++++++++++++++++++++++++++++++ internal/cmd/context/use.go | 8 ++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) 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..14ddc556 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -39,6 +39,14 @@ func UseCmd(opts *clioptions.CLIOptions) *cobra.Command { fmt.Printf("Switched to context \"%s\"\n", newContext) return nil }, + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + locator := cliconfig.NewConfigPathLocator() + config, err := locator.ReadConfig() + if err != nil { + return nil, cobra.ShellCompDirectiveNoFileComp + } + return listContexts(config), cobra.ShellCompDirectiveNoFileComp + }, } return cmd From 132fc3504765fe3666fda4f950d4bd44846b7893 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Mon, 24 Jun 2024 14:21:30 +0200 Subject: [PATCH 2/4] fix: lint --- internal/cmd/context/use.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index 14ddc556..c90a38c8 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -39,7 +39,7 @@ func UseCmd(opts *clioptions.CLIOptions) *cobra.Command { fmt.Printf("Switched to context \"%s\"\n", newContext) return nil }, - ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + ValidArgsFunction: func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { locator := cliconfig.NewConfigPathLocator() config, err := locator.ReadConfig() if err != nil { From 6cb59a390ab08e2bcaf31531c716fc7724cf8e2b Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Mon, 24 Jun 2024 16:01:12 +0200 Subject: [PATCH 3/4] update --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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 From 3218f82b414563332e13897e911fd5444637f5b8 Mon Sep 17 00:00:00 2001 From: Davide Bianchi Date: Mon, 24 Jun 2024 17:56:46 +0200 Subject: [PATCH 4/4] fix: suggest only with 1 arg --- internal/cmd/context/use.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/cmd/context/use.go b/internal/cmd/context/use.go index c90a38c8..1665f65e 100644 --- a/internal/cmd/context/use.go +++ b/internal/cmd/context/use.go @@ -39,7 +39,10 @@ func UseCmd(opts *clioptions.CLIOptions) *cobra.Command { fmt.Printf("Switched to context \"%s\"\n", newContext) return nil }, - ValidArgsFunction: func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + 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 {