gen_completion.go

  1package main
  2
  3import (
  4	"fmt"
  5	"os"
  6	"path/filepath"
  7	"sync"
  8
  9	"github.com/spf13/cobra"
 10
 11	"github.com/MichaelMure/git-bug/commands"
 12)
 13
 14func main() {
 15	fmt.Println("Generating completion files ...")
 16
 17	tasks := map[string]func(*cobra.Command) error{
 18		"Bash":       genBash,
 19		"Fish":       genFish,
 20		"PowerShell": genPowerShell,
 21		"ZSH":        genZsh,
 22	}
 23
 24	var wg sync.WaitGroup
 25	for name, f := range tasks {
 26		wg.Add(1)
 27		go func(name string, f func(*cobra.Command) error) {
 28			defer wg.Done()
 29			root := commands.NewRootCommand()
 30			err := f(root)
 31			if err != nil {
 32				fmt.Printf("  - %s: %v\n", name, err)
 33				return
 34			}
 35			fmt.Printf("  - %s: ok\n", name)
 36		}(name, f)
 37	}
 38
 39	wg.Wait()
 40}
 41
 42func genBash(root *cobra.Command) error {
 43	cwd, err := os.Getwd()
 44	if err != nil {
 45		return err
 46	}
 47	f, err := os.Create(filepath.Join(cwd, "misc", "bash_completion", "git-bug"))
 48	if err != nil {
 49		return err
 50	}
 51	defer f.Close()
 52
 53	const patch = `
 54_git_bug() {
 55    local cur prev words cword split
 56
 57    COMPREPLY=()
 58
 59    # Call _init_completion from the bash-completion package
 60    # to prepare the arguments properly
 61    if declare -F _init_completion >/dev/null 2>&1; then
 62        _init_completion -n "=:" || return
 63    else
 64        __git-bug_init_completion -n "=:" || return
 65    fi
 66
 67	# START PATCH
 68	# replace in the array ("git","bug", ...) to ("git-bug", ...) and adjust the index in cword 
 69    words=("git-bug" "${words[@]:2}")
 70    cword=$(($cword-1))
 71	# END PATCH
 72
 73    __git-bug_debug
 74    __git-bug_debug "========= starting completion logic =========="
 75    __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
 76
 77    # The user could have moved the cursor backwards on the command-line.
 78    # We need to trigger completion from the $cword location, so we need
 79    # to truncate the command-line ($words) up to the $cword location.
 80    words=("${words[@]:0:$cword+1}")
 81    __git-bug_debug "Truncated words[*]: ${words[*]},"
 82
 83    local out directive
 84    __git-bug_get_completion_results
 85    __git-bug_process_completion_results
 86}
 87`
 88	err = root.GenBashCompletionV2(f, true)
 89	if err != nil {
 90		return err
 91	}
 92
 93	// Custom bash code to connect the git completion for "git bug" to the
 94	// git-bug completion for "git-bug"
 95	_, err = f.WriteString(patch)
 96
 97	return err
 98}
 99
100func genFish(root *cobra.Command) error {
101	cwd, err := os.Getwd()
102	if err != nil {
103		return err
104	}
105	dir := filepath.Join(cwd, "misc", "fish_completion", "git-bug")
106	return root.GenFishCompletionFile(dir, true)
107}
108
109func genPowerShell(root *cobra.Command) error {
110	cwd, err := os.Getwd()
111	if err != nil {
112		return err
113	}
114	path := filepath.Join(cwd, "misc", "powershell_completion", "git-bug")
115	return root.GenPowerShellCompletionFile(path)
116}
117
118func genZsh(root *cobra.Command) error {
119	cwd, err := os.Getwd()
120	if err != nil {
121		return err
122	}
123	path := filepath.Join(cwd, "misc", "zsh_completion", "git-bug")
124	return root.GenZshCompletionFile(path)
125}