-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
108 lines (92 loc) · 1.83 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
package main
import (
"errors"
"flag"
"log"
"os"
)
const (
Version = "1.2.0"
VersionOutput = "Snag version " + Version
)
const SnagFile = ".snag.yml"
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(0)
}
func main() {
flag.Parse()
if flag.NArg() > 0 {
if err := handleSubCommand(flag.Arg(0)); err != nil {
log.Fatal(err)
}
return
}
if version {
log.Println("The 'version' flag is deprecated. Use 'snag version'")
log.Println(VersionOutput)
return
}
c, err := parseConfig()
if err != nil {
log.Fatal(err)
}
b, err := NewBuilder(c)
if err != nil {
log.Fatal(err)
}
defer b.Close()
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
b.Watch(wd)
}
func handleSubCommand(cmd string) error {
switch flag.Arg(0) {
case "init":
return initSnag()
case "version":
log.Println(VersionOutput)
return nil
default:
flag.Usage()
return nil
}
}
func initSnag() error {
if _, err := os.Stat(SnagFile); err == nil {
return errors.New("snag file already exists")
}
f, err := os.Create(SnagFile)
if err != nil {
return err
}
defer f.Close()
tmpl := `---
# Snag configuartion
#
# Make sure you modify this file to get started.
# If you have any questions please refer to https://github.com/Tonkpils/snag
#
# Verbose controls whether the process will output a command's output.
# verbose: true
#
# Use the ignore section to ignore files or directors from being watched.
# You can use 'gitignore' patterns for each item in the list.
# ignore:
# - .git
#
# Build executes a list of commands sequentially
# build:
# - echo 'Hello world'
`
_, err = f.Write([]byte(tmpl))
if err != nil {
return err
}
success := `Successfully created sample configuration %q in your current directory.
Make sure you modify this file and run 'snag' to get going!`
log.Printf(success, SnagFile)
return nil
}