-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.go
More file actions
241 lines (208 loc) · 6.37 KB
/
check.go
File metadata and controls
241 lines (208 loc) · 6.37 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/stacktodate/stacktodate-cli/cmd/helpers"
"github.com/spf13/cobra"
)
type CheckResult struct {
Status string `json:"status"`
Summary CheckSummary `json:"summary"`
Results CheckResults `json:"results"`
}
type CheckSummary struct {
Matches int `json:"matches"`
Mismatches int `json:"mismatches"`
MissingConfig int `json:"missing_config"`
}
type CheckResults struct {
Matched []ComparisonEntry `json:"matched"`
Mismatched []ComparisonEntry `json:"mismatched"`
MissingConfig []ComparisonEntry `json:"missing_config"`
}
type ComparisonEntry struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Detected string `json:"detected,omitempty"`
Source string `json:"source,omitempty"`
}
var (
checkConfigFile string
checkFormat string
)
var checkCmd = &cobra.Command{
Use: "check",
Short: "Check if detected versions match stacktodate.yml",
Long: `Verify that the versions in stacktodate.yml match the currently detected versions in your project. Useful for CI/CD pipelines.`,
Run: func(cmd *cobra.Command, args []string) {
// Load config without requiring UUID
config, err := helpers.LoadConfig(checkConfigFile)
if err != nil {
helpers.ExitWithError(2, "failed to load config: %v", err)
}
// Resolve absolute path for directory management
absConfigPath, err := helpers.ResolveAbsPath(checkConfigFile)
if err != nil {
if checkConfigFile == "" {
absConfigPath, _ = helpers.ResolveAbsPath("stacktodate.yml")
} else {
helpers.ExitOnError(err, "failed to resolve config path")
}
}
// Get config directory
configDir, err := helpers.GetConfigDir(absConfigPath)
if err != nil {
helpers.ExitOnError(err, "failed to get config directory")
}
// Detect current versions in config directory
var detectedStack map[string]helpers.StackEntry
err = helpers.WithWorkingDir(configDir, func() error {
detectedInfo := DetectProjectInfo()
detectedStack = normalizeDetectedToStack(detectedInfo)
return nil
})
if err != nil {
helpers.ExitOnError(err, "failed to detect versions")
}
// Compare stacks
result := compareStacks(config.Stack, detectedStack)
// Output results
if checkFormat == "json" {
outputJSON(result)
} else {
outputText(result)
}
// Exit with appropriate code
if result.Status != "match" {
os.Exit(1)
}
},
}
func normalizeDetectedToStack(info DetectedInfo) map[string]helpers.StackEntry {
normalized := make(map[string]helpers.StackEntry)
if len(info.Ruby) > 0 {
normalized["ruby"] = helpers.StackEntry{
Version: info.Ruby[0].Value,
Source: info.Ruby[0].Source,
}
}
if len(info.Rails) > 0 {
normalized["rails"] = helpers.StackEntry{
Version: info.Rails[0].Value,
Source: info.Rails[0].Source,
}
}
if len(info.Node) > 0 {
normalized["nodejs"] = helpers.StackEntry{
Version: info.Node[0].Value,
Source: info.Node[0].Source,
}
}
if len(info.Go) > 0 {
normalized["go"] = helpers.StackEntry{
Version: info.Go[0].Value,
Source: info.Go[0].Source,
}
}
if len(info.Python) > 0 {
normalized["python"] = helpers.StackEntry{
Version: info.Python[0].Value,
Source: info.Python[0].Source,
}
}
return normalized
}
func compareStacks(configStack, detectedStack map[string]helpers.StackEntry) CheckResult {
result := CheckResult{
Results: CheckResults{
Matched: []ComparisonEntry{},
Mismatched: []ComparisonEntry{},
MissingConfig: []ComparisonEntry{},
},
}
// Check all items in config
for tech, configEntry := range configStack {
if detectedEntry, exists := detectedStack[tech]; exists {
if configEntry.Version == detectedEntry.Version {
result.Results.Matched = append(result.Results.Matched, ComparisonEntry{
Name: tech,
Version: configEntry.Version,
Detected: detectedEntry.Version,
Source: detectedEntry.Source,
})
result.Summary.Matches++
} else {
result.Results.Mismatched = append(result.Results.Mismatched, ComparisonEntry{
Name: tech,
Version: configEntry.Version,
Detected: detectedEntry.Version,
Source: detectedEntry.Source,
})
result.Summary.Mismatches++
}
} else {
result.Results.MissingConfig = append(result.Results.MissingConfig, ComparisonEntry{
Name: tech,
Version: configEntry.Version,
Source: configEntry.Source,
})
result.Summary.MissingConfig++
}
}
// Determine overall status
if result.Summary.Mismatches == 0 && result.Summary.MissingConfig == 0 {
result.Status = "match"
} else {
result.Status = "mismatch"
}
return result
}
func outputText(result CheckResult) {
fmt.Println("Technology Check Results")
fmt.Println("========================")
fmt.Println()
if len(result.Results.Matched) > 0 {
fmt.Printf("MATCH (%d):\n", len(result.Results.Matched))
for _, entry := range result.Results.Matched {
fmt.Printf(" %-12s %s == %s ✓\n", entry.Name+":", entry.Version, entry.Detected)
}
fmt.Println()
}
if len(result.Results.Mismatched) > 0 {
fmt.Printf("MISMATCH (%d):\n", len(result.Results.Mismatched))
for _, entry := range result.Results.Mismatched {
fmt.Printf(" %-12s %s != %s (config has %s)\n", entry.Name+":", entry.Detected, entry.Version, entry.Version)
}
fmt.Println()
}
if len(result.Results.MissingConfig) > 0 {
fmt.Printf("MISSING FROM DETECTION (%d):\n", len(result.Results.MissingConfig))
for _, entry := range result.Results.MissingConfig {
fmt.Printf(" %-12s %s (in config but not detected)\n", entry.Name+":", entry.Version)
}
fmt.Println()
}
fmt.Printf("Summary: %d match, %d mismatch, %d missing\n",
result.Summary.Matches,
result.Summary.Mismatches,
result.Summary.MissingConfig)
if result.Status == "mismatch" {
fmt.Println("Exit code: 1 (has differences)")
} else {
fmt.Println("Exit code: 0 (all match)")
}
}
func outputJSON(result CheckResult) {
data, err := json.MarshalIndent(result, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "Error marshaling JSON: %v\n", err)
os.Exit(2)
}
fmt.Println(string(data))
}
func init() {
rootCmd.AddCommand(checkCmd)
checkCmd.Flags().StringVarP(&checkConfigFile, "config", "c", "", "Path to stacktodate.yml config file (default: stacktodate.yml)")
checkCmd.Flags().StringVarP(&checkFormat, "format", "f", "text", "Output format: text or json (default: text)")
}