-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: migration command #5506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: migration command #5506
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
973f628
chore: neutral configuration loader
ldez e24f69c
chore: copy v1 configuration structures
ldez 519820a
chore: clean configuration v2
ldez 450752d
chore: configuration v2 cloner
ldez 1a74a2a
feat: migration command
ldez 4ef8f4c
tests: configuration flles
ldez 9bea57a
review: add comment on generated files
ldez 101ac2a
review: add comment about v1 configuration files
ldez b7ca75f
review: rephrase
ldez e102282
review: log level
ldez ca11e3e
review: remove a switch
ldez d7d4986
fix: add a missing linter names migration case
ldez 6e989eb
docs: improve v1 configuration files
ldez c95ec05
review: rename configuration packages
ldez 8a3fbff
review
ldez 064f43f
review: add log about run.timeout
ldez 5ec43d0
review
ldez 345e674
review
ldez 1ab0066
review
ldez 7df6432
review
ldez fe69114
feat: missing case
ldez a7e11f8
review
ldez fdf27fc
review
ldez e809f59
review
ldez 7646196
chore: reduce log verbosity
ldez d66ff11
chore: rewrite the configuration loading
ldez 0a90047
fix: toml multiline string
ldez 8883832
chore: split test files by extensions
ldez d35e0d4
fix: exclude-use-default is true by default
ldez 6cbdf61
review
ldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package fakeloader | ||
|
||
// Config implements [config.BaseConfig]. | ||
// This only the stub for the real file loader. | ||
type Config struct { | ||
Version string `mapstructure:"version"` | ||
|
||
cfgDir string // Path to the directory containing golangci-lint config file. | ||
} | ||
|
||
func NewConfig() *Config { | ||
return &Config{} | ||
} | ||
|
||
// SetConfigDir sets the path to directory that contains golangci-lint config file. | ||
func (c *Config) SetConfigDir(dir string) { | ||
c.cfgDir = dir | ||
} | ||
|
||
func (*Config) IsInternalTest() bool { | ||
return false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package fakeloader | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/go-viper/mapstructure/v2" | ||
|
||
"github.com/golangci/golangci-lint/pkg/commands/internal/migrate/parser" | ||
"github.com/golangci/golangci-lint/pkg/config" | ||
) | ||
|
||
// Load is used to keep case of configuration. | ||
// Viper serialize raw map keys in lowercase, this is a problem with the configuration of some linters. | ||
func Load(srcPath string, old any) error { | ||
file, err := os.Open(srcPath) | ||
if err != nil { | ||
return fmt.Errorf("open file: %w", err) | ||
} | ||
|
||
defer func() { _ = file.Close() }() | ||
|
||
raw := map[string]any{} | ||
|
||
err = parser.Decode(file, raw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// NOTE: this is inspired by viper internals. | ||
cc := &mapstructure.DecoderConfig{ | ||
Result: old, | ||
WeaklyTypedInput: true, | ||
DecodeHook: config.DecodeHookFunc(), | ||
} | ||
|
||
decoder, err := mapstructure.NewDecoder(cc) | ||
if err != nil { | ||
return fmt.Errorf("constructing mapstructure decoder: %w", err) | ||
} | ||
|
||
err = decoder.Decode(raw) | ||
if err != nil { | ||
return fmt.Errorf("decoding configuration file: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package parser | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type File interface { | ||
io.ReadWriter | ||
Name() string | ||
} | ||
|
||
// Decode decodes a file into data. | ||
// The choice of the decoder is based on the file extension. | ||
func Decode(file File, data any) error { | ||
ext := filepath.Ext(file.Name()) | ||
|
||
switch strings.ToLower(ext) { | ||
case ".yaml", ".yml", ".json": | ||
err := yaml.NewDecoder(file).Decode(data) | ||
if err != nil && !errors.Is(err, io.EOF) { | ||
return fmt.Errorf("YAML decode file %s: %w", file.Name(), err) | ||
} | ||
|
||
case ".toml": | ||
err := toml.NewDecoder(file).Decode(&data) | ||
if err != nil { | ||
return fmt.Errorf("TOML decode file %s: %w", file.Name(), err) | ||
} | ||
|
||
default: | ||
return fmt.Errorf("unsupported file type: %s", ext) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Encode encodes data into a file. | ||
// The choice of the encoder is based on the file extension. | ||
func Encode(data any, dstFile File) error { | ||
ext := filepath.Ext(dstFile.Name()) | ||
|
||
switch strings.ToLower(ext) { | ||
case ".yml", ".yaml": | ||
encoder := yaml.NewEncoder(dstFile) | ||
encoder.SetIndent(2) | ||
|
||
return encoder.Encode(data) | ||
|
||
case ".toml": | ||
encoder := toml.NewEncoder(dstFile) | ||
|
||
return encoder.Encode(data) | ||
|
||
case ".json": | ||
// The JSON encoder converts empty struct to `{}` instead of nothing (even with omitempty JSON struct tags). | ||
// So we need to use the YAML encoder as bridge to create JSON file. | ||
|
||
var buf bytes.Buffer | ||
err := yaml.NewEncoder(&buf).Encode(data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
raw := map[string]any{} | ||
err = yaml.NewDecoder(&buf).Decode(raw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
encoder := json.NewEncoder(dstFile) | ||
encoder.SetIndent("", " ") | ||
|
||
return encoder.Encode(raw) | ||
|
||
default: | ||
return fmt.Errorf("unsupported file type: %s", ext) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"formatters": { | ||
"exclusions": { | ||
"generated": "lax" | ||
} | ||
}, | ||
"linters": { | ||
"exclusions": { | ||
"generated": "lax", | ||
"paths": [ | ||
"third_party$", | ||
"builtin$", | ||
"examples$" | ||
] | ||
} | ||
}, | ||
"version": "2" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
version = '2' | ||
|
||
[linters] | ||
[linters.exclusions] | ||
generated = 'lax' | ||
paths = ['third_party$', 'builtin$', 'examples$'] | ||
|
||
[formatters] | ||
[formatters.exclusions] | ||
generated = 'lax' |
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.