-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder.go
169 lines (149 loc) · 3.71 KB
/
folder.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package service
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/dop251/goja"
"github.com/factorysh/microdensity/task"
"github.com/google/uuid"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
var _ Service = (*FolderService)(nil)
type FolderService struct {
name string
jsruntime *goja.Runtime
validate func(map[string]interface{}) (Arguments, error)
badge func(project, branch, commit, badge string) (Badge, error)
logger *zap.Logger
meta Meta
}
type Arguments struct {
Environments map[string]string `json:"environments"`
Files map[string]string `json:"files"`
}
type Console struct {
}
func (c *Console) Log(args ...interface{}) {
spew.Dump(args...)
}
func NewFolder(_path string) (*FolderService, error) {
_path = path.Clean(_path)
logger, err := zap.NewProduction()
if err != nil {
return nil, err
}
l := logger.With(zap.String("path", _path))
stat, err := os.Stat(_path)
if err != nil {
l.Error("stat", zap.Error(err))
return nil, err
}
if !stat.IsDir() {
l.Error("Is not a directory")
return nil, fmt.Errorf("%s is not a directory", _path)
}
var content []byte
// loop over possible meta.yml files
for _, name := range []string{"meta.yml", "meta.yaml"} {
pth := filepath.Clean(filepath.Join(_path, name))
if !strings.HasPrefix(pth, _path) {
panic("Path escape " + pth)
}
content, err = os.ReadFile(pth)
if err == nil {
break
}
}
if err != nil {
return nil, fmt.Errorf("error with path %s: %v", _path, err)
}
var m Meta
err = yaml.Unmarshal(content, &m)
if err != nil {
return nil, fmt.Errorf("error with path %s: %v", _path, err)
}
_, name := path.Split(_path)
service := &FolderService{
name: name,
logger: logger,
meta: m,
}
l = l.With(zap.String("name", name))
jsPath := filepath.Clean(path.Join(_path, "meta.js"))
if !strings.HasPrefix(jsPath, _path) {
panic("Path escape : " + jsPath)
}
_, err = os.Stat(jsPath)
if err != nil {
if os.IsNotExist(err) {
// there is meta.js file
} else {
return nil, err
}
} else {
chrono := time.Now()
vm := goja.New()
vm.SetFieldNameMapper(goja.TagFieldNameMapper("json", true))
err = vm.Set("console", &Console{})
if err != nil {
return nil, err
}
src, err := ioutil.ReadFile(jsPath)
if err != nil {
return nil, err
}
_, err = vm.RunScript("meta", string(src))
if err != nil {
return nil, err
}
err = vm.ExportTo(vm.Get("validate"), &service.validate)
if err != nil {
return nil, err
}
l.Info("js is ready", zap.Float64("js cooking time (µs)", float64(time.Since(chrono))/1000))
service.jsruntime = vm
}
return service, nil
}
func (f *FolderService) Name() string {
return f.name
}
func (f *FolderService) Validate(args map[string]interface{}) (Arguments, error) {
chrono := time.Now()
defer f.logger.Info("Validate",
zap.String("service", f.name),
zap.Float64("validation time (µs)", float64(time.Since(chrono))/1000))
return f.validate(args)
}
func (f *FolderService) New(project string, args map[string]interface{}) (uuid.UUID, error) {
t := &task.Task{
Id: uuid.New(),
Project: project,
Args: args,
State: task.Ready,
}
return t.Id, nil
}
func (f *FolderService) Badge(project, branch, commit, badge string) (Badge, error) {
chrono := time.Now()
defer f.logger.Info("Badge",
zap.String("service", f.name),
zap.String("project", project),
zap.String("name", badge),
zap.Float64("validation time (µs)", float64(time.Since(chrono))/1000))
return f.badge(project, branch, commit, badge)
}
func (f *FolderService) Run(id uuid.UUID) error {
// FIXME
return nil
}
// Meta data about this service
func (f *FolderService) Meta() Meta {
return f.meta
}