-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (89 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
package main
import (
"log"
"os"
"github.com/urfave/cli"
"github.com/whoshuu/ignoreit/generate"
"github.com/whoshuu/ignoreit/spec"
)
const (
configFilename = ".ignoreit.yml"
ignoreFilename = ".gitignore"
defaultRepo = "github/gitignore"
defaultBranch = "master"
)
func main() {
config, err := spec.LoadConfig(configFilename)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
app := cli.NewApp()
app.Name = "ignoreit"
app.Usage = "Manage .gitignore templates declaratively"
var repo string
var branch string
addAndRemoveFlags := []cli.Flag{
cli.StringFlag{
Name: "repo, r",
Value: defaultRepo,
Usage: "uses .gitignore files from https://github.com/`REPO`",
Destination: &repo,
},
cli.StringFlag{
Name: "branch, b",
Value: defaultBranch,
Usage: "git `BRANCH` of the REPO",
Destination: &branch,
},
}
app.Commands = []cli.Command{
{
// Add source and branch flags
Name: "add",
Aliases: []string{"a"},
Usage: "add entries to .ignoreit.yml",
Flags: addAndRemoveFlags,
Action: func(c *cli.Context) error {
source := config.CreateSource(repo, branch)
var err error
if source != nil {
for _, entry := range c.Args() {
if err = source.AddEntry(entry); err != nil {
log.Fatalf("Error adding entry: %v", err)
}
}
return config.Save(configFilename)
}
return err
},
},
{
Name: "remove",
Aliases: []string{"rm"},
Usage: "remove entries to .ignoreit.yml",
Flags: addAndRemoveFlags,
Action: func(c *cli.Context) error {
source := config.GetSource(repo, branch)
var err error
if source != nil {
for _, entry := range c.Args() {
if err = source.RemoveEntry(entry); err != nil {
log.Fatalf("Error removing entry: %v", err)
}
}
return config.Save(configFilename)
}
return err
},
},
{
Name: "generate",
Aliases: []string{"g"},
Usage: "generate a .gitignore from .ignoreit.yml",
Action: func(c *cli.Context) error {
return generate.Inflate(config, ignoreFilename)
},
},
}
app.Run(os.Args)
}