-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
63 lines (49 loc) · 1.31 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package templates
import (
"fmt"
"io/fs"
"os"
"path"
"github.com/jijeshmohan/dago/pkg/tasks"
"github.com/jijeshmohan/dago/pkg/variables"
"github.com/jijeshmohan/dago/pkg/xfilesystem"
"gopkg.in/yaml.v3"
)
const TemplateFile = `dago-template.yaml`
type Template struct {
Name string `yaml:"name,omitempty"`
Variables variables.Variables `yaml:"variables,omitempty"`
Tasks []tasks.Task `yaml:"tasks,omitempty"`
fs xfilesystem.FS `yaml:"-"`
}
func (t Template) Filesystem() xfilesystem.FS {
return t.fs
}
func (t Template) save(folderPath string) error {
data, err := yaml.Marshal(&t)
if err != nil {
return fmt.Errorf("invalid yaml data %w", err)
}
return os.WriteFile(path.Join(folderPath, TemplateFile), data, 0644)
}
func loadTemplate(fsys xfilesystem.FS) (Template, error) {
data, err := fs.ReadFile(fsys, TemplateFile)
if err != nil {
return Template{}, fmt.Errorf("unable to find template in the path %w", err)
}
var t Template
if err := yaml.Unmarshal([]byte(data), &t); err != nil {
return Template{}, fmt.Errorf("invalid template yml: %w", err)
}
for _, v := range t.Variables {
if err := v.Validate(); err != nil {
return Template{}, err
}
}
t.fs = fsys
return t, nil
}
func isPathExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}