Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mic][iso] Save input ISO configuration as yaml #10743

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions toolkit/tools/imagecustomizerapi/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package imagecustomizerapi

import (
"bytes"
"fmt"
"os"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -51,3 +52,41 @@ func UnmarshalYaml[ValueType HasIsValid](yamlData []byte, value ValueType) error

return nil
}

func MarshalYamlFile[ValueType HasIsValid](yamlfilePath string, value ValueType) (err error) {
yamlString, err := MarshalYaml(value)
if err != nil {
return err
}

file, err := os.Create(yamlfilePath)
if err != nil {
return err
}
defer func() {
closeErr := file.Close()
if closeErr != nil {
if err != nil {
err = fmt.Errorf("%w:\nfailed to close (%s): %w", err, yamlfilePath, closeErr)
} else {
err = fmt.Errorf("failed to close (%s): %w", yamlfilePath, closeErr)
}
}
}()

_, err = file.WriteString(yamlString)
if err != nil {
return err
}

return nil
}

func MarshalYaml[ValueType HasIsValid](value ValueType) (string, error) {
yamlData, err := yaml.Marshal(value)
if err != nil {
return "", err
}

return string(yamlData), nil
}
Loading
Loading