// UnpackArchive unpacks the archive. The target archive to unpack is
// set for the command during parameter parsing.
func (self *TarGzUnpackCommand) UnpackArchive() error {

	// get a reader for the source file
	f, _, tarReader, err := archive.TarGzReader(self.Source)
	if err != nil {
		return fmt.Errorf("error opening tar file %v for reading: %v",
			self.Source, err)
	}
	defer f.Close()

	// extract the actual tarball into the destination directory
	if err := os.MkdirAll(self.DestDir, 0755); err != nil {
		return fmt.Errorf("error creating destination dir %v: %v", self.DestDir,
			err)
	}

	return archive.Extract(tarReader, self.DestDir)
}
Пример #2
0
// Fetch the specified resource from s3.
func (self *S3GetCommand) Get() error {

	// get the appropriate session and bucket
	auth := &aws.Auth{
		AccessKey: self.AwsKey,
		SecretKey: self.AwsSecret,
	}

	session := thirdparty.NewS3Session(auth, aws.USEast)
	bucket := session.Bucket(self.Bucket)

	// get a reader for the bucket
	reader, err := bucket.GetReader(self.RemoteFile)
	if err != nil {
		return fmt.Errorf("error getting bucket reader for file %v: %v",
			self.RemoteFile, err)
	}
	defer reader.Close()

	// either untar the remote, or just write to a file
	if self.LocalFile != "" {

		// remove the file, if it exists
		exists, err := util.FileExists(self.LocalFile)
		if err != nil {
			return fmt.Errorf("error checking existence of local file %v: %v",
				self.LocalFile, err)
		}
		if exists {
			if err := os.RemoveAll(self.LocalFile); err != nil {
				return fmt.Errorf("error clearing local file %v: %v",
					self.LocalFile, err)
			}
		}

		// open the local file
		file, err := os.Create(self.LocalFile)
		if err != nil {
			return fmt.Errorf("error opening local file %v: %v", self.LocalFile,
				err)
		}
		defer file.Close()

		_, err = io.Copy(file, reader)
		return err

	} else {

		// wrap the reader in a gzip reader and a tar reader
		gzipReader, err := gzip.NewReader(reader)
		if err != nil {
			return fmt.Errorf("error creating gzip reader for %v: %v",
				self.RemoteFile, err)
		}

		tarReader := tar.NewReader(gzipReader)
		err = archive.Extract(tarReader, self.ExtractTo)
		if err != nil {
			return fmt.Errorf("error extracting %v to %v: %v", self.RemoteFile,
				self.ExtractTo, err)
		}
	}

	return nil
}