-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
63 lines (53 loc) · 1.1 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
package main
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/urfave/cli/v2"
)
const BASE_URL = "https://www.gitignore.io/api/"
func main() {
app := &cli.App{
Name: "tignore",
Usage: "generate .gitignore file with cli",
Action: func(c *cli.Context) error {
var tools string
for i := 0; i < c.NArg(); i++ {
tools += c.Args().Get(i) + ","
}
tools = strings.TrimRight(tools, ",")
URL := BASE_URL + tools
resp, err := http.Get(URL)
if err != nil {
return err
}
defer resp.Body.Close()
byteArray, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// 既存の.gitignoreがある場合上書きしない
_, err = os.Stat(".gitignore")
if errors.Is(err, os.ErrNotExist) {
file, err := os.Create(".gitignore")
if err != nil {
return err
}
defer file.Close()
file.Write(byteArray)
fmt.Println(".gitignore has been generated->", tools)
} else {
fmt.Println(".gitignore already exists.")
}
return nil
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}