Skip to content

Commit 8087d82

Browse files
ldezSeigeC
authored andcommitted
docs: split configuration page into multiple sections (golangci#2484)
1 parent c3a681f commit 8087d82

File tree

5 files changed

+180
-53
lines changed

5 files changed

+180
-53
lines changed

.golangci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ linters-settings:
1616
local-prefixes: github.com/golangci/golangci-lint
1717
goconst:
1818
min-len: 2
19-
min-occurrences: 2
19+
min-occurrences: 3
2020
gocritic:
2121
enabled-tags:
2222
- diagnostic

docs/package-lock.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/docs/usage/configuration.mdx

+15-17
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,6 @@ To see a list of enabled by your configuration linters:
1212
golangci-lint linters
1313
```
1414

15-
## Command-Line Options
16-
17-
```sh
18-
golangci-lint run -h
19-
{.RunHelpText}
20-
```
21-
22-
When the `--cpu-profile-path` or `--mem-profile-path` arguments are specified, `golangci-lint` writes runtime profiling data
23-
in the format expected by the [pprof](https://github.com/google/pprof) visualization tool.
24-
25-
When the `--trace-path` argument is specified, `golangci-lint` writes runtime tracing data in the format expected by
26-
the `go tool trace` command and visualization tool.
27-
2815
## Config File
2916

3017
GolangCI-Lint looks for config files in the following paths from the current working directory:
@@ -41,13 +28,24 @@ To see which config file is being used and where it was sourced from run golangc
4128
Config options inside the file are identical to command-line options.
4229
You can configure specific linters' options only within the config file (not the command-line).
4330

44-
There is a [`.golangci.example.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) example
45-
config file with all supported options, their description and default value:
31+
There is a [`.golangci.example.yml`](https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml) file with all supported options, their description, and default values.
32+
This file is a neither a working example nor recommended configuration, it's just a reference to display all the configuration options.
4633

47-
```yaml
48-
{ .GolangciYamlExample }
34+
{ .ConfigurationExample }
35+
36+
## Command-Line Options
37+
38+
```sh
39+
golangci-lint run -h
40+
{.RunHelpText}
4941
```
5042

43+
When the `--cpu-profile-path` or `--mem-profile-path` arguments are specified, `golangci-lint` writes runtime profiling data
44+
in the format expected by the [pprof](https://github.com/google/pprof) visualization tool.
45+
46+
When the `--trace-path` argument is specified, `golangci-lint` writes runtime tracing data in the format expected by
47+
the `go tool trace` command and visualization tool.
48+
5149
## Cache
5250

5351
GolangCI-Lint stores its cache in the [default user cache directory](https://golang.org/pkg/os/#UserCacheDir).

scripts/expand_website_templates/main.go

+143-35
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func buildTemplateContext() (map[string]string, error) {
165165
return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err)
166166
}
167167

168-
lintersCfg, err := getLintersConfiguration(golangciYamlExample)
168+
snippets, err := extractExampleSnippets(golangciYamlExample)
169169
if err != nil {
170170
return nil, fmt.Errorf("can't read .golangci.example.yml: %s", err)
171171
}
@@ -202,8 +202,8 @@ func buildTemplateContext() (map[string]string, error) {
202202
}
203203

204204
return map[string]string{
205-
"LintersExample": lintersCfg,
206-
"GolangciYamlExample": strings.TrimSpace(string(golangciYamlExample)),
205+
"LintersExample": snippets.LintersSettings,
206+
"ConfigurationExample": snippets.ConfigurationFile,
207207
"LintersCommandOutputEnabledOnly": string(lintersOutParts[0]),
208208
"LintersCommandOutputDisabledOnly": string(lintersOutParts[1]),
209209
"EnabledByDefaultLinters": getLintersListMarkdown(true),
@@ -317,58 +317,166 @@ func getThanksList() string {
317317
return strings.Join(lines, "\n")
318318
}
319319

320-
func getLintersConfiguration(example []byte) (string, error) {
321-
builder := &strings.Builder{}
320+
type SettingSnippets struct {
321+
ConfigurationFile string
322+
LintersSettings string
323+
}
322324

325+
func extractExampleSnippets(example []byte) (*SettingSnippets, error) {
323326
var data yaml.Node
324327
err := yaml.Unmarshal(example, &data)
325328
if err != nil {
326-
return "", err
329+
return nil, err
327330
}
328331

329332
root := data.Content[0]
330333

334+
globalNode := &yaml.Node{
335+
Kind: root.Kind,
336+
Style: root.Style,
337+
Tag: root.Tag,
338+
Value: root.Value,
339+
Anchor: root.Anchor,
340+
Alias: root.Alias,
341+
HeadComment: root.HeadComment,
342+
LineComment: root.LineComment,
343+
FootComment: root.FootComment,
344+
Line: root.Line,
345+
Column: root.Column,
346+
}
347+
348+
snippets := SettingSnippets{}
349+
350+
builder := strings.Builder{}
351+
331352
for j, node := range root.Content {
332-
if node.Value != "linters-settings" {
353+
switch node.Value {
354+
case "run", "output", "linters", "linters-settings", "issues", "severity":
355+
default:
333356
continue
334357
}
335358

336-
nodes := root.Content[j+1]
337-
338-
for i := 0; i < len(nodes.Content); i += 2 {
339-
r := &yaml.Node{
340-
Kind: nodes.Kind,
341-
Style: nodes.Style,
342-
Tag: nodes.Tag,
343-
Value: node.Value,
344-
Content: []*yaml.Node{
345-
{
346-
Kind: root.Content[j].Kind,
347-
Value: root.Content[j].Value,
348-
},
349-
{
350-
Kind: nodes.Kind,
351-
Content: []*yaml.Node{nodes.Content[i], nodes.Content[i+1]},
352-
},
359+
nextNode := root.Content[j+1]
360+
361+
newNode := &yaml.Node{
362+
Kind: nextNode.Kind,
363+
Content: []*yaml.Node{
364+
{
365+
HeadComment: fmt.Sprintf("See the dedicated %q documentation section.", node.Value),
366+
Kind: node.Kind,
367+
Style: node.Style,
368+
Tag: node.Tag,
369+
Value: "option",
353370
},
354-
}
355-
356-
_, _ = fmt.Fprintf(builder, "### %s\n\n", nodes.Content[i].Value)
357-
_, _ = fmt.Fprintln(builder, "```yaml")
371+
{
372+
Kind: node.Kind,
373+
Style: node.Style,
374+
Tag: node.Tag,
375+
Value: "value",
376+
},
377+
},
378+
}
358379

359-
const ident = 2
360-
encoder := yaml.NewEncoder(builder)
361-
encoder.SetIndent(ident)
380+
globalNode.Content = append(globalNode.Content, node, newNode)
362381

363-
err = encoder.Encode(r)
382+
if node.Value == "linters-settings" {
383+
snippets.LintersSettings, err = getLintersSettingSnippets(node, nextNode)
364384
if err != nil {
365-
return "", err
385+
return nil, err
366386
}
367387

368-
_, _ = fmt.Fprintln(builder, "```")
369-
_, _ = fmt.Fprintln(builder)
388+
_, _ = builder.WriteString(
389+
fmt.Sprintf(
390+
"### `%s` configuration\n\nSee the dedicated [linters-settings](/usage/linters) documentation section.\n\n",
391+
node.Value,
392+
),
393+
)
394+
continue
395+
}
396+
397+
nodeSection := &yaml.Node{
398+
Kind: root.Kind,
399+
Style: root.Style,
400+
Tag: root.Tag,
401+
Value: root.Value,
402+
Content: []*yaml.Node{node, nextNode},
403+
}
404+
405+
snippet, errSnip := marshallSnippet(nodeSection)
406+
if errSnip != nil {
407+
return nil, errSnip
408+
}
409+
410+
_, _ = builder.WriteString(fmt.Sprintf("### `%s` configuration\n\n%s", node.Value, snippet))
411+
}
412+
413+
overview, err := marshallSnippet(globalNode)
414+
if err != nil {
415+
return nil, err
416+
}
417+
418+
snippets.ConfigurationFile = overview + builder.String()
419+
420+
return &snippets, nil
421+
}
422+
423+
func getLintersSettingSnippets(node, nextNode *yaml.Node) (string, error) {
424+
builder := &strings.Builder{}
425+
426+
for i := 0; i < len(nextNode.Content); i += 2 {
427+
r := &yaml.Node{
428+
Kind: nextNode.Kind,
429+
Style: nextNode.Style,
430+
Tag: nextNode.Tag,
431+
Value: node.Value,
432+
Content: []*yaml.Node{
433+
{
434+
Kind: node.Kind,
435+
Value: node.Value,
436+
},
437+
{
438+
Kind: nextNode.Kind,
439+
Content: []*yaml.Node{nextNode.Content[i], nextNode.Content[i+1]},
440+
},
441+
},
370442
}
443+
444+
_, _ = fmt.Fprintf(builder, "### %s\n\n", nextNode.Content[i].Value)
445+
_, _ = fmt.Fprintln(builder, "```yaml")
446+
447+
encoder := yaml.NewEncoder(builder)
448+
encoder.SetIndent(2)
449+
450+
err := encoder.Encode(r)
451+
if err != nil {
452+
return "", err
453+
}
454+
455+
_, _ = fmt.Fprintln(builder, "```")
456+
_, _ = fmt.Fprintln(builder)
371457
}
372458

373459
return builder.String(), nil
374460
}
461+
462+
func marshallSnippet(node *yaml.Node) (string, error) {
463+
builder := &strings.Builder{}
464+
465+
if node.Value != "" {
466+
_, _ = fmt.Fprintf(builder, "### %s\n\n", node.Value)
467+
}
468+
_, _ = fmt.Fprintln(builder, "```yaml")
469+
470+
encoder := yaml.NewEncoder(builder)
471+
encoder.SetIndent(2)
472+
473+
err := encoder.Encode(node)
474+
if err != nil {
475+
return "", err
476+
}
477+
478+
_, _ = fmt.Fprintln(builder, "```")
479+
_, _ = fmt.Fprintln(builder)
480+
481+
return builder.String(), nil
482+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func Test_extractExampleSnippets(t *testing.T) {
11+
t.Skip("only for debugging purpose")
12+
13+
example, err := os.ReadFile("../../../golangci-lint/.golangci.example.yml")
14+
require.NoError(t, err)
15+
16+
m, err := extractExampleSnippets(example)
17+
require.NoError(t, err)
18+
19+
t.Log(m)
20+
}

0 commit comments

Comments
 (0)