From b4a8b48d650e71b467ea3ab8bb6b3671c68a24dd Mon Sep 17 00:00:00 2001 From: Nima Kaviani Date: Thu, 10 Aug 2023 18:20:17 -0700 Subject: [PATCH] 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 --- hack/build.sh | 3 +- pkg/cmd/crd.go | 47 +++++++++---------- pkg/cmd/crd_test.go | 6 +-- .../{ => crd}/in-resource/input-resource.yaml | 0 .../invalid-in-resource/input-resource.yaml | 0 .../invalid-input-resource.yaml | 0 .../out-resource/output-resource.yaml | 0 7 files changed, 26 insertions(+), 30 deletions(-) rename pkg/cmd/fakes/{ => crd}/in-resource/input-resource.yaml (100%) rename pkg/cmd/fakes/{ => crd}/invalid-in-resource/input-resource.yaml (100%) rename pkg/cmd/fakes/{ => crd}/invalid-in-resource/invalid-input-resource.yaml (100%) rename pkg/cmd/fakes/{ => crd}/out-resource/output-resource.yaml (100%) diff --git a/hack/build.sh b/hack/build.sh index 76cc21a..fdabf21 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -2,7 +2,8 @@ set -e -x -u -# go test ./... +go mod tidy +go test ./... go fmt ./cmd/... ./pkg/... # build without website assets diff --git a/pkg/cmd/crd.go b/pkg/cmd/crd.go index b89bde4..77e2002 100644 --- a/pkg/cmd/crd.go +++ b/pkg/cmd/crd.go @@ -4,14 +4,13 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" "github.com/cnoe-io/cnoe-cli/pkg/models" "github.com/spf13/cobra" - "gopkg.in/yaml.v2" + "gopkg.in/yaml.v3" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) @@ -27,6 +26,7 @@ var ( templatePath string verifiers []string namespaced bool + depth uint32 templateName string templateTitle string @@ -38,6 +38,7 @@ func init() { crdCmd.Flags().StringVarP(&templatePath, "templatePath", "t", "scaffolding/template.yaml", "path to the template to be augmented with backstage info") crdCmd.Flags().StringArrayVarP(&verifiers, "verifier", "v", []string{}, "list of verifiers to test the resource against") crdCmd.Flags().BoolVarP(&namespaced, "namespaced", "n", false, "whether or not resources are namespaced") + crdCmd.Flags().Uint32Var(&depth, "depth", 2, "depth from given directory to search for CRDs/XRDs") crdCmd.Flags().StringVarP(&templateName, "templateName", "", "", "sets the name of the template") crdCmd.Flags().StringVarP(&templateTitle, "templateTitle", "", "", "sets the title of the template") @@ -109,14 +110,14 @@ func Crd( return nil } -func defs(dir string, depth int) []string { - if depth > 2 { +func defs(dir string, currentDepth uint32) []string { + if currentDepth > depth { return nil } var out []string base, _ := filepath.Abs(dir) - files, _ := ioutil.ReadDir(base) + files, _ := os.ReadDir(base) for _, file := range files { f := filepath.Join(base, file.Name()) stat, _ := os.Stat(f) @@ -151,7 +152,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd } for _, def := range defs { - data, err := ioutil.ReadFile(def) + data, err := os.ReadFile(def) if err != nil { continue } @@ -159,6 +160,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd var doc models.Definition err = yaml.Unmarshal(data, &doc) if err != nil { + fmt.Printf("failed to read %s. This file will be excluded. %s", def, err) continue } @@ -182,8 +184,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd } else { value, err = ConvertMap(v) if err != nil { - fmt.Fprintf(stdout, "failed %s: %s \n", def, err.Error()) - continue + return cmdOutput{}, err } } @@ -243,7 +244,7 @@ func writeSchema(stdout, stderr io.Writer, outputDir string, defs []string) (cmd } template := fmt.Sprintf("%s/%s.yaml", templateOutputDir, strings.ToLower(resourceName)) - err = ioutil.WriteFile(template, []byte(wrapperData), 0644) + err = os.WriteFile(template, []byte(wrapperData), 0644) if err != nil { fmt.Fprintf(stdout, "failed %s: %s \n", def, err.Error()) continue @@ -261,7 +262,7 @@ func writeToTemplate( templateFile string, outputPath string, identifiedResources []string, position int, templateName, templateTitle, templateDescription string, ) error { - templateData, err := ioutil.ReadFile(templateFile) + templateData, err := os.ReadFile(templateFile) if err != nil { return err } @@ -316,7 +317,7 @@ func writeToTemplate( return err } - err = ioutil.WriteFile(fmt.Sprintf("%s/template.yaml", outputPath), outputData, 0644) + err = os.WriteFile(fmt.Sprintf("%s/template.yaml", outputPath), outputData, 0644) if err != nil { return err } @@ -334,32 +335,26 @@ func ConvertSlice(strSlice []string) []interface{} { } func ConvertMap(originalData interface{}) (map[string]interface{}, error) { - originalMap, ok := originalData.(map[interface{}]interface{}) + originalMap, ok := originalData.(map[string]interface{}) if !ok { - return nil, errors.New("failed to convert to interface map") + return nil, errors.New("conversion failed: data is not map[string]interface{}") } convertedMap := make(map[string]interface{}) for key, value := range originalMap { - strKey, ok := key.(string) - if !ok { - // Skip the key if it cannot be converted to string - continue - } - switch v := value.(type) { case map[interface{}]interface{}: // If the value is a nested map, recursively convert it var err error - convertedMap[strKey], err = ConvertMap(v) + convertedMap[key], err = ConvertMap(v) if err != nil { - return nil, errors.New(fmt.Sprintf("failed to convert for key %s", strKey)) + return nil, errors.New(fmt.Sprintf("failed to convert for key %s", key)) } case int: - convertedMap[strKey] = int64(v) + convertedMap[key] = int64(v) case int32: - convertedMap[strKey] = int64(v) + convertedMap[key] = int64(v) case []interface{}: dv := make([]interface{}, len(v)) for i, ve := range v { @@ -367,7 +362,7 @@ func ConvertMap(originalData interface{}) (map[string]interface{}, error) { case map[interface{}]interface{}: ivec, err := ConvertMap(ive) if err != nil { - return nil, errors.New(fmt.Sprintf("failed to convert for key %s", strKey)) + return nil, errors.New(fmt.Sprintf("failed to convert for key %s", key)) } dv[i] = ivec case int: @@ -378,10 +373,10 @@ func ConvertMap(originalData interface{}) (map[string]interface{}, error) { dv[i] = ive } } - convertedMap[strKey] = dv + convertedMap[key] = dv default: // Otherwise, add the key-value pair to the converted map - convertedMap[strKey] = v + convertedMap[key] = v } } diff --git a/pkg/cmd/crd_test.go b/pkg/cmd/crd_test.go index eead1df..5f0e6eb 100644 --- a/pkg/cmd/crd_test.go +++ b/pkg/cmd/crd_test.go @@ -29,11 +29,11 @@ var _ = Describe("Template", func() { templateTitle = "test-title" templateDescription = "test-description" - inputDir = "./fakes/in-resource" - invalidInputDir = "./fakes/invalid-in-resource" + inputDir = "./fakes/crd/in-resource" + invalidInputDir = "./fakes/crd/invalid-in-resource" + expectedResourceFile = "./fakes/crd/out-resource/output-resource.yaml" templateFile = "./fakes/template/input-template.yaml" expectedTemplateFile = "./fakes/template/output-template.yaml" - expectedResourceFile = "./fakes/out-resource/output-resource.yaml" ) BeforeEach(func() { diff --git a/pkg/cmd/fakes/in-resource/input-resource.yaml b/pkg/cmd/fakes/crd/in-resource/input-resource.yaml similarity index 100% rename from pkg/cmd/fakes/in-resource/input-resource.yaml rename to pkg/cmd/fakes/crd/in-resource/input-resource.yaml diff --git a/pkg/cmd/fakes/invalid-in-resource/input-resource.yaml b/pkg/cmd/fakes/crd/invalid-in-resource/input-resource.yaml similarity index 100% rename from pkg/cmd/fakes/invalid-in-resource/input-resource.yaml rename to pkg/cmd/fakes/crd/invalid-in-resource/input-resource.yaml diff --git a/pkg/cmd/fakes/invalid-in-resource/invalid-input-resource.yaml b/pkg/cmd/fakes/crd/invalid-in-resource/invalid-input-resource.yaml similarity index 100% rename from pkg/cmd/fakes/invalid-in-resource/invalid-input-resource.yaml rename to pkg/cmd/fakes/crd/invalid-in-resource/invalid-input-resource.yaml diff --git a/pkg/cmd/fakes/out-resource/output-resource.yaml b/pkg/cmd/fakes/crd/out-resource/output-resource.yaml similarity index 100% rename from pkg/cmd/fakes/out-resource/output-resource.yaml rename to pkg/cmd/fakes/crd/out-resource/output-resource.yaml