github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/cmd/completion.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"runtime"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/turbot/steampipe/pkg/cmdconfig"
    10  	"github.com/turbot/steampipe/pkg/constants"
    11  )
    12  
    13  func generateCompletionScriptsCmd() *cobra.Command {
    14  	var cmd = &cobra.Command{
    15  		Use:                   "completion [bash|zsh|fish]",
    16  		Args:                  cobra.ArbitraryArgs,
    17  		DisableFlagsInUseLine: true,
    18  		ValidArgs:             []string{"bash", "zsh", "fish"},
    19  		Run:                   runGenCompletionScriptsCmd,
    20  		Short:                 "Generate completion scripts",
    21  	}
    22  
    23  	cmd.ResetFlags()
    24  
    25  	cmd.SetHelpFunc(completionHelp)
    26  
    27  	cmdconfig.OnCmd(cmd).AddBoolFlag(constants.ArgHelp, false, "Help for completion", cmdconfig.FlagOptions.WithShortHand("h"))
    28  
    29  	return cmd
    30  }
    31  
    32  func includeBashHelp(base string) string {
    33  	buildUp := base
    34  	buildUp = fmt.Sprintf(`%s
    35    Bash:`, buildUp)
    36  
    37  	if runtime.GOOS == "darwin" {
    38  		buildUp = fmt.Sprintf(`%s
    39      # Load for the current session:
    40      $ source <(steampipe completion bash)
    41  		
    42      # Load for every session (requires shell restart):
    43      $ steampipe completion bash > $(brew --prefix)/etc/bash_completion.d/steampipe
    44  `, buildUp)
    45  	} else if runtime.GOOS == "linux" {
    46  		buildUp = fmt.Sprintf(`%s
    47      # Load for the current session:
    48      $ source <(steampipe completion bash)
    49  
    50      # Load for every session (requires shell restart):
    51      $ steampipe completion bash > /etc/bash_completion.d/steampipe
    52  	`, buildUp)
    53  	}
    54  
    55  	return buildUp
    56  }
    57  
    58  func includeZshHelp(base string) string {
    59  	buildUp := base
    60  
    61  	if runtime.GOOS == "darwin" {
    62  		buildUp = fmt.Sprintf(`%s
    63    Zsh:
    64      # Load for every session:
    65      $ steampipe completion zsh > "${fpath[1]}/_steampipe" && compinit
    66  `, buildUp)
    67  	}
    68  
    69  	return buildUp
    70  }
    71  
    72  func includeFishHelp(base string) string {
    73  	buildUp := base
    74  
    75  	buildUp = fmt.Sprintf(`%s
    76    fish:
    77      # Load for the current session:
    78      $ steampipe completion fish | source
    79      
    80      # Load for every session (requires shell restart):
    81      $ steampipe completion fish > ~/.config/fish/completions/steampipe.fish
    82  	`, buildUp)
    83  
    84  	return buildUp
    85  }
    86  
    87  func completionHelp(cmd *cobra.Command, _ []string) {
    88  	helpString := ""
    89  
    90  	if runtime.GOOS == "darwin" {
    91  		helpString = `
    92  Note: Completions must be enabled in your environment. Please refer to: https://steampipe.io/docs/reference/cli-args#steampipe-completion
    93  	
    94  To load completions:
    95  `
    96  	} else if runtime.GOOS == "linux" {
    97  		helpString = `
    98  To load completions:
    99  `
   100  	}
   101  
   102  	helpString = includeBashHelp(helpString)
   103  	helpString = includeZshHelp(helpString)
   104  	helpString = includeFishHelp(helpString)
   105  
   106  	fmt.Println(helpString)
   107  	fmt.Println(cmd.UsageString())
   108  }
   109  
   110  func runGenCompletionScriptsCmd(cmd *cobra.Command, args []string) {
   111  	if len(args) != 1 {
   112  		completionHelp(cmd, args)
   113  		return
   114  	}
   115  
   116  	completionFor := args[0]
   117  
   118  	switch completionFor {
   119  	case "bash":
   120  		cmd.Root().GenBashCompletionV2(os.Stdout, false)
   121  	case "zsh":
   122  		cmd.Root().GenZshCompletionNoDesc(os.Stdout)
   123  	case "fish":
   124  		cmd.Root().GenFishCompletion(os.Stdout, false)
   125  	default:
   126  		completionHelp(cmd, args)
   127  	}
   128  }