-
Notifications
You must be signed in to change notification settings - Fork 33
/
generate.go
55 lines (48 loc) · 1.08 KB
/
generate.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
package tasks
import (
"github.com/go-playground/validator/v10"
"github.com/google/shlex"
"github.com/pkg/errors"
)
func Generate(descriptions ...TaskDescription) (*TaskConfiguration, error) {
tc := TaskConfiguration{
Version: "2.0.0",
BaseTaskConfiguration: BaseTaskConfiguration{
Tasks: descriptions,
},
}
v := validator.New()
if err := v.Struct(tc); err != nil {
return nil, errors.WithStack(err)
}
return &tc, nil
}
type ShellCommandOpts struct {
Cwd string
Env map[string]string
}
func GenerateFromShellCommand(name, command string, opts *ShellCommandOpts) (*TaskConfiguration, error) {
fragments, err := shlex.Split(command)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse command %q", command)
}
var args []string
if len(fragments) >= 1 {
args = fragments[1:]
}
var options *CommandOptions
if opts != nil {
options = &CommandOptions{
Cwd: opts.Cwd,
Env: opts.Env,
}
}
return Generate(TaskDescription{
Label: name,
Type: "shell",
Command: fragments[0],
Args: args,
Group: "build",
Options: options,
})
}