Skip to content

Commit 9549ea8

Browse files
committed
WIP
1 parent b14d05c commit 9549ea8

File tree

22 files changed

+1046
-4
lines changed

22 files changed

+1046
-4
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@
1818
/vendor/
1919
coverage.out
2020
coverage.xml
21+
22+
/custom-golangci-lint
23+
/gcl-custom
24+
.mygcl.yml

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ linters-settings:
8080
disabled: true
8181
- name: unused-parameter
8282

83+
8384
linters:
8485
disable-all: true
8586
enable:

cmd/golangci-lint/plugins.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package main
2+
3+
import _ "github.com/golangci/golangci-lint/pkg/experimental/modules/myplugin"

pkg/commands/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/spf13/pflag"
1212

1313
"github.com/golangci/golangci-lint/pkg/config"
14+
"github.com/golangci/golangci-lint/pkg/experimental/commands/custom"
1415
"github.com/golangci/golangci-lint/pkg/logutils"
1516
"github.com/golangci/golangci-lint/pkg/report"
1617
)
@@ -66,6 +67,7 @@ func newRootCommand(info BuildInfo) *rootCommand {
6667
newCacheCommand().cmd,
6768
newConfigCommand(log).cmd,
6869
newVersionCommand(info).cmd,
70+
custom.NewCommand(log).Cmd,
6971
)
7072

7173
rootCmd.SetHelpCommand(newHelpCommand(log).cmd)

pkg/commands/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/golangci/golangci-lint/internal/pkgcache"
3131
"github.com/golangci/golangci-lint/pkg/config"
3232
"github.com/golangci/golangci-lint/pkg/exitcodes"
33+
"github.com/golangci/golangci-lint/pkg/experimental/db"
3334
"github.com/golangci/golangci-lint/pkg/fsutils"
3435
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load"
3536
"github.com/golangci/golangci-lint/pkg/goutil"
@@ -171,7 +172,7 @@ func (c *runCommand) persistentPostRunE(_ *cobra.Command, _ []string) error {
171172

172173
func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
173174
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
174-
lintersdb.NewPluginBuilder(c.log), lintersdb.NewLinterBuilder())
175+
db.NewPluginBuilderWrapper(c.log), lintersdb.NewLinterBuilder())
175176
if err != nil {
176177
return err
177178
}

pkg/config/linters_settings.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"encoding"
55
"errors"
6+
"fmt"
67
"runtime"
78

89
"gopkg.in/yaml.v3"
@@ -292,7 +293,17 @@ type LintersSettings struct {
292293
}
293294

294295
func (s *LintersSettings) Validate() error {
295-
return s.Govet.Validate()
296+
if err := s.Govet.Validate(); err != nil {
297+
return err
298+
}
299+
300+
for name, settings := range s.Custom {
301+
if err := settings.Validate(); err != nil {
302+
return fmt.Errorf("custom linter %q: %w", name, err)
303+
}
304+
}
305+
306+
return nil
296307
}
297308

298309
type AsasalintSettings struct {
@@ -987,4 +998,19 @@ type CustomLinterSettings struct {
987998

988999
// Settings plugin settings only work with linterdb.PluginConstructor symbol.
9891000
Settings any
1001+
1002+
// FIXME goplugin,module
1003+
Type string `mapstructure:"type"`
1004+
}
1005+
1006+
func (s *CustomLinterSettings) Validate() error {
1007+
if s.Type == "module" {
1008+
return nil
1009+
}
1010+
1011+
if s.Path == "" {
1012+
return errors.New("path is required")
1013+
}
1014+
1015+
return nil
9901016
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
version: v1.56.2
2+
name: custom-golangci-lint # optional, default: gcl-custom
3+
plugins:
4+
# a plugin from a Go proxy
5+
- module: 'github.com/example/plugin3'
6+
version: v1.2.3
7+
8+
# a plugin from a Go proxy (with a specific import path)
9+
- module: 'github.com/example/plugin4'
10+
import: 'github.com/example/plugin4/foo'
11+
version: v1.0.0
12+
13+
# a plugin from local source (with absolute path)
14+
- module: 'github.com/example/plugin2'
15+
path: /my/local/path/plugin2
16+
17+
# a plugin from local source (with relative path)
18+
- module: 'github.com/example/plugin1'
19+
path: ./my/local/path/plugin1
20+
21+
# a plugin from local source (with absolute path and a specific import path)
22+
- module: 'github.com/example/plugin2'
23+
import: 'github.com/example/plugin4/foo'
24+
path: /my/local/path/plugin2
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package custom
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
9+
"github.com/spf13/cobra"
10+
11+
"github.com/golangci/golangci-lint/pkg/experimental/commands/custom/internal"
12+
"github.com/golangci/golangci-lint/pkg/logutils"
13+
)
14+
15+
const envKeepTempFiles = "MYGCL_KEEP_TEMP_FILES"
16+
17+
type Command struct {
18+
Cmd *cobra.Command
19+
20+
cfg *internal.Configuration
21+
22+
log logutils.Log
23+
}
24+
25+
func NewCommand(logger logutils.Log) *Command {
26+
c := &Command{log: logger}
27+
28+
customCmd := &cobra.Command{
29+
Use: "custom",
30+
Short: "Build a version of golangci-lint with custom linters.",
31+
Args: cobra.NoArgs,
32+
PreRunE: c.preRunE,
33+
RunE: c.runE,
34+
}
35+
36+
c.Cmd = customCmd
37+
38+
return c
39+
}
40+
41+
func (c *Command) preRunE(_ *cobra.Command, _ []string) error {
42+
cfg, err := internal.LoadConfiguration()
43+
if err != nil {
44+
return err
45+
}
46+
47+
err = cfg.Validate()
48+
if err != nil {
49+
return err
50+
}
51+
52+
c.cfg = cfg
53+
54+
return nil
55+
}
56+
57+
func (c *Command) runE(_ *cobra.Command, _ []string) error {
58+
ctx := context.Background()
59+
60+
tmp, err := os.MkdirTemp(os.TempDir(), "mygcl")
61+
if err != nil {
62+
return fmt.Errorf("create temporary directory: %w", err)
63+
}
64+
65+
defer func() {
66+
if os.Getenv(envKeepTempFiles) != "" {
67+
log.Printf("WARN: The env var %s has been dectected: the temporary directory is preserved: %s", envKeepTempFiles, tmp)
68+
69+
return
70+
}
71+
72+
_ = os.RemoveAll(tmp)
73+
}()
74+
75+
err = internal.NewBuilder(c.log, c.cfg, tmp).Build(ctx)
76+
if err != nil {
77+
return fmt.Errorf("build process: %w", err)
78+
}
79+
80+
return nil
81+
}

0 commit comments

Comments
 (0)