/
archive_zip_extract.go
63 lines (48 loc) · 1.73 KB
/
archive_zip_extract.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
package command
import (
"context"
"os"
"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/pkg/errors"
)
type zipExtract struct {
ArchivePath string `mapstructure:"path" plugin:"expand"`
TargetDirectory string `mapstructure:"destination" plugin:"expand"`
base
}
func zipExtractFactory() Command { return &zipExtract{} }
func (e *zipExtract) Name() string { return "archive.zip_extract" }
func (e *zipExtract) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, e); err != nil {
return errors.Wrapf(err, "decoding mapstructure params")
}
if e.ArchivePath == "" {
return errors.New("archive path must be specified")
}
return nil
}
func (e *zipExtract) Execute(ctx context.Context,
client client.Communicator, logger client.LoggerProducer, conf *internal.TaskConfig) error {
if err := util.ExpandValues(e, &conf.Expansions); err != nil {
return errors.Wrap(err, "applying expansions")
}
// if the target is a relative path, join it to the working dir
if !filepath.IsAbs(e.TargetDirectory) {
e.TargetDirectory = GetWorkingDirectory(conf, e.TargetDirectory)
}
if !filepath.IsAbs(e.ArchivePath) {
e.ArchivePath = GetWorkingDirectory(conf, e.ArchivePath)
}
if _, err := os.Stat(e.ArchivePath); os.IsNotExist(err) {
return errors.Errorf("archive '%s' does not exist", e.ArchivePath)
}
if err := archiver.NewZip().Unarchive(e.ArchivePath, e.TargetDirectory); err != nil {
return errors.Wrapf(err, "extracting archive '%s' to directory '%s'", e.ArchivePath, e.TargetDirectory)
}
return nil
}