-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (98 loc) · 3.06 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
package main
import (
_ "embed"
"fmt"
"os"
"path/filepath"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/contexts"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/dontpanic"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/errorstrings"
"github.com/k1LoW/gostyle/analyzer/code_review_comments/handlerrors"
"github.com/k1LoW/gostyle/analyzer/decisions/funcfmt"
"github.com/k1LoW/gostyle/analyzer/decisions/getters"
"github.com/k1LoW/gostyle/analyzer/decisions/nilslices"
"github.com/k1LoW/gostyle/analyzer/decisions/pkgnames"
"github.com/k1LoW/gostyle/analyzer/decisions/recvnames"
"github.com/k1LoW/gostyle/analyzer/decisions/recvtype"
"github.com/k1LoW/gostyle/analyzer/decisions/repetition"
"github.com/k1LoW/gostyle/analyzer/decisions/underscores"
"github.com/k1LoW/gostyle/analyzer/decisions/useany"
"github.com/k1LoW/gostyle/analyzer/decisions/useq"
"github.com/k1LoW/gostyle/analyzer/decisions/varnames"
"github.com/k1LoW/gostyle/analyzer/effective/ifacenames"
"github.com/k1LoW/gostyle/analyzer/guide/mixedcaps"
"github.com/k1LoW/gostyle/config"
"github.com/k1LoW/gostyle/version"
"golang.org/x/tools/go/analysis/unitchecker"
)
//go:embed .gostyle.yml.init
var defaultConfig []byte
func main() {
if len(os.Args) == 1 {
if err := usage(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err) //nostyle:handlerrors
os.Exit(1)
}
os.Exit(0)
}
if len(os.Args) == 2 {
switch os.Args[1] {
case "init":
if err := generateConfig(); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err) //nostyle:handlerrors
os.Exit(1)
}
os.Exit(0)
case "-v":
fmt.Printf("%s version %s\n", version.Name, version.Version)
os.Exit(0)
}
}
unitchecker.Main( //nostyle:funcfmt
config.Loader,
contexts.AnalyzerWithConfig,
dontpanic.AnalyzerWithConfig,
errorstrings.AnalyzerWithConfig,
funcfmt.AnalyzerWithConfig,
getters.AnalyzerWithConfig,
handlerrors.AnalyzerWithConfig,
ifacenames.AnalyzerWithConfig,
pkgnames.AnalyzerWithConfig,
mixedcaps.AnalyzerWithConfig,
nilslices.AnalyzerWithConfig,
recvnames.AnalyzerWithConfig,
recvtype.AnalyzerWithConfig,
repetition.AnalyzerWithConfig,
underscores.AnalyzerWithConfig,
useany.AnalyzerWithConfig,
useq.AnalyzerWithConfig,
varnames.AnalyzerWithConfig,
)
}
func usage() error {
progname := filepath.Base(os.Args[0])
u := `%[1]s is a set of analyzers for coding styles.
Usage of %[1]s:
%.16[1]s init # generate .gostyle.yml
%.16[1]s unit.cfg # execute analysis specified by config file
%.16[1]s help # general help, including listing analyzers and flags
%.16[1]s help name # help on specific analyzer and its flags
`
if _, err := fmt.Fprintf(os.Stderr, u, progname); err != nil {
return err
}
return nil
}
func generateConfig() error {
const name = ".gostyle.yml"
if _, err := os.Stat(name); err == nil {
return fmt.Errorf("%s already exists", name)
}
if err := os.WriteFile(name, defaultConfig, os.ModePerm); err != nil {
return err
}
if _, err := fmt.Fprintf(os.Stderr, "%s is generated\n", name); err != nil {
return err
}
return nil
}