This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
77 lines (69 loc) · 1.82 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
package main
import (
"fmt"
"os"
"github.com/alecthomas/kingpin"
"github.com/caarlos0/shcheck/sh"
"github.com/caarlos0/shcheck/status"
zglob "github.com/mattn/go-zglob"
)
// nolint: gochecknoglobals,lll
var (
version = "master"
app = kingpin.New("shcheck", "shcheck validates your scripts against both shellcheck and shfmt")
ignoredFiles = app.Flag("ignore", "ignore files or folders").HintOptions("folder/**/*", "*.bash").Short('i').Strings()
shellcheckExcludes = app.Flag("shellcheck-exclude", "exclude some shellcheck checks").HintOptions("SC1090", "SC1004").Short('e').Strings()
)
func main() {
app.Version("shfmt version " + version)
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')
kingpin.MustParse(app.Parse(os.Args[1:]))
// nolint: godox
// TODO: also look for executables with a valid shell shebang
files, err := zglob.Glob(`**/*.*sh`)
kingpin.FatalIfError(err, "fail to find all shell files")
var fail bool
for _, file := range files {
if err := check(file); err != nil {
fail = true
}
}
if fail {
kingpin.Fatalf("\nsome checks failed. check output above.\n")
}
}
func check(file string) error {
if ignore(*ignoredFiles, file) {
status.Ignore(file)
return nil
}
var options = sh.Options{
Shellcheck: sh.ShellcheckOptions{
Exclude: *shellcheckExcludes,
},
}
var errors []error
for _, check := range sh.Checkers(options) {
if err := check.Check(file); err != nil {
errors = append(errors, err)
}
}
if len(errors) == 0 {
status.Success(file)
return nil
}
status.Fail(file)
for _, err := range errors {
fmt.Println(err)
}
return fmt.Errorf("check failed")
}
func ignore(patterns []string, file string) bool {
for _, pattern := range patterns {
if ok, err := zglob.Match(pattern, file); ok && err == nil {
return true
}
}
return false
}