@@ -4,14 +4,13 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"io"
7
- "io/ioutil"
8
7
"os"
9
8
"path/filepath"
10
9
"strings"
11
10
12
11
"github.com/cnoe-io/cnoe-cli/pkg/models"
13
12
"github.com/spf13/cobra"
14
- "gopkg.in/yaml.v2 "
13
+ "gopkg.in/yaml.v3 "
15
14
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
16
15
)
17
16
27
26
templatePath string
28
27
verifiers []string
29
28
namespaced bool
29
+ depth uint32
30
30
31
31
templateName string
32
32
templateTitle string
@@ -38,6 +38,7 @@ func init() {
38
38
crdCmd .Flags ().StringVarP (& templatePath , "templatePath" , "t" , "scaffolding/template.yaml" , "path to the template to be augmented with backstage info" )
39
39
crdCmd .Flags ().StringArrayVarP (& verifiers , "verifier" , "v" , []string {}, "list of verifiers to test the resource against" )
40
40
crdCmd .Flags ().BoolVarP (& namespaced , "namespaced" , "n" , false , "whether or not resources are namespaced" )
41
+ crdCmd .Flags ().Uint32Var (& depth , "depth" , 2 , "depth from given directory to search for CRDs/XRDs" )
41
42
42
43
crdCmd .Flags ().StringVarP (& templateName , "templateName" , "" , "" , "sets the name of the template" )
43
44
crdCmd .Flags ().StringVarP (& templateTitle , "templateTitle" , "" , "" , "sets the title of the template" )
@@ -109,14 +110,14 @@ func Crd(
109
110
return nil
110
111
}
111
112
112
- func defs (dir string , depth int ) []string {
113
- if depth > 2 {
113
+ func defs (dir string , currentDepth uint32 ) []string {
114
+ if currentDepth > depth {
114
115
return nil
115
116
}
116
117
117
118
var out []string
118
119
base , _ := filepath .Abs (dir )
119
- files , _ := ioutil .ReadDir (base )
120
+ files , _ := os .ReadDir (base )
120
121
for _ , file := range files {
121
122
f := filepath .Join (base , file .Name ())
122
123
stat , _ := os .Stat (f )
@@ -151,14 +152,15 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
151
152
}
152
153
153
154
for _ , def := range defs {
154
- data , err := ioutil .ReadFile (def )
155
+ data , err := os .ReadFile (def )
155
156
if err != nil {
156
157
continue
157
158
}
158
159
159
160
var doc models.Definition
160
161
err = yaml .Unmarshal (data , & doc )
161
162
if err != nil {
163
+ fmt .Printf ("failed to read %s. This file will be excluded. %s" , def , err )
162
164
continue
163
165
}
164
166
@@ -182,8 +184,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
182
184
} else {
183
185
value , err = ConvertMap (v )
184
186
if err != nil {
185
- fmt .Fprintf (stdout , "failed %s: %s \n " , def , err .Error ())
186
- continue
187
+ return cmdOutput {}, err
187
188
}
188
189
}
189
190
@@ -243,7 +244,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
243
244
}
244
245
245
246
template := fmt .Sprintf ("%s/%s.yaml" , templateOutputDir , strings .ToLower (resourceName ))
246
- err = ioutil .WriteFile (template , []byte (wrapperData ), 0644 )
247
+ err = os .WriteFile (template , []byte (wrapperData ), 0644 )
247
248
if err != nil {
248
249
fmt .Fprintf (stdout , "failed %s: %s \n " , def , err .Error ())
249
250
continue
@@ -261,7 +262,7 @@ func writeToTemplate(
261
262
templateFile string , outputPath string , identifiedResources []string , position int ,
262
263
templateName , templateTitle , templateDescription string ,
263
264
) error {
264
- templateData , err := ioutil .ReadFile (templateFile )
265
+ templateData , err := os .ReadFile (templateFile )
265
266
if err != nil {
266
267
return err
267
268
}
@@ -316,7 +317,7 @@ func writeToTemplate(
316
317
return err
317
318
}
318
319
319
- err = ioutil .WriteFile (fmt .Sprintf ("%s/template.yaml" , outputPath ), outputData , 0644 )
320
+ err = os .WriteFile (fmt .Sprintf ("%s/template.yaml" , outputPath ), outputData , 0644 )
320
321
if err != nil {
321
322
return err
322
323
}
@@ -334,40 +335,34 @@ func ConvertSlice(strSlice []string) []interface{} {
334
335
}
335
336
336
337
func ConvertMap (originalData interface {}) (map [string ]interface {}, error ) {
337
- originalMap , ok := originalData .(map [interface {} ]interface {})
338
+ originalMap , ok := originalData .(map [string ]interface {})
338
339
if ! ok {
339
- return nil , errors .New ("failed to convert to interface map" )
340
+ return nil , errors .New ("conversion failed: data is not map[string]interface{} " )
340
341
}
341
342
342
343
convertedMap := make (map [string ]interface {})
343
344
344
345
for key , value := range originalMap {
345
- strKey , ok := key .(string )
346
- if ! ok {
347
- // Skip the key if it cannot be converted to string
348
- continue
349
- }
350
-
351
346
switch v := value .(type ) {
352
347
case map [interface {}]interface {}:
353
348
// If the value is a nested map, recursively convert it
354
349
var err error
355
- convertedMap [strKey ], err = ConvertMap (v )
350
+ convertedMap [key ], err = ConvertMap (v )
356
351
if err != nil {
357
- return nil , errors .New (fmt .Sprintf ("failed to convert for key %s" , strKey ))
352
+ return nil , errors .New (fmt .Sprintf ("failed to convert for key %s" , key ))
358
353
}
359
354
case int :
360
- convertedMap [strKey ] = int64 (v )
355
+ convertedMap [key ] = int64 (v )
361
356
case int32 :
362
- convertedMap [strKey ] = int64 (v )
357
+ convertedMap [key ] = int64 (v )
363
358
case []interface {}:
364
359
dv := make ([]interface {}, len (v ))
365
360
for i , ve := range v {
366
361
switch ive := ve .(type ) {
367
362
case map [interface {}]interface {}:
368
363
ivec , err := ConvertMap (ive )
369
364
if err != nil {
370
- return nil , errors .New (fmt .Sprintf ("failed to convert for key %s" , strKey ))
365
+ return nil , errors .New (fmt .Sprintf ("failed to convert for key %s" , key ))
371
366
}
372
367
dv [i ] = ivec
373
368
case int :
@@ -378,10 +373,10 @@ func ConvertMap(originalData interface{}) (map[string]interface{}, error) {
378
373
dv [i ] = ive
379
374
}
380
375
}
381
- convertedMap [strKey ] = dv
376
+ convertedMap [key ] = dv
382
377
default :
383
378
// Otherwise, add the key-value pair to the converted map
384
- convertedMap [strKey ] = v
379
+ convertedMap [key ] = v
385
380
}
386
381
}
387
382
0 commit comments