-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
91 lines (80 loc) · 1.92 KB
/
env.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
package ci
// SetEnv changes the environment variable
type SetEnv struct {
Global bool
Env string
Value string
}
// Setup sets up the step
func (step *SetEnv) Setup(parent *Task) {
task := parent.Subtask("%v := %q", step.Env, step.Value)
task.Exec = func(context, _ *Context) error {
value, err := context.ExpandEnv(step.Value)
if err != nil {
return err
}
if step.Global {
context.Global.GEnv.Set(step.Env, value)
} else {
context.SetEnv(step.Env, value)
}
return nil
}
}
// WhenEnv executes only when the given environment variable matches the value
type WhenEnv struct {
Env string
Value string
Steps []Step
}
// Setup sets up the step
func (step *WhenEnv) Setup(parent *Task) {
task := parent.Subtask("when %v == %q", step.Env, step.Value)
task.Exec = func(context, _ *Context) error {
value, err := context.ExpandEnv(step.Value)
if err != nil {
return err
}
current, _ := context.GetEnv(step.Env)
if current != value {
return ErrSkip
}
return nil
}
task.AddSteps(step.Steps)
}
// WhenEnvSet executes only when the given environment variable is set
type WhenEnvSet struct {
Env string
Steps []Step
}
// Setup sets up the step
func (step *WhenEnvSet) Setup(parent *Task) {
task := parent.Subtask("when %v", step.Env)
task.Exec = func(context, _ *Context) error {
current, _ := context.GetEnv(step.Env)
if current == "" {
return ErrSkip
}
return nil
}
task.AddSteps(step.Steps)
}
// CreateTempDir creates a temporary directory and sets it to environment variable
type CreateTempDir struct {
Global bool
Env string
}
// Setup sets up the step
func (step *CreateTempDir) Setup(parent *Task) {
task := parent.Subtask("%v := tempdir", step.Env)
task.Exec = func(context, _ *Context) error {
dir := context.Global.CreateTempDir(step.Env)
if step.Global {
context.Global.GEnv.Set(step.Env, dir)
} else {
context.SetEnv(step.Env, dir)
}
return nil
}
}