diff --git a/models/meshmodel/core/v1beta1/models.go b/models/meshmodel/core/v1beta1/models.go index 3924ebd4..bfe27398 100644 --- a/models/meshmodel/core/v1beta1/models.go +++ b/models/meshmodel/core/v1beta1/models.go @@ -120,14 +120,22 @@ func (m *Model) UpdateStatus(db *database.Handler, status entity.EntityStatus) e return nil } -func (c Model) WriteModelDefinition(modelDefPath string) error { +// WriteModelDefinition writes out the model to the given `modelDefPath` in the `outputType` format. +// `outputType` can be `yaml` or `json`. +func (c Model) WriteModelDefinition(modelDefPath string, outputType string) error { err := utils.CreateDirectory(modelDefPath) if err != nil { return err } - - modelFilePath := filepath.Join(modelDefPath, "model.json") + var modelFilePath string + if(outputType == "json"){ + modelFilePath = filepath.Join(modelDefPath) err = utils.WriteJSONToFile[Model](modelFilePath, c) + } + if(outputType == "yaml"){ + modelFilePath = filepath.Join(modelDefPath) + err = utils.WriteYamlToFile[Model](modelFilePath, c) + } if err != nil { return err } diff --git a/utils/utils.go b/utils/utils.go index cfd50df3..e856d3bb 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -19,6 +19,7 @@ import ( "strings" log "github.com/sirupsen/logrus" + "gopkg.in/yaml.v3" ) // transforms the keys of a Map recursively with the given transform function @@ -345,6 +346,25 @@ func MergeMaps(mergeInto, toMerge map[string]interface{}) map[string]interface{} return mergeInto } +func WriteYamlToFile[K any](outputPath string, data K) error { + byt, err := yaml.Marshal(data) + if err != nil { + // Use a different error code + return ErrMarshal(err) + } + + file, err := os.Create(outputPath) + if err != nil { + return ErrCreateFile(err, outputPath) + } + + _, err = file.Write(byt) + if err != nil { + return ErrWriteFile(err, outputPath) + } + return nil +} + func WriteJSONToFile[K any](outputPath string, data K) error { byt, err := json.MarshalIndent(data, " ", " ") if err != nil {