-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_helpers.go
86 lines (73 loc) · 1.78 KB
/
job_helpers.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
package bkp
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/ghodss/yaml"
"github.com/rs/zerolog/log"
)
func AllJobs(sourceDirs []string) []*Job {
jobs := make([]*Job, 0)
for _, sourceDir := range sourceDirs {
log.Info().Str("sourceDir", sourceDir).Msg("Searching jobs")
for _, job := range ScanJobDir(filepath.Join(sourceDir, "jobs"), sourceDirs) {
log.Debug().Str("job", job.String()).Msg("Job found")
jobs = append(jobs, job)
}
}
return jobs
}
func ScanJobDir(path string, targetDirs []string) []*Job {
jobs := make([]*Job, 0)
var errParse error
_ = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
var job *Job
switch {
case strings.HasSuffix(path, ".yml"):
job, errParse = ParseJobFromYmlFile(path, targetDirs)
case strings.HasSuffix(path, ".json"):
job, errParse = ParseJobFromJSONFile(path, targetDirs)
default:
return nil
}
if errParse != nil {
return nil
}
jobs = append(jobs, job)
return nil
})
return jobs
}
func ParseJobFromJSONFile(filename string, targetDirs []string) (*Job, error) {
var job Job
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &job)
if err != nil {
return nil, err
}
augmentJob(&job, filename, targetDirs)
return &job, nil
}
func ParseJobFromYmlFile(filename string, targetDirs []string) (*Job, error) {
var job Job
data, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
err = yaml.Unmarshal(data, &job)
if err != nil {
fmt.Println("parsing error", err)
return nil, err
}
augmentJob(&job, filename, targetDirs)
return &job, nil
}
func augmentJob(job *Job, filename string, targetDirs []string) {
job.Filename = filename
job.Target = TargetByName(job.TargetName, targetDirs)
}