-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.go
137 lines (111 loc) · 3.32 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
"github.com/golangci/golangci-lint/scripts/website/types"
)
func main() {
err := saveLinters()
if err != nil {
log.Fatalf("Save linters: %v", err)
}
err = saveDefaultExclusions()
if err != nil {
log.Fatalf("Save default exclusions: %v", err)
}
err = saveCLIHelp(filepath.Join("assets", "cli-help.json"))
if err != nil {
log.Fatalf("Save CLI help: %v", err)
}
}
func saveLinters() error {
linters, _ := lintersdb.NewLinterBuilder().Build(config.NewDefault())
var wraps []types.LinterWrapper
for _, l := range linters {
if l.IsDeprecated() && l.Deprecation.Level > linter.DeprecationWarning {
continue
}
wrapper := types.LinterWrapper{
Name: l.Linter.Name(),
Desc: l.Linter.Desc(),
EnabledByDefault: l.EnabledByDefault,
LoadMode: l.LoadMode,
InPresets: l.InPresets,
AlternativeNames: l.AlternativeNames,
OriginalURL: l.OriginalURL,
Internal: l.Internal,
CanAutoFix: l.CanAutoFix,
IsSlow: l.IsSlow,
DoesChangeTypes: l.DoesChangeTypes,
Since: l.Since,
}
if l.Deprecation != nil {
wrapper.Deprecation = &types.Deprecation{
Since: l.Deprecation.Since,
Message: l.Deprecation.Message,
Replacement: l.Deprecation.Replacement,
}
}
wraps = append(wraps, wrapper)
}
return saveToJSONFile(filepath.Join("assets", "linters-info.json"), wraps)
}
func saveDefaultExclusions() error {
var excludePatterns []types.ExcludePattern
for _, pattern := range config.DefaultExcludePatterns {
excludePatterns = append(excludePatterns, types.ExcludePattern{
ID: pattern.ID,
Pattern: pattern.Pattern,
Linter: pattern.Linter,
Why: pattern.Why,
})
}
return saveToJSONFile(filepath.Join("assets", "default-exclusions.json"), excludePatterns)
}
func saveCLIHelp(dst string) error {
err := exec.Command("make", "build").Run()
if err != nil {
return fmt.Errorf("can't run make build: %w", err)
}
lintersOut, err := exec.Command("./golangci-lint", "help", "linters").Output()
if err != nil {
return fmt.Errorf("can't run linters cmd: %w", err)
}
lintersOutParts := bytes.Split(lintersOut, []byte("\n\n"))
helpCmd := exec.Command("./golangci-lint", "run", "-h")
helpCmd.Env = append(helpCmd.Env, os.Environ()...)
helpCmd.Env = append(helpCmd.Env, "HELP_RUN=1") // make default concurrency stable: don't depend on machine CPU number
help, err := helpCmd.Output()
if err != nil {
return fmt.Errorf("can't run help cmd: %w", err)
}
helpLines := bytes.Split(help, []byte("\n"))
shortHelp := bytes.Join(helpLines[2:], []byte("\n"))
data := types.CLIHelp{
Enable: string(lintersOutParts[0]),
Help: string(shortHelp),
}
return saveToJSONFile(dst, data)
}
func saveToJSONFile(dst string, data any) error {
file, err := os.Create(dst)
if err != nil {
return fmt.Errorf("open file (%s): %w", dst, err)
}
defer func() { _ = file.Close() }()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
err = encoder.Encode(data)
if err != nil {
return fmt.Errorf("encode JSON (%s): %w", dst, err)
}
return nil
}