This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckmake.go
131 lines (106 loc) · 2.96 KB
/
checkmake.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"io"
"log"
"os"
docopt "github.com/docopt/docopt-go"
"github.com/mrtazz/checkmake/config"
"github.com/mrtazz/checkmake/formatters"
"github.com/mrtazz/checkmake/logger"
"github.com/mrtazz/checkmake/parser"
"github.com/mrtazz/checkmake/rules"
"github.com/mrtazz/checkmake/validator"
"github.com/olekukonko/tablewriter"
)
var (
usage = `checkmake.
Usage:
checkmake [options] <makefile>
checkmake -h | --help
checkmake --version
checkmake --list-rules
Options:
-h --help Show this screen.
--version Show version.
--debug Enable debug mode
--config=<configPath> Configuration file to read
--format=<format> Output format as a Golang text/template template
--list-rules List registered rules
`
version = ""
buildTime = ""
builder = ""
goversion = ""
configPath = "checkmake.ini"
)
func main() {
args, err := docopt.Parse(usage, nil, true,
fmt.Sprintf("%s %s built at %s by %s with %s",
"checkmake", version, buildTime, builder, goversion), false)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
formatter, violations := parseArgsAndGetFormatter(args)
formatter.Format(violations)
os.Exit(len(violations))
}
func parseArgsAndGetFormatter(args map[string]interface{}) (formatters.Formatter,
rules.RuleViolationList) {
if args["--debug"] == true {
logger.SetLogLevel(logger.DebugLevel)
}
if args["--list-rules"] == true {
listRules(os.Stdout)
os.Exit(0)
}
makefile, parseError := parser.Parse(args["<makefile>"].(string))
if parseError != nil {
log.Fatal(parseError)
os.Exit(1)
}
if args["--config"] != nil {
configPath = args["--config"].(string)
}
cfg, cfgError := config.NewConfigFromFile(configPath)
if cfgError != nil {
logger.Info(fmt.Sprintf("Unable to parse config file %q, running with defaults",
configPath))
}
violations := validator.Validate(makefile, cfg)
var formatter formatters.Formatter
if args["--format"] != nil {
format := args["--format"].(string)
var err error
formatter, err = formatters.NewCustomFormatter(format)
if err != nil {
logger.Error(fmt.Sprintf("Unable to create formatter: %q", err.Error()))
os.Exit(1)
}
} else if format, formatErr := cfg.GetConfigValue("format"); formatErr == nil {
var err error
formatter, err = formatters.NewCustomFormatter(format)
if err != nil {
logger.Error(fmt.Sprintf("Unable to create formatter: %q", err.Error()))
os.Exit(1)
}
} else {
formatter = formatters.NewDefaultFormatter()
}
return formatter, violations
}
func listRules(w io.Writer) {
data := [][]string{}
for _, rule := range rules.GetRegisteredRules() {
data = append(data, []string{rule.Name(), rule.Description()})
}
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"Name", "Description"})
table.SetCenterSeparator(" ")
table.SetColumnSeparator(" ")
table.SetRowSeparator(" ")
table.SetAutoWrapText(true)
table.AppendBulk(data)
table.Render()
}