github.com/chenbh/concourse/v6@v6.4.2/fly/commands/completion.go (about)

     1  package commands
     2  
     3  import "fmt"
     4  
     5  type CompletionCommand struct {
     6  	Shell string `long:"shell" required:"true" choice:"bash" choice:"zsh"` // add more choices later
     7  }
     8  
     9  // credits:
    10  // https://godoc.org/github.com/jessevdk/go-flags#hdr-Completion
    11  // https://github.com/chenbh/concourse/v6/issues/1309#issuecomment-452893900
    12  const bashCompletionSnippet = `_fly_compl() {
    13  	args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
    14  	local IFS=$'\n'
    15  	COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
    16  	return 0
    17  }
    18  complete -F _fly_compl fly
    19  `
    20  
    21  // initial implemenation just using bashcompinit
    22  const zshCompletionSnippet = `autoload -Uz compinit && compinit
    23  autoload -Uz bashcompinit && bashcompinit
    24  ` + bashCompletionSnippet
    25  
    26  func (command *CompletionCommand) Execute([]string) error {
    27  	switch command.Shell {
    28  	case "bash":
    29  		_, err := fmt.Print(bashCompletionSnippet)
    30  		return err
    31  	case "zsh":
    32  		_, err := fmt.Print(zshCompletionSnippet)
    33  		return err
    34  	default:
    35  		// this should be unreachable
    36  		return fmt.Errorf("unknown shell %s", command.Shell)
    37  	}
    38  }