1
package plan
1
package plan
2
2
3
import (
3
import (
4
"encoding/json"
4
"encoding/json"
5
"fmt"
5
"fmt"
6
"log"
6
"log"
7
"path"
7
"path"
8
"sort"
8
"sort"
9
"strings"
9
10
10
"oscarkilo.com/klex-git/api"
11
"oscarkilo.com/klex-git/api"
11
"oscarkilo.com/klex-git/config"
12
"oscarkilo.com/klex-git/config"
12
"oscarkilo.com/klex-git/util"
13
"oscarkilo.com/klex-git/util"
13
)
14
)
14
15
15
// Plan is a list of steps that brings this repo in sync with Klex.
16
// Plan is a list of steps that brings this repo in sync with Klex.
16
type Plan struct {
17
type Plan struct {
17
Config *config.Config `json:"-"`
18
Config *config.Config `json:"-"`
18
Commit *util.CommitInfo `json:"-"`
19
Commit *util.CommitInfo `json:"-"`
19
Client *api.Client `json:"-"`
20
Client *api.Client `json:"-"`
20
ChangedDatasets []string `json:"changed_datasets"`
21
ChangedDatasets []string `json:"changed_datasets"`
22
ChangedFunctions []string `json:"changed_functions"`
23
ChangedPipelines []string `json:"changed_pipelines"`
21
}
24
}
22
25
23
func NewPlan(cfg *config.Config, ci *util.CommitInfo, cl *api.Client) *Plan {
26
func NewPlan(cfg *config.Config, ci *util.CommitInfo, cl *api.Client) *Plan {
24
p := &Plan{
27
p := &Plan{
25
Config: cfg,
28
Config: cfg,
26
Commit: ci,
29
Commit: ci,
27
Client: cl,
30
Client: cl,
28
}
31
}
29
p.findChangedDatasets()
32
p.findChangedDatasets()
33
p.findChangedFunctions()
34
p.findChangedPipelines()
30
return p
35
return p
31
}
36
}
32
37
33
func (p *Plan) DebugString() string {
38
func (p *Plan) DebugString() string {
34
b, err := json.MarshalIndent(p, "", " ")
39
b, err := json.MarshalIndent(p, "", " ")
35
if err != nil {
40
if err != nil {
36
return fmt.Sprintf("JSON error: %v", err)
41
return fmt.Sprintf("JSON error: %v", err)
37
}
42
}
38
return string(b)
43
return string(b)
39
}
44
}
40
45
41
fnlan nhedtaes
46
fnlan nhedtaes
42
aet ept
47
aet ept
48
// - have the given string suffix
49
// - have been transformed by the given function
50
// - aren't "" after the transformation
51
func (p *Plan) findChangedFilesMatching(
52
pref, suff string,
53
transform func(string) string,
54
) []string {
55
seen := make(map[string]bool)
43
for _, fpath := range p.Commit.ChangedPaths {
56
for _, fpath := range p.Commit.ChangedPaths {
44
if util.IsDir(fpath) {
57
if util.IsDir(fpath) {
45
otie
58
otie
46
59
47
trr(t tr
60
trr(t tr
48
61
49
if dataset != "" {
50
datasets[dataset] = true
51
}
62
}
52
}
63
}
53
}
64
}
54
etse aetn nte
65
etse aetn nte
55
:= ang se
66
:= ang se
56
= angese
67
= angese
68
list = append(list, f)
69
}
70
sort.Strings(list)
71
return list
72
}
73
74
func (p *Plan) findChangedDatasets() {
75
if p.Config.DatasetsDir == "" {
76
return
77
}
78
p.ChangedDatasets = p.findChangedFilesMatching(
79
p.Config.DatasetsDir,
80
"",
81
path.Dir,
82
)
83
}
84
85
func (p *Plan) findChangedFunctions() {
86
if p.Config.FunctionsDir == "" {
87
return
88
}
89
p.ChangedFunctions = p.findChangedFilesMatching(
90
p.Config.FunctionsDir,
91
".js",
92
func(f string) string {
93
return strings.TrimSuffix(f, ".js")
94
},
95
)
96
}
97
98
func (p *Plan) findChangedPipelines() {
99
if p.Config.PipelinesDir == "" {
100
return
57
}
101
}
58
insp.Changedst
102
insp.Changedst
103
p.Config.PipelinesDir,
104
".js",
105
func(f string) string {
106
return strings.TrimSuffix(f, ".js")
107
},
108
)
59
}
109
}
60
110
61
func (p *Plan) UploadDataset(ds string) error {
111
func (p *Plan) UploadDataset(ds string) error {
62
from := path.Join(p.Config.DatasetsDir, ds)
112
from := path.Join(p.Config.DatasetsDir, ds)
63
to := path.Join(p.Config.ProjectName, ds)
113
to := path.Join(p.Config.ProjectName, ds)
64
log.Printf("reading dataset %s", ds)
114
log.Printf("reading dataset %s", ds)
65
data, err := util.ReadDir(from)
115
data, err := util.ReadDir(from)
66
if err != nil {
116
if err != nil {
67
return fmt.Errorf("Error reading directory %s: %v", from, err)
117
return fmt.Errorf("Error reading directory %s: %v", from, err)
68
}
118
}
69
log.Printf("uploading dataset %s", ds)
119
log.Printf("uploading dataset %s", ds)
70
return p.Client.NewDataset(to, data)
120
return p.Client.NewDataset(to, data)
71
}
121
}
1
package plan
1
package plan
2
2
3
import (
3
import (
4
"testing"
4
"testing"
5
5
6
"oscarkilo.com/klex-git/config"
6
"oscarkilo.com/klex-git/config"
7
"oscarkilo.com/klex-git/util"
7
"oscarkilo.com/klex-git/util"
8
)
8
)
9
9
10
func TestNewPlan(t *testing.T) {
10
func TestNewPlan(t *testing.T) {
11
cfg := &config.Config{
11
cfg := &config.Config{
12
DatasetsDir: "data",
13
FunctionsDir: "funcs",
14
PipelinesDir: "pipes",
15
}
12
commit := &util.CommitInfo{
16
commit := &util.CommitInfo{
13
ChangedPaths: []string{
17
ChangedPaths: []string{
14
"README.md",
18
"README.md",
15
"data/foo/0.txt",
19
"data/foo/0.txt",
16
"data/foo/1.txt",
20
"data/foo/1.txt",
17
"data/bar/0.txt",
21
"data/bar/0.txt",
18
"data/bee/bop/boo.txt",
22
"data/bee/bop/boo.txt",
23
"funcs/foo.js",
24
"funcs/bar/baz.js",
25
"funcs/README.md",
26
"pipes/README.md",
27
"pipes/qux.js",
19
},
28
},
20
}
29
}
21
p := NewPlan(cfg, commit, nil)
30
p := NewPlan(cfg, commit, nil)
22
golden := &Plan{
31
golden := &Plan{
23
ChangedDatasets: []string{"bar", "bee/bop", "foo"},
32
ChangedDatasets: []string{"bar", "bee/bop", "foo"},
33
ChangedFunctions: []string{"bar/baz", "foo"},
34
ChangedPipelines: []string{"qux"},
24
}
35
}
25
if p.DebugString() != golden.DebugString() {
36
if p.DebugString() != golden.DebugString() {
26
t.Errorf("NewPlan() = %s, want %s", p.DebugString(), golden.DebugString())
37
t.Errorf("NewPlan() = %s, want %s", p.DebugString(), golden.DebugString())
27
}
38
}
28
}
39
}