-
Notifications
You must be signed in to change notification settings - Fork 124
/
archive_zip_create.go
96 lines (74 loc) · 2.56 KB
/
archive_zip_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
92
93
94
95
96
package command
import (
"context"
"path/filepath"
"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/message"
"github.com/pkg/errors"
)
type zipArchiveCreate struct {
// the tgz file that will be created
Target string `mapstructure:"target" plugin:"expand"`
// the directory to compress
SourceDir string `mapstructure:"source_dir" plugin:"expand"`
// a list of filename blobs to include,
// e.g. "*.tgz", "file.txt", "test_*"
Include []string `mapstructure:"include" plugin:"expand"`
// a list of filename blobs to exclude,
// e.g. "*.zip", "results.out", "ignore/**"
ExcludeFiles []string `mapstructure:"exclude_files" plugin:"expand"`
base
}
func zipArchiveCreateFactory() Command { return &zipArchiveCreate{} }
func (c *zipArchiveCreate) Name() string { return "archive.zip_pack" }
func (c *zipArchiveCreate) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, c); err != nil {
return errors.Wrap(err, "decoding mapstructure params")
}
if c.Target == "" {
return errors.New("target cannot be blank")
}
if c.SourceDir == "" {
return errors.New("source directory cannot be blank")
}
if len(c.Include) == 0 {
return errors.New("include cannot be empty")
}
return nil
}
func (c *zipArchiveCreate) 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")
}
// if the source dir is a relative path, join it to the working dir
if !filepath.IsAbs(c.SourceDir) {
c.SourceDir = GetWorkingDirectory(conf, c.SourceDir)
}
// if the target is a relative path, join it to the working dir
if !filepath.IsAbs(c.Target) {
c.Target = GetWorkingDirectory(conf, c.Target)
}
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.NewZip().Archive(filenames, c.Target); err != nil {
return errors.Wrapf(err, "constructing zip archive '%s'", c.Target)
}
logger.Task().Info(message.Fields{
"target": c.Target,
"num_files": len(filenames),
"message": "successfully created archive",
"format": "zip",
})
return nil
}