1
1
package main
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"os"
6
7
"path/filepath"
7
8
8
9
"github.com/idsulik/helm-cel/pkg/generator"
10
+ "github.com/idsulik/helm-cel/pkg/models"
9
11
"github.com/idsulik/helm-cel/pkg/validator"
10
12
"github.com/spf13/cobra"
13
+ "gopkg.in/yaml.v3"
11
14
)
12
15
13
16
var (
@@ -17,16 +20,19 @@ var (
17
20
outputFile string
18
21
19
22
// Flags for validate command
20
- valuesFiles []string
21
- rulesFiles []string
23
+ valuesFiles []string
24
+ rulesFiles []string
25
+ outputFormat string
22
26
)
23
27
24
28
const (
25
29
validateShort = "Validate Helm values using CEL expressions"
26
30
validateLong = `A Helm plugin to validate values.yaml using CEL expressions defined in .cel.yaml files.
27
31
Example using defaults: helm cel validate ./mychart
28
32
Example with specific values: helm cel validate ./mychart -v values1.yaml -v values2.yaml
29
- Example with multiple files: helm cel validate ./mychart -v prod.yaml,staging.yaml -r rules1.cel.yaml,rules2.cel.yaml`
33
+ Example with multiple files: helm cel validate ./mychart -v prod.yaml,staging.yaml -r rules1.cel.yaml,rules2.cel.yaml
34
+ Example with JSON output: helm cel validate ./mychart -o json
35
+ Example with YAML output: helm cel validate ./mychart -o yaml`
30
36
31
37
generateShort = "Generate CEL validation rules from values.yaml"
32
38
generateLong = `Generate values.cel.yaml file with validation rules based on the structure of values.yaml.
@@ -73,6 +79,13 @@ func init() {
73
79
[]string {"values.cel.yaml" },
74
80
"Rules files to validate against (comma-separated or multiple -r flags)" ,
75
81
)
82
+ validateCmd .Flags ().StringVarP (
83
+ & outputFormat ,
84
+ "output" ,
85
+ "o" ,
86
+ "text" ,
87
+ "Output format: text, json, or yaml" ,
88
+ )
76
89
77
90
generateCmd .Flags ().BoolVarP (& forceOverwrite , "force" , "f" , false , "Force overwrite existing values.cel.yaml" )
78
91
generateCmd .Flags ().StringVarP (
@@ -116,21 +129,56 @@ func runValidator(_ *cobra.Command, args []string) error {
116
129
return err
117
130
}
118
131
119
- if result .HasErrors () {
120
- return result
132
+ output := models.ValidationOutput {
133
+ HasErrors : result .HasErrors (),
134
+ HasWarnings : len (result .Warnings ) > 0 ,
135
+ Result : result ,
121
136
}
122
137
123
- if len (result .Warnings ) > 0 {
124
- fmt .Println (result .Error ())
125
- fmt .Println ("-------------------------------------------------" )
126
- fmt .Println ("⚠️✅ Values validation successful with warnings!" )
127
- } else {
128
- fmt .Println ("✅ Values validation successful!" )
138
+ switch outputFormat {
139
+ case "json" :
140
+ if err := outputJson (output ); err != nil {
141
+ return err
142
+ }
143
+ case "yaml" :
144
+ if err := outputYaml (output ); err != nil {
145
+ return err
146
+ }
147
+ default :
148
+ if result .HasErrors () {
149
+ return result
150
+ }
151
+
152
+ if len (result .Warnings ) > 0 {
153
+ fmt .Println (result .Error ())
154
+ fmt .Println ("-------------------------------------------------" )
155
+ fmt .Println ("⚠️✅ Values validation successful with warnings!" )
156
+ } else {
157
+ fmt .Println ("✅ Values validation successful!" )
158
+ }
129
159
}
130
160
131
161
return nil
132
162
}
133
163
164
+ func outputJson (output models.ValidationOutput ) error {
165
+ json , err := json .MarshalIndent (output , "" , " " )
166
+ if err != nil {
167
+ return fmt .Errorf ("failed to marshal output to JSON: %v" , err )
168
+ }
169
+ fmt .Println (string (json ))
170
+ return nil
171
+ }
172
+
173
+ func outputYaml (output models.ValidationOutput ) error {
174
+ yaml , err := yaml .Marshal (output )
175
+ if err != nil {
176
+ return fmt .Errorf ("failed to marshal output to YAML: %v" , err )
177
+ }
178
+ fmt .Println (string (yaml ))
179
+ return nil
180
+ }
181
+
134
182
func runGenerator (_ * cobra.Command , args []string ) error {
135
183
if len (args ) != 1 {
136
184
return fmt .Errorf ("chart path is required" )
0 commit comments