| 1 | |
| 2 | |
| 3 | |
| 4 | package limatmpl |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 10 | "github.com/lima-vm/lima/v2/pkg/limayaml" |
| 11 | ) |
| 12 | |
| 13 | type Template struct { |
| 14 | Locator string |
| 15 | Bytes []byte |
| 16 | |
| 17 | |
| 18 | Name string |
| 19 | Config *limatype.LimaYAML |
| 20 | |
| 21 | expr strings.Builder |
| 22 | } |
| 23 | |
| 24 | func (tmpl *Template) ClearOnError(err error) error { |
| 25 | if err != nil { |
| 26 | tmpl.Bytes = nil |
| 27 | tmpl.Config = nil |
| 28 | tmpl.expr.Reset() |
| 29 | } |
| 30 | return err |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | func (tmpl *Template) Unmarshal() error { |
| 36 | if tmpl.Config == nil { |
| 37 | tmpl.Config = &limatype.LimaYAML{} |
| 38 | if err := limayaml.Unmarshal(tmpl.Bytes, tmpl.Config, tmpl.Locator); err != nil { |
| 39 | tmpl.Config = nil |
| 40 | return err |
| 41 | } |
| 42 | } |
| 43 | return nil |
| 44 | } |