Skip to content

Commit b4a8b48

Browse files
committed
fix the crd templating command
- make depth configurable - restructure the fake files for tests - use yaml v3 - do a `go mod tidy` prior to building fixes #10
1 parent acff685 commit b4a8b48

File tree

7 files changed

+26
-30
lines changed

7 files changed

+26
-30
lines changed

hack/build.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
set -e -x -u
44

5-
# go test ./...
5+
go mod tidy
6+
go test ./...
67
go fmt ./cmd/... ./pkg/...
78

89
# build without website assets

pkg/cmd/crd.go

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"strings"
1110

1211
"github.com/cnoe-io/cnoe-cli/pkg/models"
1312
"github.com/spf13/cobra"
14-
"gopkg.in/yaml.v2"
13+
"gopkg.in/yaml.v3"
1514
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1615
)
1716

@@ -27,6 +26,7 @@ var (
2726
templatePath string
2827
verifiers []string
2928
namespaced bool
29+
depth uint32
3030

3131
templateName string
3232
templateTitle string
@@ -38,6 +38,7 @@ func init() {
3838
crdCmd.Flags().StringVarP(&templatePath, "templatePath", "t", "scaffolding/template.yaml", "path to the template to be augmented with backstage info")
3939
crdCmd.Flags().StringArrayVarP(&verifiers, "verifier", "v", []string{}, "list of verifiers to test the resource against")
4040
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")
4142

4243
crdCmd.Flags().StringVarP(&templateName, "templateName", "", "", "sets the name of the template")
4344
crdCmd.Flags().StringVarP(&templateTitle, "templateTitle", "", "", "sets the title of the template")
@@ -109,14 +110,14 @@ func Crd(
109110
return nil
110111
}
111112

112-
func defs(dir string, depth int) []string {
113-
if depth > 2 {
113+
func defs(dir string, currentDepth uint32) []string {
114+
if currentDepth > depth {
114115
return nil
115116
}
116117

117118
var out []string
118119
base, _ := filepath.Abs(dir)
119-
files, _ := ioutil.ReadDir(base)
120+
files, _ := os.ReadDir(base)
120121
for _, file := range files {
121122
f := filepath.Join(base, file.Name())
122123
stat, _ := os.Stat(f)
@@ -151,14 +152,15 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
151152
}
152153

153154
for _, def := range defs {
154-
data, err := ioutil.ReadFile(def)
155+
data, err := os.ReadFile(def)
155156
if err != nil {
156157
continue
157158
}
158159

159160
var doc models.Definition
160161
err = yaml.Unmarshal(data, &doc)
161162
if err != nil {
163+
fmt.Printf("failed to read %s. This file will be excluded. %s", def, err)
162164
continue
163165
}
164166

@@ -182,8 +184,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
182184
} else {
183185
value, err = ConvertMap(v)
184186
if err != nil {
185-
fmt.Fprintf(stdout, "failed %s: %s \n", def, err.Error())
186-
continue
187+
return cmdOutput{}, err
187188
}
188189
}
189190

@@ -243,7 +244,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd
243244
}
244245

245246
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)
247248
if err != nil {
248249
fmt.Fprintf(stdout, "failed %s: %s \n", def, err.Error())
249250
continue
@@ -261,7 +262,7 @@ func writeToTemplate(
261262
templateFile string, outputPath string, identifiedResources []string, position int,
262263
templateName, templateTitle, templateDescription string,
263264
) error {
264-
templateData, err := ioutil.ReadFile(templateFile)
265+
templateData, err := os.ReadFile(templateFile)
265266
if err != nil {
266267
return err
267268
}
@@ -316,7 +317,7 @@ func writeToTemplate(
316317
return err
317318
}
318319

319-
err = ioutil.WriteFile(fmt.Sprintf("%s/template.yaml", outputPath), outputData, 0644)
320+
err = os.WriteFile(fmt.Sprintf("%s/template.yaml", outputPath), outputData, 0644)
320321
if err != nil {
321322
return err
322323
}
@@ -334,40 +335,34 @@ func ConvertSlice(strSlice []string) []interface{} {
334335
}
335336

336337
func ConvertMap(originalData interface{}) (map[string]interface{}, error) {
337-
originalMap, ok := originalData.(map[interface{}]interface{})
338+
originalMap, ok := originalData.(map[string]interface{})
338339
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{}")
340341
}
341342

342343
convertedMap := make(map[string]interface{})
343344

344345
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-
351346
switch v := value.(type) {
352347
case map[interface{}]interface{}:
353348
// If the value is a nested map, recursively convert it
354349
var err error
355-
convertedMap[strKey], err = ConvertMap(v)
350+
convertedMap[key], err = ConvertMap(v)
356351
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))
358353
}
359354
case int:
360-
convertedMap[strKey] = int64(v)
355+
convertedMap[key] = int64(v)
361356
case int32:
362-
convertedMap[strKey] = int64(v)
357+
convertedMap[key] = int64(v)
363358
case []interface{}:
364359
dv := make([]interface{}, len(v))
365360
for i, ve := range v {
366361
switch ive := ve.(type) {
367362
case map[interface{}]interface{}:
368363
ivec, err := ConvertMap(ive)
369364
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))
371366
}
372367
dv[i] = ivec
373368
case int:
@@ -378,10 +373,10 @@ func ConvertMap(originalData interface{}) (map[string]interface{}, error) {
378373
dv[i] = ive
379374
}
380375
}
381-
convertedMap[strKey] = dv
376+
convertedMap[key] = dv
382377
default:
383378
// Otherwise, add the key-value pair to the converted map
384-
convertedMap[strKey] = v
379+
convertedMap[key] = v
385380
}
386381
}
387382

pkg/cmd/crd_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ var _ = Describe("Template", func() {
2929
templateTitle = "test-title"
3030
templateDescription = "test-description"
3131

32-
inputDir = "./fakes/in-resource"
33-
invalidInputDir = "./fakes/invalid-in-resource"
32+
inputDir = "./fakes/crd/in-resource"
33+
invalidInputDir = "./fakes/crd/invalid-in-resource"
34+
expectedResourceFile = "./fakes/crd/out-resource/output-resource.yaml"
3435
templateFile = "./fakes/template/input-template.yaml"
3536
expectedTemplateFile = "./fakes/template/output-template.yaml"
36-
expectedResourceFile = "./fakes/out-resource/output-resource.yaml"
3737
)
3838

3939
BeforeEach(func() {

0 commit comments

Comments
 (0)