Skip to content

Commit 6fb922a

Browse files
committed
WIP
1 parent a087808 commit 6fb922a

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
@@ -31,6 +31,7 @@ import (
3131
"github.com/golangci/golangci-lint/internal/pkgcache"
3232
"github.com/golangci/golangci-lint/pkg/config"
3333
"github.com/golangci/golangci-lint/pkg/exitcodes"
34+
"github.com/golangci/golangci-lint/pkg/experimental/db"
3435
"github.com/golangci/golangci-lint/pkg/fsutils"
3536
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis/load"
3637
"github.com/golangci/golangci-lint/pkg/goutil"
@@ -177,7 +178,7 @@ func (c *runCommand) persistentPostRunE(_ *cobra.Command, _ []string) error {
177178

178179
func (c *runCommand) preRunE(_ *cobra.Command, _ []string) error {
179180
dbManager, err := lintersdb.NewManager(c.log.Child(logutils.DebugKeyLintersDB), c.cfg,
180-
lintersdb.NewPluginBuilder(c.log), lintersdb.NewLinterBuilder())
181+
db.NewPluginBuilderWrapper(c.log), lintersdb.NewLinterBuilder())
181182
if err != nil {
182183
return err
183184
}

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"
@@ -280,7 +281,17 @@ type LintersSettings struct {
280281
}
281282

282283
func (s *LintersSettings) Validate() error {
283-
return s.Govet.Validate()
284+
if err := s.Govet.Validate(); err != nil {
285+
return err
286+
}
287+
288+
for name, settings := range s.Custom {
289+
if err := settings.Validate(); err != nil {
290+
return fmt.Errorf("custom linter %q: %w", name, err)
291+
}
292+
}
293+
294+
return nil
284295
}
285296

286297
type AsasalintSettings struct {
@@ -958,4 +969,19 @@ type CustomLinterSettings struct {
958969

959970
// Settings plugin settings only work with linterdb.PluginConstructor symbol.
960971
Settings any
972+
973+
// FIXME goplugin,module
974+
Type string `mapstructure:"type"`
975+
}
976+
977+
func (s *CustomLinterSettings) Validate() error {
978+
if s.Type == "module" {
979+
return nil
980+
}
981+
982+
if s.Path == "" {
983+
return errors.New("path is required")
984+
}
985+
986+
return nil
961987
}
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)