Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limatmpl/template.go
2604 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
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 // template locator (absolute path or URL)
15
Bytes []byte // file contents
16
17
// The following fields are only used when the template represents a YAML config file.
18
Name string // instance name, may be inferred from locator
19
Config *limatype.LimaYAML
20
21
expr strings.Builder // yq expression to update template
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
// Unmarshal makes sure the tmpl.Config field is set. Any operation that modified
34
// tmpl.Bytes is expected to set tmpl.Config back to nil.
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
}
45
46