forked from cvbarros/go-teamcity
/
step_command_line.go
135 lines (115 loc) · 3.71 KB
/
step_command_line.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package teamcity
import (
"encoding/json"
"errors"
"fmt"
)
//StepCommandLine represents a a build step of type "CommandLine"
type StepCommandLine struct {
ID string
Name string
stepType string
stepJSON *stepJSON
isExecutable bool
//CustomScript contains code for platform specific script, like .cmd on windows or shell script on Unix-like environments.
CustomScript string
//CommandExecutable is the executable program to be called from this step.
CommandExecutable string
//CommandParameters are additional parameters to be passed on to the CommandExecutable.
CommandParameters string
//ExecuteMode is the execute mode for the step. See StepExecuteMode for details.
ExecuteMode StepExecuteMode
}
//NewStepCommandLineScript creates a command line build step that runs an inline platform-specific script.
func NewStepCommandLineScript(name string, script string) (*StepCommandLine, error) {
if script == "" {
return nil, errors.New("script is required")
}
return &StepCommandLine{
Name: name,
isExecutable: false,
stepType: StepTypeCommandLine,
CustomScript: script,
ExecuteMode: StepExecuteModeDefault,
}, nil
}
//NewStepCommandLineExecutable creates a command line that invokes an external executable.
func NewStepCommandLineExecutable(name string, executable string, args string) (*StepCommandLine, error) {
if executable == "" {
return nil, errors.New("executable is required")
}
return &StepCommandLine{
Name: name,
stepType: StepTypeCommandLine,
isExecutable: true,
CommandExecutable: executable,
CommandParameters: args,
ExecuteMode: StepExecuteModeDefault,
}, nil
}
//GetID is a wrapper implementation for ID field, to comply with Step interface
func (s *StepCommandLine) GetID() string {
return s.ID
}
//GetName is a wrapper implementation for Name field, to comply with Step interface
func (s *StepCommandLine) GetName() string {
return s.Name
}
//Type returns the step type, in this case "StepTypePowershell".
func (s *StepCommandLine) Type() BuildStepType {
return StepTypeCommandLine
}
func (s *StepCommandLine) properties() *Properties {
props := NewPropertiesEmpty()
props.AddOrReplaceValue("teamcity.step.mode", string(s.ExecuteMode))
if s.isExecutable {
props.AddOrReplaceValue("command.executable", s.CommandExecutable)
if s.CommandParameters != "" {
props.AddOrReplaceValue("command.parameters", s.CommandParameters)
}
} else {
props.AddOrReplaceValue("script.content", s.CustomScript)
props.AddOrReplaceValue("use.custom.script", "true")
}
return props
}
//MarshalJSON implements JSON serialization for StepCommandLine
func (s *StepCommandLine) MarshalJSON() ([]byte, error) {
out := &stepJSON{
ID: s.ID,
Name: s.Name,
Type: s.stepType,
Properties: s.properties(),
}
return json.Marshal(out)
}
//UnmarshalJSON implements JSON deserialization for StepCommandLine
func (s *StepCommandLine) UnmarshalJSON(data []byte) error {
var aux stepJSON
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
if aux.Type != string(StepTypeCommandLine) {
return fmt.Errorf("invalid type %s trying to deserialize into StepCommandLine entity", aux.Type)
}
s.Name = aux.Name
s.ID = aux.ID
s.stepType = StepTypeCommandLine
props := aux.Properties
if _, ok := props.GetOk("use.custom.script"); ok {
s.isExecutable = false
if v, ok := props.GetOk("script.content"); ok {
s.CustomScript = v
}
}
if v, ok := props.GetOk("command.executable"); ok {
s.CommandExecutable = v
if v, ok := props.GetOk("command.parameters"); ok {
s.CommandParameters = v
}
}
if v, ok := props.GetOk("teamcity.step.mode"); ok {
s.ExecuteMode = StepExecuteMode(v)
}
return nil
}