// Generate generates unstructured go types for resources defined in yaml
// manifests.
Generate generates unstructured go types for resources defined in yaml
manifests.
// Generate generates unstructured go types for resources defined in yaml // manifests .
func Generate(resourceYaml []byte, varName string, values ...interface{}) (string, error) {
	if len(values) > 1 {
		return "", ErrTooManyValues
	} else if len(values) == 1 {
		yamlTemplate, err := template.New("yamlFile").Parse(string(resourceYaml))
		if err != nil {
			return "", fmt.Errorf("unable to parse template in yaml file, %w", err)
		}
		var yamlBuf bytes.Buffer
		if err := yamlTemplate.Execute(&yamlBuf, values[0]); err != nil {
			return "", fmt.Errorf("unable to resolve templating in yaml file, %w", err)
		}
		resourceYaml = yamlBuf.Bytes()
	}
	unstructuredObj := elements{}
	if err := yaml.Unmarshal(resourceYaml, &unstructuredObj); err != nil {
		return "", fmt.Errorf("unable to unmarshal input yaml, %w", err)
	}
	obj := object{
		VarName:  varName,
		Elements: unstructuredObj[0].Elements,
		Source:   string(resourceYaml),
	}
	t, err := template.New("objectTemplate").Funcs(funcMap()).Parse(objTemplate)
	if err != nil {
		return "", fmt.Errorf("unable to parse template, %w", err)
	}
	var buf bytes.Buffer
	err = t.Execute(&buf, obj)
	if err != nil {
		return "", fmt.Errorf("unable to generate go code, %w", err)
	}
	objSource, err := format.Source(buf.Bytes())
	if err != nil {
		return "", fmt.Errorf("unable to format file, %w", err)
	}
	return string(objSource), nil
}
func Generate ( resourceYaml [ ] byte , varName string , values ... interface { } ) ( string , error ) { if len ( values ) > 1 { return " " , ErrTooManyValues } else if len ( values ) == 1 { yamlTemplate , err := template . New ( " " ) . Parse ( string ( resourceYaml ) ) if err != nil { return " " , fmt . Errorf ( " " , err ) } var yamlBuf bytes . Buffer if err := yamlTemplate . Execute ( & yamlBuf , values [ 0 ] ) ; err != nil { return " " , fmt . Errorf ( " " , err ) } resourceYaml = yamlBuf . Bytes ( ) } unstructuredObj := elements { } if err := yaml . Unmarshal ( resourceYaml , & unstructuredObj ) ; err != nil { return " " , fmt . Errorf ( " " , err ) } obj := object { VarName : varName , Elements : unstructuredObj [ 0 ] . Elements , Source : string ( resourceYaml ) , } t , err := template . New ( " " ) . Funcs ( funcMap ( ) ) . Parse ( objTemplate ) if err != nil { return " " , fmt . Errorf ( " " , err ) } var buf bytes . Buffer err = t . Execute ( & buf , obj ) if err != nil { return " " , fmt . Errorf ( " " , err ) } objSource , err := format . Source ( buf . Bytes ( ) ) if err != nil { return " " , fmt . Errorf ( " " , err ) } return string ( objSource ) , nil }
Generate generates unstructured go types for resources defined in yaml
manifests.
Generate generates unstructured go types for resources defined in yaml manifests .