-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandspec.go
65 lines (58 loc) · 1.28 KB
/
commandspec.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
package beater
import (
"errors"
"fmt"
"github.com/uphy/commandbeat/command"
"github.com/uphy/commandbeat/config"
"github.com/uphy/commandbeat/parser"
)
// defaultCommandSpec implements command.Spec interface.
type commandSpec struct {
name string
command string
args []string
parser parser.Parser
debug bool
}
func (cb *Commandbeat) newSpec(name string, task *config.TaskConfig) (command.Spec, error) {
parser, err := task.Parser()
if err != nil {
return nil, err
}
if task.Shell {
script, err := task.Command()
if err != nil {
return nil, err
}
s, err := cb.scriptManager.createScript(name, script[0])
if err != nil {
return nil, fmt.Errorf("Failed to create script file. (dir=%s, name=%s, err=%s)", cb.scriptManager.directory, name, err)
}
return &commandSpec{
name,
s.Command(),
s.Args(),
parser,
task.Debug,
}, nil
}
c, err := task.Command()
if err != nil {
return nil, err
}
if len(c) == 0 {
return nil, errors.New("command empty")
}
commandName := c[0]
commandArgs := []string{}
if len(c) > 0 {
commandArgs = c[1:]
}
return &commandSpec{name, commandName, commandArgs, parser, task.Debug}, nil
}
func (c *commandSpec) Command() string {
return c.command
}
func (c *commandSpec) Args() []string {
return c.args
}