|
| 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 | +} |
0 commit comments