diff --git a/README.md b/README.md index fee58f7..bd82ebd 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ microcks [command] [flags] | `login` | Log in to a Microcks instance using Keycloak credentials | [`login`](documentation/cmd/login.md) | | `logout` | Log out and remove authentication from a given context | [`logout`](documentation/cmd/logout.md) | | `context` | Manage CLI contexts (list, use, delete) | [`context`](documentation/cmd/context.md) | +| `completion` | Generate shell completion scripts | [`completion`](documentation/cmd/completion.md) | | `start` | Start a local Microcks instance via Docker/Podman | [`start`](documentation/cmd/start.md) | | `stop` | Stop a local Microcks instance | [`stop`](documentation/cmd/stop.md) | | `import` | Import API spec files from local filesystem | [`import`](documentation/cmd/import.md) | diff --git a/cmd/cmd.go b/cmd/cmd.go index 16df6b9..c5a10dd 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -48,6 +48,7 @@ func NewCommad() *cobra.Command { command.AddCommand(NewContextCommand(&clientOpts)) command.AddCommand(NewLoginCommand(&clientOpts)) command.AddCommand(NewLogoutCommand(&clientOpts)) + command.AddCommand(NewCompletionCommand()) defaultLocalConfigPath, err := config.DefaultLocalConfigPath() errors.CheckError(err) diff --git a/cmd/completion.go b/cmd/completion.go new file mode 100644 index 0000000..3364446 --- /dev/null +++ b/cmd/completion.go @@ -0,0 +1,54 @@ +/* + * Copyright The Microcks Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package cmd + +import ( + "github.com/microcks/microcks-cli/pkg/errors" + "github.com/spf13/cobra" +) + +func NewCompletionCommand() *cobra.Command { + var command = &cobra.Command{ + Use: "completion [bash|zsh|fish|powershell]", + Short: "Generate shell completion scripts", + Long: `Generate shell completion scripts`, + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + ValidArgs: []string{ + "bash", + "zsh", + "fish", + "powershell", + }, + Run: func(cmd *cobra.Command, args []string) { + rootCmd := cmd.Root() + var err error + + switch args[0] { + case "bash": + err = rootCmd.GenBashCompletion(cmd.OutOrStdout()) + case "zsh": + err = rootCmd.GenZshCompletion(cmd.OutOrStdout()) + case "fish": + err = rootCmd.GenFishCompletion(cmd.OutOrStdout(), true) + case "powershell": + err = rootCmd.GenPowerShellCompletion(cmd.OutOrStdout()) + } + errors.CheckError(err) + }, + } + + return command +} diff --git a/cmd/completion_test.go b/cmd/completion_test.go new file mode 100644 index 0000000..38b9e65 --- /dev/null +++ b/cmd/completion_test.go @@ -0,0 +1,51 @@ +package cmd + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewCompletionCommand(t *testing.T) { + cmd := NewCompletionCommand() + + assert.Equal(t, "completion [bash|zsh|fish|powershell]", cmd.Use) + assert.Equal(t, "Generate shell completion scripts", cmd.Short) +} + +func TestCompletionCommandRegistered(t *testing.T) { + cmd := NewCommad() + + completionCmd, _, err := cmd.Find([]string{"completion"}) + + assert.NoError(t, err) + assert.NotNil(t, completionCmd) + assert.Equal(t, "completion", completionCmd.Name()) +} + +func TestCompletionCommandGeneratesBash(t *testing.T) { + cmd := NewCommad() + buf := &bytes.Buffer{} + cmd.SetOut(buf) + cmd.SetErr(buf) + cmd.SetArgs([]string{"completion", "bash"}) + + err := cmd.Execute() + + assert.NoError(t, err) + assert.Contains(t, buf.String(), "completion for microcks") +} + +func TestCompletionCommandRejectsUnsupportedShell(t *testing.T) { + cmd := NewCommad() + buf := &bytes.Buffer{} + cmd.SetOut(buf) + cmd.SetErr(buf) + cmd.SetArgs([]string{"completion", "nushell"}) + + err := cmd.Execute() + + assert.Error(t, err) + assert.Contains(t, err.Error(), "invalid argument") +} diff --git a/documentation/cmd/completion.md b/documentation/cmd/completion.md new file mode 100644 index 0000000..1860da8 --- /dev/null +++ b/documentation/cmd/completion.md @@ -0,0 +1,39 @@ +## `microcks completion` – Generate Shell Completion Scripts +Generates shell completion scripts for bash, zsh, fish, and PowerShell. + +### Usage +```bash +microcks completion [bash|zsh|fish|powershell] +``` + +### Example +```bash +# Generate bash completion +microcks completion bash + +# Generate zsh completion +microcks completion zsh + +# Generate fish completion +microcks completion fish + +# Generate PowerShell completion +microcks completion powershell +``` + +### Options +| Flag | Description | +| ------------ | --------------- | +| `-h, --help` | help for completion | + +### Options Inherited from Parent Commands +| Flag | Description | +| ------------------------ | ------------------------------------------- | +| `--config` | Path to Microcks config file | +| `--microcks-context` | Name of the Microcks context to use | +| `--verbose` | Produce dumps of HTTP exchanges | +| `--insecure-tls` | Allow insecure HTTPS connections | +| `--caCerts` | Comma-separated paths of CA cert files | +| `--keycloakClientId` | Keycloak Realm Service Account ClientId | +| `--keycloakClientSecret` | Keycloak Realm Service Account ClientSecret | +| `--microcksURL` | Microcks API URL |