-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
66 lines (52 loc) · 1.27 KB
/
main.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
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
taskrunner "github.com/dockersamples/gopher-task-system/internal/task-runner"
"github.com/dockersamples/gopher-task-system/internal/types"
yaml "gopkg.in/yaml.v2"
)
const (
argRun = "run"
helpMessage = `You must provide valid arguments to gopher.
Example:
./gopher run tasks.yaml`
errReadTaskDef = "failed to read tasks due to error: %v\n"
errNewRunner = "failed to create new runner: %v\n"
errTaskRun = "failed to run the task: %v\n"
)
func main() {
args := os.Args[1:]
if len(args) < 2 || args[0] != argRun {
fmt.Println(helpMessage)
return
}
// read the task definition file
def, err := readTaskDefinition(args[1])
if err != nil {
fmt.Printf(errReadTaskDef, err)
}
// create a task runner for the task definition
ctx := context.Background()
runner, err := taskrunner.NewRunner(def)
if err != nil {
fmt.Printf(errNewRunner, err)
}
doneCh := make(chan bool)
go runner.Run(ctx, doneCh)
<-doneCh
}
func readTaskDefinition(fileName string) (types.TaskDefinition, error) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return types.TaskDefinition{}, err
}
var def types.TaskDefinition
err = yaml.Unmarshal(data, &def)
if err != nil {
return def, err
}
return def, nil
}