-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
76 lines (63 loc) · 1.53 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
package main
import (
"flag"
"fmt"
"github.com/Parquery/gocontracts/gocontracts"
"os"
)
var version = flag.Bool("version", false, "print the version to STDOUT and exit immediately")
var inPlace = flag.Bool("w", false, "write result to (source) file instead of stdout")
var remove = flag.Bool("r", false,
"remove the condition checks from the code (but leave them in the comments). "+
"This is useful when you want to build a production binary without the checks.")
func usage() {
_, err := fmt.Fprintf(os.Stderr, "usage: gocontracts [flags] [path]\n")
if err != nil {
panic(err.Error())
}
flag.PrintDefaults()
}
func main() {
os.Exit(func() (retcode int) {
flag.Parse()
if *version {
fmt.Println("1.3.0")
return 0
}
if flag.NArg() != 1 {
_, err := fmt.Fprintf(os.Stderr, "Expected the path to the file as a single positional argument, "+
"but got positional %d argument(s)\n", flag.NArg())
if err != nil {
panic(err.Error())
}
usage()
retcode = 1
return
}
pth := flag.Arg(0)
if *inPlace {
err := gocontracts.ProcessInPlace(pth, *remove)
if err != nil {
_, err = fmt.Fprintf(os.Stderr, err.Error())
if err != nil {
panic(err.Error())
}
return 1
}
} else {
updated, err := gocontracts.ProcessFile(pth, *remove)
if err != nil {
_, err = fmt.Fprintf(os.Stderr, err.Error())
if err != nil {
panic(err.Error())
}
return 1
}
_, err = fmt.Fprint(os.Stdout, updated)
if err != nil {
panic(err.Error())
}
}
return 0
}())
}