-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
113 lines (93 loc) · 2.13 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/kendfss/but"
)
const (
gitignoreUrl = "https://github.com/github/gitignore/archive/master.zip"
dataDir = ".joe-data"
)
var (
userHome, dataPath, version string
errLogger = log.New(os.Stderr, "", 0)
)
func init() {
var err error
userHome, err = os.UserHomeDir()
but.Exif(err)
dataPath = path.Join(userHome, dataDir)
}
func findGitignores() (a map[string]string, err error) {
_, err = ioutil.ReadDir(dataPath)
if err != nil {
return nil, err
}
filelist := make(map[string]string)
filepath.Walk(dataPath, func(filepath string, info os.FileInfo, err error) error {
but.Exif(err)
if strings.HasSuffix(info.Name(), ".gitignore") {
name := strings.ToLower(strings.Replace(info.Name(), ".gitignore", "", 1))
filelist[name] = filepath
}
return nil
})
return filelist, nil
}
func availableFiles() (a []string, err error) {
gitignores, err := findGitignores()
if err != nil {
return nil, err
}
availableGitignores := []string{".txt"}
for key := range gitignores {
availableGitignores = append(availableGitignores, key)
}
return availableGitignores, nil
}
func search(arg string) {
gitignores, err := findGitignores()
but.Exif(err)
for ig := range gitignores {
b, err := regexp.MatchString(arg, ig)
if err != nil {
errLogger.Println(err)
} else if b {
fmt.Println(ig)
}
}
}
func generate(args string) {
names := strings.Split(args, ",")
gitignores, err := findGitignores()
but.Exif(err)
notFound := []string{}
// output := ".DS_Store\n._*\n"
output := ""
for _, name := range names {
if filepath, ok := gitignores[strings.ToLower(name)]; ok {
bytes, err := ioutil.ReadFile(filepath)
if err == nil {
output += "#### " + name + " ####\n"
output += string(bytes)
output += "\n"
}
} else {
notFound = append(notFound, name)
}
}
if len(notFound) > 0 {
errLogger.Printf("Unsupported files: %s\n", strings.Join(notFound, ", "))
errLogger.Fatal("Run `joe ls` to see list of available gitignores.")
}
fmt.Println(output)
}
func main() {
app.Run(os.Args)
}