/
chtignore.go
175 lines (145 loc) · 3.73 KB
/
chtignore.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"encoding/json"
"fmt"
"github.com/codegangsta/cli"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strings"
"sync"
"unicode"
)
// VERSION injected at compile time by goxc (see https://github.com/laher/goxc/wiki/versioning#version-number-interpolation)
var VERSION = "unknown-snapshot"
var logger = log.New(os.Stderr, "", 0)
func main() {
app(os.Args, os.Stdout)
}
func app(args []string, output io.Writer) {
app := cli.NewApp()
app.Name = "chtignore"
app.Usage = "print .gitignore templates in standard output"
app.ArgsUsage = "template"
app.Action = printTemplates
app.Version = VERSION
app.Commands = []cli.Command{
{
Name: "list",
Usage: "list available templates",
Action: list,
},
}
app.Writer = output
app.Run(args)
}
func printTemplates(c *cli.Context) {
args := c.Args()
output := c.App.Writer
if len(args) == 0 {
cli.ShowAppHelp(c)
}
var wg sync.WaitGroup
contents := make(chan string)
for _, candidate := range args {
wg.Add(1)
go func(candidate string) {
defer wg.Done()
candidate = upperFirstChar(candidate)
content := tryGetTemplate(candidate)
if content != "" {
contents <- fmt.Sprintf("# %s\n%s", candidate, content)
}
}(candidate)
}
go func() {
wg.Wait()
close(contents)
}()
for content := range contents {
fmt.Fprintln(output, content)
}
}
func list(c *cli.Context) {
fmt.Fprintln(c.App.Writer, listTemplates())
}
func tryGetTemplate(template string) string {
var resp *http.Response
if template == "JetBrains-build" {
resp = get("https://raw.githubusercontent.com/github/gitignore/38d6cac990a82a1f7814571634e08295086763b5/Global/JetBrains.gitignore")
} else {
resp = get(fmt.Sprintf("https://raw.githubusercontent.com/github/gitignore/master/%s.gitignore", template))
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
resp = get(fmt.Sprintf("https://raw.githubusercontent.com/github/gitignore/master/Global/%s.gitignore", template))
}
if resp.StatusCode != http.StatusOK {
logger.Fatal("Cannot find a template for: ", template)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Fatal(err)
}
return string(body)
}
func get(url string) (resp *http.Response) {
resp, err := http.Get(url)
if err != nil {
logger.Fatal(err)
}
return resp
}
func upperFirstChar(str string) string {
a := []rune(str)
firstChar := a[0]
if unicode.IsUpper(firstChar) {
return str
}
a[0] = unicode.ToUpper(firstChar)
return string(a)
}
func listTemplates() string {
templates := make([]string, 0)
templates = getAndAppend(templates, "https://api.github.com/repos/github/gitignore/contents/")
templates = getAndAppend(templates, "https://api.github.com/repos/github/gitignore/contents/Global")
templates = append(templates, "JetBrains-build")
sort.Strings(templates)
return strings.Join(templates, ", ")
}
func getAndAppend(templates []string, url string) []string {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logger.Fatal(err)
}
req.Header.Add("Accept", "application/vnd.github.v3+json")
resp, err := client.Do(req)
if err != nil {
logger.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logger.Fatal("Cannot list templates: ", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Fatal(err)
}
result := make([]gitIgnoreTemplate, 0)
if err := json.Unmarshal(body, &result); err != nil {
logger.Fatal(err)
}
for _, template := range result {
if template.Name != "" && strings.Contains(template.Name, ".gitignore") {
templates = append(templates, strings.Replace(template.Name, ".gitignore", "", 1))
}
}
return templates
}
type gitIgnoreTemplate struct {
Name string `json:"name"`
}