-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (134 loc) · 4.08 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"embed"
"errors"
"flag"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/maratori/pairedbrackets/pkg/pairedbrackets"
"golang.org/x/tools/go/analysis/singlechecker"
)
//go:embed pkg
//go:embed plugin
//go:embed go.*
var self embed.FS
const (
buildFlagName = "build-golangci-lint-plugin"
buildFlagDefault = false
buildFlagUsage = "build plugin for your version of golangci-lint, see other flags"
goPathFlagName = "go-path"
goPathFlagDefault = "go"
goPathFlagUsage = "path to go executable, is used only with -" + buildFlagName
linterFlagName = "golangci-lint"
linterFlagDefault = "golangci-lint"
linterFlagUsage = "path to golangci-lint executable, is used only with -" + buildFlagName
outputFlagName = "plugin-output"
outputFlagDefault = "pairedbrackets.so"
outputFlagUsage = "plugin output path, is used only with -" + buildFlagName
)
func main() {
set := flag.NewFlagSet("", flag.ContinueOnError)
set.SetOutput(io.Discard)
build := set.Bool(buildFlagName, buildFlagDefault, buildFlagUsage)
goPath := set.String(goPathFlagName, goPathFlagDefault, goPathFlagUsage)
linter := set.String(linterFlagName, linterFlagDefault, linterFlagUsage)
output := set.String(outputFlagName, outputFlagDefault, outputFlagUsage)
if set.Parse(os.Args[1:]) == nil && *build {
err := buildPlugin(*goPath, *linter, *output)
if err != nil {
fmt.Println(err) //nolint:forbidigo // better than log
os.Exit(1)
}
os.Exit(0)
}
analyzer := pairedbrackets.NewAnalyzer()
set.VisitAll(func(f *flag.Flag) {
analyzer.Flags.Var(f.Value, f.Name, f.Usage) // for documentation
})
singlechecker.Main(analyzer)
}
func buildPlugin(goPath string, linterPath string, outputPath string) error {
goPath, linterPath, outputPath, err := resolvePaths(goPath, linterPath, outputPath)
if err != nil {
return err
}
output, err := exec.Command(goPath, "version", "-m", linterPath).CombinedOutput()
if err != nil {
return fmt.Errorf("can't get version of golangci-lint dependencies: %w", err)
}
match := regexp.MustCompile(`dep\s+golang.org/x/tools\s+(v\S+)`).FindSubmatch(output)
if len(match) == 0 {
return errors.New("golang.org/x/tools not found in golangci-lint dependencies")
}
lib := "golang.org/x/tools@" + string(match[1])
temp, err := os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("temp dir not created: %w", err)
}
defer os.RemoveAll(temp)
err = writeSelfSourceCodeToDisk(temp)
if err != nil {
return fmt.Errorf("can't write pairedbrackets source code: %w", err)
}
err = os.Chdir(temp)
if err != nil {
return fmt.Errorf("can't change wording directory: %w", err)
}
output, err = exec.Command(goPath, "get", lib).CombinedOutput()
if err != nil {
return fmt.Errorf("can't get %s: %w\n%s", lib, err, output)
}
output, err = exec.Command(
goPath,
"build",
"-buildmode=plugin",
"-o",
outputPath,
"plugin/pairedbrackets.go",
).CombinedOutput()
if err != nil {
return fmt.Errorf("can't build plugin: %w\n%s", err, output)
}
fmt.Printf("pairedbrackets.so is built with %s\n", lib) //nolint:forbidigo // better than log
return nil
}
func resolvePaths(goPath string, linterPath string, outputPath string) (string, string, string, error) {
goPath, err := exec.LookPath(goPath)
if err != nil {
return "", "", "", fmt.Errorf("go not found: %w", err)
}
linterPath, err = exec.LookPath(linterPath)
if err != nil {
return "", "", "", fmt.Errorf("golangci-lint not found: %w", err)
}
outputPath, err = filepath.Abs(outputPath)
if err != nil {
return "", "", "", fmt.Errorf("can't get absolute output path: %w", err)
}
return goPath, linterPath, outputPath, nil
}
func writeSelfSourceCodeToDisk(temp string) error {
return fs.WalkDir(self, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
fullPath := filepath.Join(temp, path)
if d.IsDir() {
return os.MkdirAll(fullPath, 0700)
}
content, err := fs.ReadFile(self, path)
if err != nil {
return err
}
err = os.WriteFile(fullPath, content, 0600)
if err != nil {
return err
}
return nil
})
}