Skip to content

Commit 8957bc6

Browse files
add shell completion command
Signed-off-by: puneeth_aditya_5656 <myakampuneeth@gmail.com>
1 parent c01f511 commit 8957bc6

5 files changed

Lines changed: 132 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ microcks [command] [flags]
6666
| `login` | Log in to a Microcks instance using Keycloak credentials | [`login`](documentation/cmd/login.md) |
6767
| `logout` | Log out and remove authentication from a given context | [`logout`](documentation/cmd/logout.md) |
6868
| `context` | Manage CLI contexts (list, use, delete) | [`context`](documentation/cmd/context.md) |
69+
| `completion` | Generate shell completion scripts | [`completion`](documentation/cmd/completion.md) |
6970
| `start` | Start a local Microcks instance via Docker/Podman | [`start`](documentation/cmd/start.md) |
7071
| `stop` | Stop a local Microcks instance | [`stop`](documentation/cmd/stop.md) |
7172
| `import` | Import API spec files from local filesystem | [`import`](documentation/cmd/import.md) |

cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func NewCommad() *cobra.Command {
4848
command.AddCommand(NewContextCommand(&clientOpts))
4949
command.AddCommand(NewLoginCommand(&clientOpts))
5050
command.AddCommand(NewLogoutCommand(&clientOpts))
51+
command.AddCommand(NewCompletionCommand())
5152

5253
defaultLocalConfigPath, err := config.DefaultLocalConfigPath()
5354
errors.CheckError(err)

cmd/completion.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright The Microcks Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"fmt"
20+
21+
"github.com/microcks/microcks-cli/pkg/errors"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
func NewCompletionCommand() *cobra.Command {
26+
var command = &cobra.Command{
27+
Use: "completion [bash|zsh|fish|powershell]",
28+
Short: "Generate shell completion scripts",
29+
Long: `Generate shell completion scripts`,
30+
Args: cobra.ExactArgs(1),
31+
Run: func(cmd *cobra.Command, args []string) {
32+
rootCmd := cmd.Root()
33+
var err error
34+
35+
switch args[0] {
36+
case "bash":
37+
err = rootCmd.GenBashCompletion(cmd.OutOrStdout())
38+
case "zsh":
39+
err = rootCmd.GenZshCompletion(cmd.OutOrStdout())
40+
case "fish":
41+
err = rootCmd.GenFishCompletion(cmd.OutOrStdout(), true)
42+
case "powershell":
43+
err = rootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
44+
default:
45+
err = fmt.Errorf("unsupported shell %q", args[0])
46+
}
47+
errors.CheckError(err)
48+
},
49+
}
50+
51+
return command
52+
}

cmd/completion_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestNewCompletionCommand(t *testing.T) {
11+
cmd := NewCompletionCommand()
12+
13+
assert.Equal(t, "completion [bash|zsh|fish|powershell]", cmd.Use)
14+
assert.Equal(t, "Generate shell completion scripts", cmd.Short)
15+
}
16+
17+
func TestCompletionCommandRegistered(t *testing.T) {
18+
cmd := NewCommad()
19+
20+
completionCmd, _, err := cmd.Find([]string{"completion"})
21+
22+
assert.NoError(t, err)
23+
assert.NotNil(t, completionCmd)
24+
assert.Equal(t, "completion", completionCmd.Name())
25+
}
26+
27+
func TestCompletionCommandGeneratesBash(t *testing.T) {
28+
cmd := NewCommad()
29+
buf := &bytes.Buffer{}
30+
cmd.SetOut(buf)
31+
cmd.SetErr(buf)
32+
cmd.SetArgs([]string{"completion", "bash"})
33+
34+
err := cmd.Execute()
35+
36+
assert.NoError(t, err)
37+
assert.Contains(t, buf.String(), "completion for microcks")
38+
}

documentation/cmd/completion.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## `microcks completion` - Generate shell completion scripts
2+
3+
Generates shell completion scripts for supported shells.
4+
5+
### Usage
6+
```bash
7+
microcks completion [bash|zsh|fish|powershell]
8+
```
9+
10+
### Examples
11+
```bash
12+
# Generate bash completion
13+
microcks completion bash
14+
15+
# Generate zsh completion
16+
microcks completion zsh
17+
18+
# Generate fish completion
19+
microcks completion fish
20+
21+
# Generate PowerShell completion
22+
microcks completion powershell
23+
```
24+
25+
### Options
26+
| Flag | Description |
27+
| ------------ | --------------- |
28+
| `-h, --help` | help for completion |
29+
30+
### Options Inherited from Parent Commands
31+
| Flag | Description |
32+
| ------------------------ | ------------------------------------------- |
33+
| `--config` | Path to Microcks config file |
34+
| `--microcks-context` | Name of the Microcks context to use |
35+
| `--verbose` | Produce dumps of HTTP exchanges |
36+
| `--insecure-tls` | Allow insecure HTTPS connections |
37+
| `--caCerts` | Comma-separated paths of CA cert files |
38+
| `--keycloakClientId` | Keycloak Realm Service Account ClientId |
39+
| `--keycloakClientSecret` | Keycloak Realm Service Account ClientSecret |
40+
| `--microcksURL` | Microcks API URL |

0 commit comments

Comments
 (0)