/
file_reference.go
81 lines (71 loc) · 2.24 KB
/
file_reference.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
// Copyright 2019 Honey Science Corporation
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
package config
import (
"fmt"
"io/ioutil"
"path"
"strings"
"github.com/honeydipper/honeydipper/pkg/dipper"
)
func (c *Repo) normalizeFilePath(cwd string, file string) string {
fullpath := path.Clean(path.Join(c.root, cwd, file))
if !strings.HasPrefix(fullpath, c.root+"/") {
panic(fmt.Errorf("path is invalid: %s", fullpath))
}
return fullpath
}
func (c *Repo) normalizeFilePaths(currentFile string, content *DataSet) {
var processor func(key string, val interface{}) (interface{}, bool)
cwd := path.Dir(currentFile)
processor = func(_ string, val interface{}) (interface{}, bool) {
switch v := val.(type) {
case string:
if len(v) > 2 && v[0:2] == "@:" {
text, err := ioutil.ReadFile(c.normalizeFilePath(cwd, v[2:]))
if err != nil {
panic(err)
}
return string(text), true
}
return nil, false
case Trigger:
dipper.Recursive(v.Match, processor)
dipper.Recursive(v.Parameters, processor)
dipper.Recursive(v.Export, processor)
return nil, false
case Function:
dipper.Recursive(v.Parameters, processor)
dipper.Recursive(v.Export, processor)
dipper.Recursive(v.ExportOnSuccess, processor)
dipper.Recursive(v.ExportOnFailure, processor)
return nil, false
case System:
dipper.Recursive(v.Triggers, processor)
dipper.Recursive(v.Functions, processor)
dipper.Recursive(v.Data, processor)
return nil, false
case Rule:
dipper.Recursive(&v.Do, processor)
dipper.Recursive(&v.When, processor)
return nil, false
case Workflow:
dipper.Recursive(v.Match, processor)
dipper.Recursive(v.UnlessMatch, processor)
dipper.Recursive(v.Local, processor)
dipper.Recursive(v.Export, processor)
dipper.Recursive(v.ExportOnSuccess, processor)
dipper.Recursive(v.ExportOnFailure, processor)
dipper.Recursive(v.Else, processor)
dipper.Recursive(v.Steps, processor)
dipper.Recursive(v.Threads, processor)
dipper.Recursive(v.Cases, processor)
return nil, false
}
return nil, false
}
dipper.Recursive(content, processor)
}