Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 54 additions & 0 deletions cmd/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
51 changes: 51 additions & 0 deletions cmd/completion_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
39 changes: 39 additions & 0 deletions documentation/cmd/completion.md
Original file line number Diff line number Diff line change
@@ -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 |