forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskdata_send.go
76 lines (62 loc) · 1.89 KB
/
taskdata_send.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
package command
import (
"context"
"os"
"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 taskDataSend struct {
File string `mapstructure:"file" plugin:"expand"`
DataName string `mapstructure:"name" plugin:"expand"`
base
}
func taskDataSendFactory() Command { return &taskDataSend{} }
func (c *taskDataSend) Name() string { return "json.send" }
func (c *taskDataSend) 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("'file' param must not be blank")
}
if c.DataName == "" {
return errors.New("'name' param must not be blank")
}
return nil
}
func (c *taskDataSend) Execute(ctx context.Context,
comm client.Communicator, logger client.LoggerProducer, conf *model.TaskConfig) error {
td := client.TaskData{ID: conf.Task.Id, Secret: conf.Task.Secret}
errChan := make(chan error)
go func() {
// attempt to open the file
fileLoc := filepath.Join(conf.WorkDir, c.File)
jsonFile, err := os.Open(fileLoc)
if err != nil {
errChan <- errors.Wrap(err, "Couldn't open json file")
return
}
jsonData := map[string]interface{}{}
err = util.ReadJSONInto(jsonFile, &jsonData)
if err != nil {
errChan <- errors.Wrap(err, "File contained invalid json")
return
}
errChan <- errors.Wrapf(comm.PostJSONData(ctx, td, c.DataName, jsonData),
"problem posting task data for %s (%s)", c.DataName, td.ID)
}()
select {
case err := <-errChan:
if err != nil {
logger.Task().Errorf("Sending json data failed: %v", err)
}
return errors.WithStack(err)
case <-ctx.Done():
logger.Execution().Info("Received abort signal, stopping.")
return nil
}
}