package cli

import (
	"os"

	"github.com/spf13/cobra"
)

var AutocompleteCommand = &cobra.Command{
	Use:   "autocomplete [bash|zsh|fish|powershell]",
	Short: "Generate autocompletion script",
	Long: `To load completions:

Bash:
  # Load autocompletion for the current Bash session
  $ source <(abra autocomplete bash)

  # To load autocompletion for each session, execute once:
  # Linux:
  $ abra autocomplete bash | sudo tee /etc/bash_completion.d/abra
  # macOS:
  $ abra autocomplete bash | sudo tee $(brew --prefix)/etc/bash_completion.d/abra

Zsh:
  # If shell autocompletion 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

  # To load autocompletions for each session, execute once:
  $ abra autocomplete zsh > "${fpath[1]}/_abra"

  # You will need to start a new shell for this setup to take effect.

fish:
  $ abra autocomplete fish | source

  # To load autocompletions for each session, execute once:
  $ abra autocomplete fish > ~/.config/fish/completions/abra.fish

PowerShell:
  PS> abra autocomplete powershell | Out-String | Invoke-Expression

  # To load autocompletions for every new session, run:
  PS> abra autocomplete powershell > abra.ps1
  # and source this file from your PowerShell profile.`,
	DisableFlagsInUseLine: true,
	ValidArgs:             []string{"bash", "zsh", "fish", "powershell"},
	Args:                  cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
	Run: func(cmd *cobra.Command, args []string) {
		switch args[0] {
		case "bash":
			cmd.Root().GenBashCompletion(os.Stdout)
		case "zsh":
			cmd.Root().GenZshCompletion(os.Stdout)
		case "fish":
			cmd.Root().GenFishCompletion(os.Stdout, true)
		case "powershell":
			cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
		}
	},
}