Skip to content

Commit f219063

Browse files
feat: write only enabled to config by default
1 parent 3b65632 commit f219063

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

config/config.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ func LookupAndParse() (*lint.Config, error) {
159159
return conf, nil
160160
}
161161

162-
// WriteTo writes config in yaml format to given io.Writer
162+
// WriteTo writes config in yaml format to given io.Writer, including all
163+
// settings and every field even if empty or zero-valued.
163164
func WriteTo(w io.Writer, conf *lint.Config) (retErr error) {
164165
enc := yaml.NewEncoder(w)
165166
defer func() {
@@ -171,6 +172,30 @@ func WriteTo(w io.Writer, conf *lint.Config) (retErr error) {
171172
return enc.Encode(conf)
172173
}
173174

175+
// WriteCompactTo writes config in yaml format to given io.Writer.
176+
// Only settings for enabled rules are written, keeping the output compact.
177+
func WriteCompactTo(w io.Writer, conf *lint.Config) error {
178+
// Build a compact copy: only settings for enabled rules
179+
compact := *conf
180+
if len(compact.Rules) > 0 && len(compact.Settings) > 0 {
181+
enabled := make(map[string]struct{}, len(compact.Rules))
182+
for _, r := range compact.Rules {
183+
enabled[r] = struct{}{}
184+
}
185+
filtered := make(map[string]lint.RuleSetting, len(compact.Rules))
186+
for name, setting := range compact.Settings {
187+
if _, ok := enabled[name]; ok {
188+
filtered[name] = setting
189+
}
190+
}
191+
compact.Settings = filtered
192+
}
193+
194+
enc := yaml.NewEncoder(w)
195+
defer enc.Close()
196+
return enc.Encode(&compact)
197+
}
198+
174199
func isValidVersion(versionNo string) error {
175200
if versionNo == "" {
176201
return errors.New("version is empty")

internal/cmd/cmd.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,16 @@ func newConfigCmd() *cli.Command {
121121
Usage: "Output file name",
122122
Value: ".commitlint.yaml",
123123
},
124+
&cli.BoolFlag{
125+
Name: "all",
126+
Usage: "Write all settings (not just enabled rules)",
127+
},
124128
},
125129
Action: func(ctx *cli.Context) error {
126130
isReplace := ctx.Bool("replace")
127131
fileName := ctx.String("file")
128-
err := configCreate(fileName, isReplace)
132+
all := ctx.Bool("all")
133+
err := configCreate(fileName, isReplace, all)
129134
if err != nil {
130135
if isConfExists(err) {
131136
fmt.Println("config file already exists")

internal/cmd/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// configCreate is the callback function for create config command
12-
func configCreate(fileName string, isReplace bool) (retErr error) {
12+
func configCreate(fileName string, isReplace bool, all bool) (retErr error) {
1313
outPath := filepath.Join(".", fileName)
1414
// if config file already exists skip creating or overwriting it
1515
if _, err := os.Stat(outPath); !os.IsNotExist(err) {
@@ -39,7 +39,10 @@ func configCreate(fileName string, isReplace bool) (retErr error) {
3939
}()
4040

4141
defConf := config.NewDefault()
42-
return config.WriteTo(w, defConf)
42+
if all {
43+
return config.WriteTo(w, defConf)
44+
}
45+
return config.WriteCompactTo(w, defConf)
4346
}
4447

4548
// configCheck is the callback function for check/verify command

lint/config.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package lint
33
// RuleSetting represent config for a rule
44
type RuleSetting struct {
55
Argument interface{} `yaml:"argument"`
6-
Flags map[string]interface{} `yaml:"flags,omitempty"`
6+
Flags map[string]interface{} `yaml:"flags"`
77
}
88

99
// SeverityConfig represent severity levels for rules
1010
type SeverityConfig struct {
1111
Default Severity `yaml:"default"`
12-
Rules map[string]Severity `yaml:"rules,omitempty"`
12+
Rules map[string]Severity `yaml:"rules"`
1313
}
1414

1515
// Config represent linter config
@@ -36,13 +36,13 @@ type Config struct {
3636

3737
// DisableDefaultIgnores disables the built-in ignore patterns
3838
// (merge, revert, fixup, squash, etc.) when set to true.
39-
DisableDefaultIgnores bool `yaml:"disable-default-ignores,omitempty"`
39+
DisableDefaultIgnores bool `yaml:"disable-default-ignores"`
4040

4141
// IgnorePatterns is a list of user-defined regex patterns.
4242
// If the first line of the commit message matches any pattern,
4343
// linting is skipped. These are added on top of the default
4444
// patterns (unless DisableDefaultIgnores is true).
45-
IgnorePatterns []string `yaml:"ignores,omitempty"`
45+
IgnorePatterns []string `yaml:"ignores"`
4646

4747
// DefaultIgnorePatterns holds the built-in patterns (set by config package).
4848
// Not serialized to YAML - users never set this directly.

0 commit comments

Comments
 (0)