/
downstream_expansions_set.go
108 lines (89 loc) · 2.78 KB
/
downstream_expansions_set.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
package command
import (
"context"
"os"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/agent/internal"
"github.com/evergreen-ci/evergreen/agent/internal/client"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
// setExpansions takes a file of key value pairs and
// sets the downstream parameters for a patch
type setDownstream struct {
// Filename for a yaml file containing key-value pairs
YamlFile string `mapstructure:"file"`
IgnoreMissingFile bool `mapstructure:"ignore_missing_file"`
base
// Key-value pairs for updating the task's parameters with
downstreamParams []patch.Parameter
}
func setExpansionsFactory() Command { return &setDownstream{} }
func (c *setDownstream) Name() string { return "downstream_expansions.set" }
// ParseParams validates the input to setDownstream, returning and error
// if something is incorrect. Fulfills Command interface.
func (c *setDownstream) ParseParams(params map[string]interface{}) error {
err := mapstructure.Decode(params, c)
if err != nil {
return errors.Wrap(err, "decoding mapstructure params")
}
if c.YamlFile == "" {
return errors.New("file cannot be blank")
}
return nil
}
// Execute updates the expansions. Fulfills Command interface.
// params are not expanded as part of Execute, they will be expanded as part of parameterized builds.
func (c *setDownstream) Execute(ctx context.Context,
comm client.Communicator, logger client.LoggerProducer, conf *internal.TaskConfig) error {
var err error
c.YamlFile, err = conf.Expansions.ExpandString(c.YamlFile)
if err != nil {
return errors.WithStack(err)
}
filename := GetWorkingDirectory(conf, c.YamlFile)
_, err = os.Stat(filename)
if os.IsNotExist(err) {
if c.IgnoreMissingFile {
return nil
}
return errors.Errorf("file '%s' does not exist", filename)
}
err = c.ParseFromFile(filename)
if err != nil {
return err
}
logger.Task().Infof("Saving downstream parameters to patch with keys from file '%s'.", c.YamlFile)
if len(c.downstreamParams) == 0 {
return nil
}
if !evergreen.IsPatchRequester(conf.Task.Requester) {
return nil
}
err = comm.SetDownstreamParams(ctx, c.downstreamParams, client.TaskData{ID: conf.Task.Id, Secret: conf.Task.Secret})
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (c *setDownstream) ParseFromFile(filename string) error {
filedata, err := os.ReadFile(filename)
if err != nil {
return err
}
params_from_file := make(map[string]string)
err = yaml.Unmarshal(filedata, params_from_file)
if err != nil {
return err
}
for k, v := range params_from_file {
param := patch.Parameter{
Key: k,
Value: v,
}
c.downstreamParams = append(c.downstreamParams, param)
}
return nil
}