|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | +package commands |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + CompletionCommandName = "completion" |
| 26 | + BashShell = "bash" |
| 27 | + ZshShell = "zsh" |
| 28 | + FishShell = "fish" |
| 29 | + PowerShell = "powershell" |
| 30 | +) |
| 31 | + |
| 32 | +var longText = `To enable shell autocompletion: |
| 33 | +
|
| 34 | +Bash: |
| 35 | +
|
| 36 | +$ source <(odfe-cli completion bash) |
| 37 | +
|
| 38 | +# To enable auto completion for commands for each session, execute once: |
| 39 | +Linux: |
| 40 | + $ odfe-cli completion bash > /etc/bash_completion.d/odfe-cli |
| 41 | +MacOS: |
| 42 | + $ odfe-cli completion bash > /usr/local/etc/bash_completion.d/odfe-cli |
| 43 | +
|
| 44 | +Zsh: |
| 45 | +
|
| 46 | +# If shell completion is not already enabled in your environment you will need |
| 47 | +# to enable it. You can execute the following once: |
| 48 | +
|
| 49 | +$ echo "autoload -U compinit; compinit" >> ~/.zshrc |
| 50 | +
|
| 51 | +# To enable auto completion for commands for each session, execute once: |
| 52 | +$ odfe-cli completion zsh > "${fpath[1]}/_odfe-cli" |
| 53 | +
|
| 54 | +# You will need to start a new shell for this setup to take effect. |
| 55 | +
|
| 56 | +Fish: |
| 57 | +
|
| 58 | +$ odfe-cli completion fish | source |
| 59 | +
|
| 60 | +# To enable auto completion for commands for each session, execute once: |
| 61 | +$ odfe-cli completion fish > ~/.config/fish/completions/odfe-cli.fish |
| 62 | +
|
| 63 | +Powershell: |
| 64 | +
|
| 65 | +PS> odfe-cli completion powershell | Out-String | Invoke-Expression |
| 66 | +
|
| 67 | +# To enable auto completion for commands for each session, execute once: |
| 68 | +PS> odfe-cli completion powershell > odfe-cli.ps1 |
| 69 | +# and source this file from your powershell profile. |
| 70 | +` |
| 71 | + |
| 72 | +var completionCmd = &cobra.Command{ |
| 73 | + Use: fmt.Sprintf("%s [ %s | %s | %s | %s ]", CompletionCommandName, BashShell, ZshShell, FishShell, PowerShell), |
| 74 | + Short: "Generate completion script for your shell", |
| 75 | + Long: longText, |
| 76 | + DisableFlagsInUseLine: true, |
| 77 | + ValidArgs: []string{BashShell, ZshShell, FishShell, PowerShell}, |
| 78 | + Args: cobra.ExactValidArgs(1), |
| 79 | + Run: func(cmd *cobra.Command, args []string) { |
| 80 | + var err error |
| 81 | + switch args[0] { |
| 82 | + case BashShell: |
| 83 | + err = cmd.Root().GenBashCompletion(os.Stdout) |
| 84 | + case ZshShell: |
| 85 | + err = cmd.Root().GenZshCompletion(os.Stdout) |
| 86 | + case FishShell: |
| 87 | + err = cmd.Root().GenFishCompletion(os.Stdout, true) |
| 88 | + case PowerShell: |
| 89 | + err = cmd.Root().GenPowerShellCompletion(os.Stdout) |
| 90 | + } |
| 91 | + DisplayError(err, CompletionCommandName) |
| 92 | + }, |
| 93 | +} |
| 94 | + |
| 95 | +func init() { |
| 96 | + GetRoot().AddCommand(completionCmd) |
| 97 | +} |
0 commit comments