-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_runner.go
104 lines (91 loc) · 2.37 KB
/
dart_runner.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
package main
import (
_ "embed"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/APTrust/dart-runner/constants"
"github.com/APTrust/dart-runner/core"
"github.com/APTrust/dart-runner/util"
)
//go:embed help.txt
var help string
// Version value is injected at build time.
var Version string
func main() {
constants.AppVersion = Version
exitCode := constants.ExitOK
options := core.ParseOptions()
if !options.AreValid() {
ShowOptionsError()
exitCode = constants.ExitUsageErr
} else if options.ShowHelp {
ShowHelp()
} else if options.Version {
ShowVersion()
} else if len(options.StdinData) > 0 || core.StdinHasData() {
exitCode = RunJob(options)
} else {
exitCode = RunWorkflow(options)
}
os.Exit(exitCode)
}
func RunJob(opts *core.Options) int {
params, err := InitParams(opts)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating job: %s\n", err.Error())
return constants.ExitRuntimeErr
}
return core.RunJob(params.ToJob(), opts.DeleteAfterUpload, true)
}
func RunWorkflow(opts *core.Options) int {
runner, err := core.NewWorkflowRunner(
opts.WorkflowFilePath,
opts.BatchFilePath,
opts.OutputDir,
opts.DeleteAfterUpload,
opts.Concurrency,
)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot start workflow: %s\n", err.Error())
return constants.ExitRuntimeErr
}
return runner.Run()
}
func InitParams(opts *core.Options) (*core.JobParams, error) {
if !util.FileExists(opts.OutputDir) {
return nil, fmt.Errorf("Output directory '%s' does not exist. You must create it first.", opts.OutputDir)
}
workflow, err := core.WorkflowFromJson(opts.WorkflowFilePath)
if err != nil {
return nil, fmt.Errorf("Workflow JSON (%s): %s", opts.WorkflowFilePath, err.Error())
}
partialParams := &core.JobParams{}
err = json.Unmarshal(opts.StdinData, partialParams)
if err != nil {
return nil, fmt.Errorf("JobParams JSON: %s", err.Error())
}
params := core.NewJobParams(
workflow,
partialParams.PackageName,
filepath.Join(opts.OutputDir, partialParams.PackageName),
partialParams.Files,
partialParams.Tags)
return params, nil
}
func ShowHelp() {
fmt.Println(help)
}
func ShowVersion() {
fmt.Println(Version)
}
func ShowOptionsError() {
message := `
Invalid arguments.
Params --workflow and --output-dir are always required.
For batch jobs, param --batch is also required.
For more info: dart-runner --help
`
fmt.Fprintln(os.Stderr, message)
}