|
| 1 | +package commands |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + hcversion "github.com/hashicorp/go-version" |
| 11 | + "github.com/pelletier/go-toml/v2" |
| 12 | + "github.com/santhosh-tekuri/jsonschema/v5" |
| 13 | + _ "github.com/santhosh-tekuri/jsonschema/v5/httploader" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/spf13/pflag" |
| 16 | + "gopkg.in/yaml.v3" |
| 17 | + |
| 18 | + "github.com/golangci/golangci-lint/pkg/exitcodes" |
| 19 | +) |
| 20 | + |
| 21 | +type verifyOptions struct { |
| 22 | + schemaURL string // For debugging purpose only (Flag only). |
| 23 | +} |
| 24 | + |
| 25 | +func (c *configCommand) executeVerify(cmd *cobra.Command, _ []string) error { |
| 26 | + usedConfigFile := c.getUsedConfig() |
| 27 | + if usedConfigFile == "" { |
| 28 | + c.log.Warnf("No config file detected") |
| 29 | + os.Exit(exitcodes.NoConfigFileDetected) |
| 30 | + } |
| 31 | + |
| 32 | + schemaURL, err := createSchemaURL(cmd.Flags(), c.buildInfo) |
| 33 | + if err != nil { |
| 34 | + return fmt.Errorf("get JSON schema: %w", err) |
| 35 | + } |
| 36 | + |
| 37 | + err = validateConfiguration(schemaURL, usedConfigFile) |
| 38 | + if err != nil { |
| 39 | + var v *jsonschema.ValidationError |
| 40 | + if !errors.As(err, &v) { |
| 41 | + return fmt.Errorf("[%s] validate: %w", usedConfigFile, err) |
| 42 | + } |
| 43 | + |
| 44 | + detail := v.DetailedOutput() |
| 45 | + |
| 46 | + printValidationDetail(cmd, &detail) |
| 47 | + |
| 48 | + return fmt.Errorf("the configuration contains invalid elements") |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error) { |
| 55 | + schemaURL, err := flags.GetString("schema") |
| 56 | + if err != nil { |
| 57 | + return "", fmt.Errorf("get schema flag: %w", err) |
| 58 | + } |
| 59 | + |
| 60 | + if schemaURL != "" { |
| 61 | + return schemaURL, nil |
| 62 | + } |
| 63 | + |
| 64 | + switch { |
| 65 | + case buildInfo.Version != "" && buildInfo.Version != "(devel)": |
| 66 | + version, err := hcversion.NewVersion(buildInfo.Version) |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("parse version: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + schemaURL = fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json", |
| 72 | + version.Segments()[0], version.Segments()[1]) |
| 73 | + |
| 74 | + case buildInfo.Commit != "" && buildInfo.Commit != "?": |
| 75 | + if buildInfo.Commit == "unknown" { |
| 76 | + return "", errors.New("unknown commit information") |
| 77 | + } |
| 78 | + |
| 79 | + commit := buildInfo.Commit |
| 80 | + |
| 81 | + if strings.HasPrefix(commit, "(") { |
| 82 | + c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",") |
| 83 | + if !ok { |
| 84 | + return "", errors.New("commit information not found") |
| 85 | + } |
| 86 | + |
| 87 | + commit = c |
| 88 | + } |
| 89 | + |
| 90 | + schemaURL = fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json", |
| 91 | + commit) |
| 92 | + |
| 93 | + default: |
| 94 | + return "", errors.New("version not found") |
| 95 | + } |
| 96 | + |
| 97 | + return schemaURL, nil |
| 98 | +} |
| 99 | + |
| 100 | +func validateConfiguration(schemaPath, targetFile string) error { |
| 101 | + compiler := jsonschema.NewCompiler() |
| 102 | + compiler.Draft = jsonschema.Draft7 |
| 103 | + |
| 104 | + schema, err := compiler.Compile(schemaPath) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("compile schema: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + var m any |
| 110 | + |
| 111 | + switch strings.ToLower(filepath.Ext(targetFile)) { |
| 112 | + case ".yaml", ".yml", ".json": |
| 113 | + m, err = decodeYamlFile(targetFile) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + case ".toml": |
| 119 | + m, err = decodeTomlFile(targetFile) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + default: |
| 125 | + // unsupported |
| 126 | + return errors.New("unsupported configuration format") |
| 127 | + } |
| 128 | + |
| 129 | + return schema.Validate(m) |
| 130 | +} |
| 131 | + |
| 132 | +func printValidationDetail(cmd *cobra.Command, detail *jsonschema.Detailed) { |
| 133 | + if detail.Error != "" { |
| 134 | + cmd.PrintErrf("jsonschema: %q does not validate with %q: %s\n", |
| 135 | + strings.ReplaceAll(strings.TrimPrefix(detail.InstanceLocation, "/"), "/", "."), detail.KeywordLocation, detail.Error) |
| 136 | + } |
| 137 | + |
| 138 | + for _, d := range detail.Errors { |
| 139 | + d := d |
| 140 | + printValidationDetail(cmd, &d) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func decodeYamlFile(filename string) (any, error) { |
| 145 | + file, err := os.Open(filename) |
| 146 | + if err != nil { |
| 147 | + return nil, fmt.Errorf("[%s] file open: %w", filename, err) |
| 148 | + } |
| 149 | + |
| 150 | + defer func() { _ = file.Close() }() |
| 151 | + |
| 152 | + var m any |
| 153 | + err = yaml.NewDecoder(file).Decode(&m) |
| 154 | + if err != nil { |
| 155 | + return nil, fmt.Errorf("[%s] YAML decode: %w", filename, err) |
| 156 | + } |
| 157 | + |
| 158 | + return m, nil |
| 159 | +} |
| 160 | + |
| 161 | +func decodeTomlFile(filename string) (any, error) { |
| 162 | + file, err := os.Open(filename) |
| 163 | + if err != nil { |
| 164 | + return nil, fmt.Errorf("[%s] file open: %w", filename, err) |
| 165 | + } |
| 166 | + |
| 167 | + defer func() { _ = file.Close() }() |
| 168 | + |
| 169 | + var m any |
| 170 | + err = toml.NewDecoder(file).Decode(&m) |
| 171 | + if err != nil { |
| 172 | + return nil, fmt.Errorf("[%s] TOML decode: %w", filename, err) |
| 173 | + } |
| 174 | + |
| 175 | + return m, nil |
| 176 | +} |
0 commit comments