/
main.go
74 lines (62 loc) · 1.36 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
package main
import (
"encoding/json"
"fmt"
"github.com/mh-cbon/ignore-file/ignored"
"github.com/urfave/cli"
"os"
"path/filepath"
)
var VERSION = "0.0.0"
func main() {
app := cli.NewApp()
app.Name = "ignore-file"
app.Version = VERSION
app.Usage = "Test a ignore file"
app.UsageText = "ignore-file <ignore file path> <options>"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "json,j",
Usage: "return json response",
},
cli.StringSliceFlag{
Name: "also,a",
Usage: "Add new rule",
},
}
app.Action = run
app.Run(os.Args)
}
func run(c *cli.Context) error {
also := c.StringSlice("also")
if c.NArg() == 0 {
return cli.NewExitError("Missing <ignore file path> argument", 1)
}
file := c.Args().Get(0)
if _, err := os.Stat(file); os.IsNotExist(err) {
return cli.NewExitError(err.Error(), 1)
}
dir := filepath.Dir(file)
ignore := ignored.Ignored{}
if err := ignore.Load(file); err != nil {
return cli.NewExitError(err.Error(), 1)
}
for _, r := range also {
if err := ignore.Append(r); err != nil {
return cli.NewExitError(err.Error(), 1)
}
}
computed := ignore.ComputeDirectory(dir)
if c.Bool("json") {
jsoned, err := json.MarshalIndent(computed, "", " ")
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Print(string(jsoned))
} else {
for _, l := range computed {
fmt.Println(l)
}
}
return nil
}