-
Notifications
You must be signed in to change notification settings - Fork 124
/
attach_artifacts.go
148 lines (120 loc) · 4 KB
/
attach_artifacts.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
136
137
138
139
140
141
142
143
144
145
146
147
148
package command
import (
"context"
"os"
"path/filepath"
"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/artifact"
"github.com/evergreen-ci/evergreen/util"
"github.com/evergreen-ci/utility"
"github.com/mitchellh/mapstructure"
"github.com/mongodb/grip"
"github.com/pkg/errors"
)
type attachArtifacts struct {
// Files is a list of files, using gitignore syntax.
Files []string `mapstructure:"files" plugin:"expand"`
// ExactFileNames, when set to true, causes this command to treat the files array as an array of exact filenames to match,
// rather than the default behavior, which treats files as an array of gitignore file globs.
ExactFileNames bool `mapstructure:"exact_file_names"`
// Prefix is an optional directory prefix to start file globbing in, relative to Evergreen's working directory.
Prefix string `mapstructure:"prefix" plugin:"expand"`
// Optional, when set to true, causes this command to be skipped over without an error when
// the path specified in files does not exist. Defaults to false, which triggers errors
// for missing files.
Optional bool `mapstructure:"optional"`
base
}
func attachArtifactsFactory() Command { return &attachArtifacts{} }
func (c *attachArtifacts) Name() string { return evergreen.AttachArtifactsCommandName }
func (c *attachArtifacts) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, c); err != nil {
return errors.Wrap(err, "decoding mapstructure params")
}
if len(c.Files) == 0 {
return errors.Errorf("must specify at least one file pattern to parse")
}
return nil
}
func (c *attachArtifacts) Execute(ctx context.Context,
comm client.Communicator, logger client.LoggerProducer, conf *internal.TaskConfig) error {
var err error
if err = util.ExpandValues(c, &conf.Expansions); err != nil {
return errors.Wrap(err, "applying expansions")
}
if !c.ExactFileNames {
workDir := GetWorkingDirectory(conf, c.Prefix)
include := utility.NewGitIgnoreFileMatcher(workDir, c.Files...)
b := utility.FileListBuilder{
WorkingDir: workDir,
Include: include,
}
if c.Files, err = b.Build(); err != nil {
return errors.Wrap(err, "building wildcard paths")
}
}
if len(c.Files) == 0 {
err = errors.New("expanded file specification had no items")
if c.Optional {
logger.Task().Error(err)
return nil
}
return err
}
catcher := grip.NewBasicCatcher()
missedSegments := 0
files := []*artifact.File{}
var segment []*artifact.File
for idx := range c.Files {
segment, err = readArtifactsFile(GetWorkingDirectory(conf, c.Prefix), c.Files[idx])
if err != nil {
if c.Optional && os.IsNotExist(errors.Cause(err)) {
// pass;
} else {
catcher.Add(err)
}
continue
}
if segment == nil {
missedSegments++
continue
}
files = append(files, segment...)
}
if catcher.HasErrors() {
return errors.Wrap(catcher.Resolve(), "reading artifact JSON files")
}
if missedSegments > 0 {
logger.Task().Noticef("Encountered %d empty file definitions.", missedSegments)
}
if len(files) == 0 {
logger.Task().Warning("No artifacts defined.")
return nil
}
td := client.TaskData{ID: conf.Task.Id, Secret: conf.Task.Secret}
if err = comm.AttachFiles(ctx, td, files); err != nil {
return errors.Wrap(err, "attach artifacts failed")
}
logger.Task().Infof("'%s' attached %d resources to task.", c.Name(), len(files))
return nil
}
func readArtifactsFile(wd, fn string) ([]*artifact.File, error) {
if !filepath.IsAbs(fn) {
fn = filepath.Join(wd, fn)
}
if _, err := os.Stat(fn); os.IsNotExist(err) {
return nil, errors.Wrapf(err, "file '%s' does not exist", fn)
}
file, err := os.Open(fn)
if err != nil {
return nil, errors.Wrapf(err, "opening file '%s'", fn)
}
defer file.Close()
out := []*artifact.File{}
if err = utility.ReadJSON(file, &out); err != nil {
return nil, errors.Wrapf(err, "reading JSON from file '%s'", fn)
}
return out, nil
}