-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: add verify command #4527
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: add verify command #4527
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ab92542
feat: add verify command
ldez bdd92de
chore: add versioned copy of the JSON Schema
ldez 6fa3e3a
chore: silence some command usage on error
ldez 9f2bc93
chore: add the command to the CI
ldez 461b822
chore: use a dedicated file for the sub command
ldez f35212b
fix: hack to hide deprecation message
ldez 18cea9f
chore: rename execute to executePath
ldez 9eed0cb
review
ldez 28d33f2
fix: use current schema on tests
ldez cd2e5b1
review
ldez 5797a36
Revert "chore: silence some command usage on error"
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,175 @@ | ||
package commands | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
hcversion "github.com/hashicorp/go-version" | ||
"github.com/pelletier/go-toml/v2" | ||
"github.com/santhosh-tekuri/jsonschema/v5" | ||
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/golangci/golangci-lint/pkg/exitcodes" | ||
) | ||
|
||
func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error { | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
usedConfigFile := c.getUsedConfig() | ||
if usedConfigFile == "" { | ||
c.log.Warnf("No config file detected") | ||
os.Exit(exitcodes.NoConfigFileDetected) | ||
} | ||
|
||
schemaURL, err := getSchemaURL(cmd.Flags(), c.buildInfo) | ||
if err != nil { | ||
return fmt.Errorf("get JSON schema: %w", err) | ||
} | ||
|
||
err = validateConfiguration(schemaURL, usedConfigFile) | ||
if err != nil { | ||
var v *jsonschema.ValidationError | ||
if !errors.As(err, &v) { | ||
return fmt.Errorf("[%s] validate: %w", usedConfigFile, err) | ||
} | ||
|
||
detail := v.DetailedOutput() | ||
|
||
printValidationDetail(cmd, &detail) | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
func printValidationDetail(cmd *cobra.Command, detail *jsonschema.Detailed) { | ||
if detail.Error != "" { | ||
cmd.PrintErrf("jsonschema: %s does not validate with %s: %s\n", | ||
ldez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, detail.Error) | ||
} | ||
|
||
for _, d := range detail.Errors { | ||
d := d | ||
printValidationDetail(cmd, &d) | ||
} | ||
} | ||
|
||
func getSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error) { | ||
ldez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
schemaURL, err := flags.GetString("schema") | ||
if err != nil { | ||
return "", fmt.Errorf("get schema flag: %w", err) | ||
} | ||
|
||
if schemaURL != "" { | ||
return schemaURL, nil | ||
} | ||
|
||
switch { | ||
case buildInfo.Version != "" && buildInfo.Version != "(devel)": | ||
version, err := hcversion.NewVersion(buildInfo.Version) | ||
if err != nil { | ||
return "", fmt.Errorf("parse version: %w", err) | ||
} | ||
|
||
schemaURL = fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json", | ||
version.Segments()[0], version.Segments()[1]) | ||
|
||
case buildInfo.Commit != "" && buildInfo.Commit != "?": | ||
if buildInfo.Commit != "unknown" { | ||
return "", errors.New("unknown commit information") | ||
} | ||
|
||
commit := buildInfo.Commit | ||
|
||
if strings.HasPrefix(commit, "(") { | ||
c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",") | ||
if !ok { | ||
return "", errors.New("commit information not found") | ||
} | ||
|
||
commit = c | ||
} | ||
|
||
schemaURL = fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json", | ||
commit) | ||
|
||
default: | ||
return "", errors.New("version not found") | ||
} | ||
|
||
return schemaURL, nil | ||
} | ||
|
||
func validateConfiguration(schemaPath, targetFile string) error { | ||
compiler := jsonschema.NewCompiler() | ||
compiler.Draft = jsonschema.Draft7 | ||
|
||
schema, err := compiler.Compile(schemaPath) | ||
if err != nil { | ||
return fmt.Errorf("compile schema: %w", err) | ||
} | ||
|
||
var m any | ||
|
||
switch strings.ToLower(filepath.Ext(targetFile)) { | ||
case ".yaml", ".yml", ".json": | ||
m, err = decodeYamlFile(targetFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
case ".toml": | ||
m, err = decodeTomlFile(targetFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
default: | ||
// unsupported | ||
return errors.New("unsupported configuration format") | ||
} | ||
|
||
err = schema.Validate(m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
ldez marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
func decodeYamlFile(filename string) (any, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("[%s] file open: %w", filename, err) | ||
} | ||
|
||
defer func() { _ = file.Close() }() | ||
|
||
var m any | ||
err = yaml.NewDecoder(file).Decode(&m) | ||
if err != nil { | ||
return nil, fmt.Errorf("[%s] YAML decode: %w", filename, err) | ||
} | ||
|
||
return m, nil | ||
} | ||
|
||
func decodeTomlFile(filename string) (any, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("[%s] file open: %w", filename, err) | ||
} | ||
|
||
defer func() { _ = file.Close() }() | ||
|
||
var m any | ||
err = toml.NewDecoder(file).Decode(&m) | ||
if err != nil { | ||
return nil, fmt.Errorf("[%s] TOML decode: %w", filename, err) | ||
} | ||
|
||
return m, nil | ||
} | ||
alexandear marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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.
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.