summaryrefslogtreecommitdiff
path: root/makeAutocompleteFiles.go
blob: 647abe3dfd1ee70da52097abb16ff57fcd655eb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package prep

// This is where the actual autocomplete happens
// lots of the fun magic is in here

import (
	"fmt"
	"os"
	"path/filepath"

	"go.wit.com/lib/config"
	"go.wit.com/log"
)

// makes a autocomplete file for your command
func makeAutocompleteFiles(argname string) {
	fmt.Println(makeBashCompletionText2(argname))

	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Printf("# %v\n", err)
		os.Exit(0)
	}
	filename := filepath.Join(homeDir, ".local/share/bash-completion/completions", argname)
	if config.Exists(filename) {
		log.Fprintln(os.Stderr, "# file already exists", filename)
		// os.Exit(0)
	}
	basedir, _ := filepath.Split(filename)
	if !config.IsDir(basedir) {
		os.MkdirAll(basedir, os.ModePerm)
	}

	if f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
		f.Write([]byte(makeBashCompletionText2(argname)))
		f.Close()
	} else {
		log.Fprintln(os.Stderr, "# open file error", filename, err)
	}
	os.Exit(0)
}