Commit 60da53b3 authored by Simon Schürg's avatar Simon Schürg 🚀
Browse files

feat(completion): Enable shell completion for config login profiles

closes #5
parent 6b624fe1
......@@ -6,39 +6,48 @@ import (
"github.com/spf13/cobra"
)
// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate shell completion scripts",
Short: "Generate completion script",
Long: `To load completions:
Bash:
$ source <(actl completion bash)
$ source <(actl completion bash)
# To load completions for each session, execute once:
Linux:
# To load completions for each session, execute once:
# Linux:
$ actl completion bash > /etc/bash_completion.d/actl
MacOS:
# macOS:
$ actl completion bash > /usr/local/etc/bash_completion.d/actl
Zsh:
# If shell completion is not already enabled in your environment you will need
# to enable it. You can execute the following once:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ actl completion zsh > "${fpath[1]}/_actl"
# To load completions for each session, execute once:
$ actl completion zsh > "${fpath[1]}/_actl"
# You will need to start a new shell for this setup to take effect.
# You will need to start a new shell for this setup to take effect.
Fish:
fish:
$ actl completion fish | source
$ actl completion fish | source
# To load completions for each session, execute once:
$ actl completion fish > ~/.config/fish/completions/actl.fish
# To load completions for each session, execute once:
$ actl completion fish > ~/.config/fish/completions/actl.fish
PowerShell:
PS> actl completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> actl completion powershell > actl.ps1
# and source this file from your PowerShell profile.
`,
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
......@@ -52,10 +61,9 @@ $ actl completion fish > ~/.config/fish/completions/actl.fish
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletion(os.Stdout)
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
},
}
}}
func init() {
rootCmd.AddCommand(completionCmd)
......@@ -64,9 +72,9 @@ func init() {
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// echoCmd.PersistentFlags().String("foo", "", "A help for foo")
// completionCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// echoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
// completionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
......@@ -83,6 +83,14 @@ var loginCmd = &cobra.Command{
},
}
func GetProfileNames() []string {
keys := []string{}
for key, _ := range viper.GetStringMapString("logins") {
keys = append(keys, key)
}
return keys
}
// GetProfile loads profiles from config
func GetProfile(profileID string) Credentials {
logins := viper.GetViper().GetStringMap("logins")
......@@ -195,6 +203,9 @@ func init() {
rootCmd.AddCommand(loginCmd)
loginCmd.Flags().StringVar(&profile, "profile", "", "Use login information (profile) from actl config")
loginCmd.RegisterFlagCompletionFunc("profile", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return GetProfileNames(), cobra.ShellCompDirectiveDefault
})
loginCmd.Flags().BoolVarP(&toClipboard, "clipboard", "c", false, "Copy Access Token to system clipboard")
loginCmd.AddCommand(loginACCmd)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment