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", "completion", "bash", "git-bug"))
48 if err != nil {
49 return err
50 }
51 defer f.Close()
52
53 const patch = `
54# Custom bash code to connect the git completion for "git bug" to the
55# git-bug completion for "git-bug"
56_git_bug() {
57 local cur prev words cword split
58
59 COMPREPLY=()
60
61 # Call _init_completion from the bash-completion package
62 # to prepare the arguments properly
63 if declare -F _init_completion >/dev/null 2>&1; then
64 _init_completion -n "=:" || return
65 else
66 __git-bug_init_completion -n "=:" || return
67 fi
68
69 # START PATCH
70 # replace in the array ("git","bug", ...) to ("git-bug", ...) and adjust the index in cword
71 words=("git-bug" "${words[@]:2}")
72 cword=$(($cword-1))
73 # END PATCH
74
75 __git-bug_debug
76 __git-bug_debug "========= starting completion logic =========="
77 __git-bug_debug "cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}, cword is $cword"
78
79 # The user could have moved the cursor backwards on the command-line.
80 # We need to trigger completion from the $cword location, so we need
81 # to truncate the command-line ($words) up to the $cword location.
82 words=("${words[@]:0:$cword+1}")
83 __git-bug_debug "Truncated words[*]: ${words[*]},"
84
85 local out directive
86 __git-bug_get_completion_results
87 __git-bug_process_completion_results
88}
89`
90 err = root.GenBashCompletionV2(f, true)
91 if err != nil {
92 return err
93 }
94
95 // Custom bash code to connect the git completion for "git bug" to the
96 // git-bug completion for "git-bug"
97 _, err = f.WriteString(patch)
98
99 return err
100}
101
102func genFish(root *cobra.Command) error {
103 cwd, err := os.Getwd()
104 if err != nil {
105 return err
106 }
107 dir := filepath.Join(cwd, "misc", "completion", "fish", "git-bug")
108 return root.GenFishCompletionFile(dir, true)
109}
110
111func genPowerShell(root *cobra.Command) error {
112 cwd, err := os.Getwd()
113 if err != nil {
114 return err
115 }
116 path := filepath.Join(cwd, "misc", "completion", "powershell", "git-bug")
117 return root.GenPowerShellCompletionFile(path)
118}
119
120func genZsh(root *cobra.Command) error {
121 cwd, err := os.Getwd()
122 if err != nil {
123 return err
124 }
125 path := filepath.Join(cwd, "misc", "completion", "zsh", "git-bug")
126 return root.GenZshCompletionFile(path)
127}