-
Notifications
You must be signed in to change notification settings - Fork 124
/
archive_auto_create.go
91 lines (71 loc) · 2.89 KB
/
archive_auto_create.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 command
import (
"context"
"github.com/evergreen-ci/evergreen/agent/internal"
"github.com/evergreen-ci/evergreen/agent/internal/client"
"github.com/evergreen-ci/evergreen/util"
"github.com/mholt/archiver/v3"
"github.com/mitchellh/mapstructure"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
type autoArchiveCreate struct {
// Target is the archive file that will be created. The target file's
// extension will determine the archiving format to use.
Target string `mapstructure:"target" plugin:"expand"`
// SourceDir is the directory to compress.
SourceDir string `mapstructure:"source_dir" plugin:"expand"`
// Include is a list of filename blobs to include from the source directory,
// e.g. "*.tgz", "file.txt", "test_*". If not specified, the entire source
// directory will be used.
Include []string `mapstructure:"include" plugin:"expand"`
// ExcludeFiles is a list of filename blobs to exclude from the source
// directory, e.g. "*.auto", "results.out", "ignore/**"
ExcludeFiles []string `mapstructure:"exclude_files" plugin:"expand"`
base
}
func autoArchiveCreateFactory() Command { return &autoArchiveCreate{} }
func (c *autoArchiveCreate) Name() string { return "archive.auto_pack" }
func (c *autoArchiveCreate) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, c); err != nil {
return errors.Wrap(err, "decoding mapstructure params")
}
catcher := grip.NewBasicCatcher()
catcher.NewWhen(c.Target == "", "target cannot be blank")
catcher.NewWhen(c.SourceDir == "", "source directory cannot be blank")
catcher.NewWhen(len(c.ExcludeFiles) > 0 && len(c.Include) == 0, "if specifying files to exclude, must also specify files to include")
return catcher.Resolve()
}
func (c *autoArchiveCreate) Execute(ctx context.Context,
client client.Communicator, logger client.LoggerProducer, conf *internal.TaskConfig) error {
if err := util.ExpandValues(c, &conf.Expansions); err != nil {
return errors.Wrap(err, "applying expansions")
}
c.SourceDir = GetWorkingDirectory(conf, c.SourceDir)
c.Target = GetWorkingDirectory(conf, c.Target)
var filenames []string
if len(c.Include) == 0 && len(c.ExcludeFiles) == 0 {
// If using the whole source directory, skip the unnecessary search for
// matching files.
filenames = []string{c.SourceDir}
} else {
files, _, err := findContentsToArchive(ctx, c.SourceDir, c.Include, c.ExcludeFiles)
if err != nil {
return errors.Wrap(err, "finding files to archive")
}
filenames = make([]string, len(files))
for idx := range files {
filenames[idx] = files[idx].path
}
}
if err := archiver.Archive(filenames, c.Target); err != nil {
return errors.Wrapf(err, "constructing auto archive '%s'", c.Target)
}
logger.Task().Info(message.Fields{
"target": c.Target,
"num_files": len(filenames),
"message": "successfully created archive",
})
return nil
}