/
archive_tarball_extract.go
78 lines (59 loc) · 2.13 KB
/
archive_tarball_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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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/mitchellh/mapstructure"
"github.com/pkg/errors"
)
type tarballExtract struct {
ArchivePath string `mapstructure:"path" plugin:"expand"`
TargetDirectory string `mapstructure:"destination" plugin:"expand"`
// a list of filename blobs to exclude when extracting
ExcludeFiles []string `mapstructure:"exclude_files" plugin:"expand"`
base
}
func tarballExtractFactory() Command { return &tarballExtract{} }
func (e *tarballExtract) Name() string { return "archive.targz_extract" }
func (e *tarballExtract) ParseParams(params map[string]interface{}) error {
if err := mapstructure.Decode(params, e); err != nil {
return errors.Wrap(err, "decoding mapstructure params")
}
if e.ArchivePath == "" {
return errors.New("archive path must be specified")
}
return nil
}
func (e *tarballExtract) 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 e.TargetDirectory == "" {
return errors.New("must specify a target directory")
}
// 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)
}
archive, err := os.Open(e.ArchivePath)
if err != nil {
return errors.Wrapf(err, "reading file '%s'", e.ArchivePath)
}
defer func() {
logger.Task().Notice(errors.Wrapf(archive.Close(), "closing file '%s'", e.ArchivePath))
}()
if err := extractTarball(ctx, archive, e.TargetDirectory, e.ExcludeFiles); err != nil {
return errors.Wrapf(err, "extracting file '%s'", e.ArchivePath)
}
return nil
}