Skip to content

Commit

Permalink
fix the crd templating command
Browse files Browse the repository at this point in the history
- make depth configurable
- restructure the fake files for tests
- use yaml v3
- do a `go mod tidy` prior to building

fixes #10
  • Loading branch information
nimakaviani committed Aug 11, 2023
1 parent acff685 commit b4a8b48
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
3 changes: 2 additions & 1 deletion hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

set -e -x -u

# go test ./...
go mod tidy
go test ./...
go fmt ./cmd/... ./pkg/...

# build without website assets
Expand Down
47 changes: 21 additions & 26 deletions pkg/cmd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -27,6 +26,7 @@ var (
templatePath string
verifiers []string
namespaced bool
depth uint32

templateName string
templateTitle string
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -151,14 +152,15 @@ 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
}

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
}

Expand All @@ -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
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -334,40 +335,34 @@ 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 {
switch ive := ve.(type) {
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:
Expand All @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/crd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b4a8b48

Please sign in to comment.