-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodernize.go
More file actions
178 lines (152 loc) · 4.44 KB
/
modernize.go
File metadata and controls
178 lines (152 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"flag"
"fmt"
"os"
"github.com/GoCodeAlone/workflow/modernize"
"gopkg.in/yaml.v3"
)
func runModernize(args []string) error {
fs := flag.NewFlagSet("modernize", flag.ContinueOnError)
apply := fs.Bool("apply", false, "Apply fixes in-place (default: dry-run)")
listRules := fs.Bool("list-rules", false, "List all available modernize rules")
rulesFlag := fs.String("rules", "", "Comma-separated list of rule IDs to run (default: all)")
excludeFlag := fs.String("exclude-rules", "", "Comma-separated list of rule IDs to skip")
format := fs.String("format", "text", "Output format: text or json")
dir := fs.String("dir", "", "Scan all YAML files in a directory (recursive)")
pluginDir := fs.String("plugin-dir", "", "Directory of installed external plugins; their modernize rules are loaded")
fs.Usage = func() {
fmt.Fprintf(fs.Output(), `Usage: wfctl modernize [options] <config.yaml> [config2.yaml ...]
Detect and fix known YAML config anti-patterns.
By default runs in dry-run mode (report only). Use --apply to write fixes.
Examples:
wfctl modernize config/app.yaml
wfctl modernize --apply config/app.yaml
wfctl modernize --dir ./config/
wfctl modernize --rules hyphen-steps,conditional-field config.yaml
wfctl modernize --list-rules
wfctl modernize --plugin-dir data/plugins config.yaml
Options:
`)
fs.PrintDefaults()
}
args = reorderFlags(args)
if err := fs.Parse(args); err != nil {
return err
}
rules := modernize.AllRules()
// Load additional modernize rules from installed external plugins.
if *pluginDir != "" {
pluginRules, err := modernize.LoadRulesFromDir(*pluginDir)
if err != nil {
return fmt.Errorf("failed to load plugin rules from %s: %w", *pluginDir, err)
}
rules = append(rules, pluginRules...)
}
if *listRules {
fmt.Println("Available modernize rules:")
fmt.Println()
for _, r := range rules {
fixable := "fixable"
if r.Fix == nil {
fixable = "detect-only"
}
fmt.Printf(" %-24s [%-7s] [%-11s] %s\n", r.ID, r.Severity, fixable, r.Description)
}
return nil
}
// Filter rules
rules = modernize.FilterRules(rules, *rulesFlag, *excludeFlag)
// Collect files
var files []string
if *dir != "" {
found, err := findYAMLFiles(*dir)
if err != nil {
return fmt.Errorf("scan directory %s: %w", *dir, err)
}
files = append(files, found...)
}
files = append(files, fs.Args()...)
if len(files) == 0 {
fs.Usage()
return fmt.Errorf("at least one config file or --dir is required")
}
totalFindings := 0
totalFixes := 0
for _, f := range files {
findings, fixes, err := modernizeFile(f, rules, *apply)
if err != nil {
fmt.Fprintf(os.Stderr, " SKIP %s: %v\n", f, err)
continue
}
totalFindings += len(findings)
totalFixes += fixes
if len(findings) == 0 {
continue
}
switch *format {
case "json":
// JSON output handled after all files
default:
fmt.Printf("%s:\n", f)
for _, finding := range findings {
fixable := ""
if finding.Fixable {
fixable = " (fixable)"
}
fmt.Printf(" line %d: [%s] %s%s\n", finding.Line, finding.RuleID, finding.Message, fixable)
}
fmt.Println()
}
}
// Summary
if totalFindings == 0 {
fmt.Println("No issues found.")
return nil
}
if *apply {
fmt.Printf("%d fix(es) applied across %d finding(s).\n", totalFixes, totalFindings)
} else {
fmt.Printf("%d issue(s) found. Run with --apply to fix.\n", totalFindings)
}
return nil
}
// modernizeFile checks (and optionally fixes) a single YAML file.
func modernizeFile(path string, rules []modernize.Rule, apply bool) ([]modernize.Finding, int, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, 0, err
}
var doc yaml.Node
if err := yaml.Unmarshal(data, &doc); err != nil {
return nil, 0, fmt.Errorf("parse YAML: %w", err)
}
// Check phase
var allFindings []modernize.Finding
for _, r := range rules {
findings := r.Check(&doc, data)
allFindings = append(allFindings, findings...)
}
if !apply || len(allFindings) == 0 {
return allFindings, 0, nil
}
// Fix phase
fixCount := 0
for _, r := range rules {
if r.Fix == nil {
continue
}
changes := r.Fix(&doc)
fixCount += len(changes)
}
if fixCount > 0 {
out, err := yaml.Marshal(&doc)
if err != nil {
return allFindings, 0, fmt.Errorf("marshal fixed YAML: %w", err)
}
if err := os.WriteFile(path, out, 0600); err != nil {
return allFindings, 0, fmt.Errorf("write fixed file: %w", err)
}
}
return allFindings, fixCount, nil
}