-
Notifications
You must be signed in to change notification settings - Fork 7
/
run.go
87 lines (72 loc) · 1.97 KB
/
run.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
package command
import (
"fmt"
"os"
"strings"
"github.com/Sirupsen/logrus"
"github.com/jessevdk/go-flags"
"github.com/mitchellh/cli"
"github.com/pkg/errors"
)
//RunOpts describes command options
type RunOpts struct {
*NerdOpts
Environment []string `short:"e" description:"container environment variables"`
}
//Run command
type Run struct {
*command
opts *RunOpts
parser *flags.Parser
}
//RunFactory returns a factory method for the join command
func RunFactory() func() (cmd cli.Command, err error) {
cmd := &Run{
command: &command{
help: "",
synopsis: "Run a new compute task.\nOptionally you can give a dataset-ID as the second argument. This dataset-ID will be available as the NERD_DATASET_INPUT env variable inside the container.",
parser: flags.NewNamedParser("nerd run <image> [dataset-ID]", flags.Default),
ui: &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stderr,
},
},
opts: &RunOpts{},
}
cmd.runFunc = cmd.DoRun
_, err := cmd.command.parser.AddGroup("options", "options", cmd.opts)
if err != nil {
panic(err)
}
return func() (cli.Command, error) {
return cmd, nil
}
}
//DoRun is called by run and allows an error to be returned
func (cmd *Run) DoRun(args []string) error {
if len(args) < 1 {
return fmt.Errorf("not enough arguments, see --help")
}
dataset := ""
if len(args) == 2 {
dataset = args[1]
}
env := make(map[string]string)
for i, e := range cmd.opts.Environment {
split := strings.Split(e, "=")
if len(split) != 2 {
HandleError(errors.Errorf("Environment variable %v (%v) is in the wrong format. Please specify environment flag as '-e [KEY]=[VALUE]'", (i+1), e), cmd.opts.VerboseOutput)
}
env[split[0]] = split[1]
}
client, err := NewClient(cmd.ui)
if err != nil {
HandleError(err, cmd.opts.VerboseOutput)
}
task, err := client.CreateTask(args[0], dataset, env)
if err != nil {
HandleError(err, cmd.opts.VerboseOutput)
}
logrus.Infof("Created task with ID %v", task.TaskID)
return nil
}