github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/plugin/builtin/archive/tar_gz_unpack_command.go (about) 1 package archive 2 3 import ( 4 "os" 5 6 "github.com/evergreen-ci/evergreen/archive" 7 "github.com/evergreen-ci/evergreen/model" 8 "github.com/evergreen-ci/evergreen/plugin" 9 "github.com/mitchellh/mapstructure" 10 "github.com/mongodb/grip/slogger" 11 "github.com/pkg/errors" 12 ) 13 14 // Plugin command responsible for unpacking a tgz archive. 15 type TarGzUnpackCommand struct { 16 // the tgz file to unpack 17 Source string `mapstructure:"source" plugin:"expand"` 18 // the directory that the unpacked contents should be put into 19 DestDir string `mapstructure:"dest_dir" plugin:"expand"` 20 } 21 22 func (self *TarGzUnpackCommand) Name() string { 23 return TarGzUnpackCmdName 24 } 25 26 func (self *TarGzUnpackCommand) Plugin() string { 27 return ArchivePluginName 28 } 29 30 // Implementation of ParseParams. 31 func (self *TarGzUnpackCommand) ParseParams(params map[string]interface{}) error { 32 if err := mapstructure.Decode(params, self); err != nil { 33 return errors.Wrapf(err, "error parsing '%v' params", self.Name()) 34 } 35 if err := self.validateParams(); err != nil { 36 return errors.Wrapf(err, "error validating '%v' params", self.Name()) 37 } 38 return nil 39 } 40 41 // Make sure both source and dest dir are speciifed. 42 func (self *TarGzUnpackCommand) validateParams() error { 43 44 if self.Source == "" { 45 return errors.New("source cannot be blank") 46 } 47 if self.DestDir == "" { 48 return errors.New("dest_dir cannot be blank") 49 } 50 51 return nil 52 } 53 54 // Implementation of Execute, to unpack the archive. 55 func (self *TarGzUnpackCommand) Execute(pluginLogger plugin.Logger, 56 pluginCom plugin.PluginCommunicator, 57 conf *model.TaskConfig, 58 stop chan bool) error { 59 60 if err := plugin.ExpandValues(self, conf.Expansions); err != nil { 61 return errors.Wrap(err, "error expanding params") 62 } 63 64 errChan := make(chan error) 65 go func() { 66 errChan <- self.UnpackArchive() 67 }() 68 69 select { 70 case err := <-errChan: 71 return err 72 case <-stop: 73 pluginLogger.LogExecution(slogger.INFO, "Received signal to terminate"+ 74 " execution of targz unpack command") 75 return nil 76 } 77 } 78 79 // UnpackArchive unpacks the archive. The target archive to unpack is 80 // set for the command during parameter parsing. 81 func (self *TarGzUnpackCommand) UnpackArchive() error { 82 83 // get a reader for the source file 84 f, _, tarReader, err := archive.TarGzReader(self.Source) 85 if err != nil { 86 return errors.Wrapf(err, "error opening tar file %v for reading", self.Source) 87 } 88 defer f.Close() 89 90 // extract the actual tarball into the destination directory 91 if err := os.MkdirAll(self.DestDir, 0755); err != nil { 92 return errors.Wrapf(err, "error creating destination dir %v", self.DestDir) 93 } 94 95 return errors.WithStack(archive.Extract(tarReader, self.DestDir)) 96 }