/
main.go
83 lines (69 loc) · 1.47 KB
/
main.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"github.com/gobuffalo/packr"
"github.com/ktr0731/go-fuzzyfinder"
)
// gitignore - A struct that holds names and metadata for gitignore files
type gitignore struct {
name string
content string
}
func main() {
// packr box holds byte strings for gitignores
box := packr.NewBox("./gitignore")
// invoke a fuzzy search
gi := getGitignoreFiles(box)
idx := searchGitignores(gi)
c := []byte(gi[idx[0]].content)
// Write out the chosen gitignore
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
err = ioutil.WriteFile(dir+"/.gitignore", c, 0644)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
// getGitignoreFiles needs to pack the binaries for the gitignores, fetch them from the box
// then return them as a slice of []gitignore structs
func getGitignoreFiles(b packr.Box) []gitignore {
var g []gitignore
list := b.List()
for _, file := range list {
content, err := b.FindString(file)
if err != nil {
log.Fatal(err)
}
g = append(g, gitignore{name: file, content: string(content)})
}
return g
}
func searchGitignores(g []gitignore) []int {
idx, err := fuzzyfinder.FindMulti(
g,
func(i int) string {
return g[i].name
},
fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
if i == -1 {
return ""
}
return fmt.Sprintf(
"gitignore: %s \n---\n%s\n---",
g[i].name,
g[i].content,
)
}),
)
if err != nil {
log.Fatal(err)
}
return idx
}