github.com/beauknowssoftware/makehcl@v0.0.0-20200322000747-1b9bb1e1c008/internal/cmd/completion.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  type CompletionCommand struct {
     9  }
    10  
    11  func (c *CompletionCommand) Execute(_ []string) error {
    12  	name := os.Args[0]
    13  	fmt.Printf(`_%v() {
    14      # All arguments except the first one
    15      args=("${COMP_WORDS[@]:1:$COMP_CWORD}")
    16  
    17      # Only split on newlines
    18      local IFS=$'\n'
    19  
    20      # Call completion (note that the first element of COMP_WORDS is
    21      # the executable itself)
    22      COMPREPLY=($(GO_FLAGS_COMPLETION=1 ${COMP_WORDS[0]} "${args[@]}"))
    23      return 0
    24  }
    25  
    26  complete -F _%v %v`+"\n", name, name, name)
    27  
    28  	return nil
    29  }