forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskdata_get.go
71 lines (57 loc) · 1.97 KB
/
taskdata_get.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
package command
import (
"context"
"io/ioutil"
"path/filepath"
"github.com/evergreen-ci/evergreen/model"
"github.com/evergreen-ci/evergreen/rest/client"
"github.com/evergreen-ci/evergreen/util"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
)
type taskDataGet struct {
File string `mapstructure:"file" plugin:"expand"`
DataName string `mapstructure:"name" plugin:"expand"`
TaskName string `mapstructure:"task" plugin:"expand"`
Variant string `mapstructure:"variant" plugin:"expand"`
base
}
func taskDataGetFactory() Command { return &taskDataGet{} }
func (c *taskDataGet) Name() string { return "json.get" }
func (c *taskDataGet) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, c); err != nil {
return errors.Wrapf(err, "error decoding '%v' params", c.Name())
}
if c.File == "" {
return errors.New("JSON 'get' command must not have blank 'file' parameter")
}
if c.DataName == "" {
return errors.New("JSON 'get' command must not have a blank 'name' param")
}
if c.TaskName == "" {
return errors.New("JSON 'get' command must not have a blank 'task' param")
}
return nil
}
func (c *taskDataGet) Execute(ctx context.Context,
comm client.Communicator, logger client.LoggerProducer, conf *model.TaskConfig) error {
err := errors.WithStack(util.ExpandValues(c, conf.Expansions))
if err != nil {
return err
}
if !filepath.IsAbs(c.File) {
c.File = filepath.Join(conf.WorkDir, c.File)
}
if err = createEnclosingDirectoryIfNeeded(c.File); err != nil {
return errors.WithStack(err)
}
td := client.TaskData{ID: conf.Task.Id, Secret: conf.Task.Secret}
data, err := comm.GetJSONData(ctx, td, c.TaskName, c.DataName, c.Variant)
if err != nil {
return errors.Wrapf(err, "problem retrieving data from %s/%s/%s",
c.TaskName, c.DataName, c.Variant)
}
return errors.Wrapf(ioutil.WriteFile(c.File, data, 0755),
"problem writing data from task %s/%s/%s to file %s",
c.TaskName, c.DataName, c.Variant, c.File)
}