github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/archive/archiver.go (about)

     1  package archive
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  type Archiver interface {
     9  	ArchiveContent(content []byte, infilename string) error
    10  	ArchiveFile(infilename string) error
    11  	ArchiveDir(indirname string) error
    12  }
    13  
    14  type ArchiverBuilder func(filepath string) Archiver
    15  
    16  var archiverBuilders = map[string]ArchiverBuilder{
    17  	"zip": NewZipArchiver,
    18  }
    19  
    20  func getArchiver(archiveType string, filepath string) Archiver {
    21  	if builder, ok := archiverBuilders[archiveType]; ok {
    22  		return builder(filepath)
    23  	}
    24  	return nil
    25  }
    26  
    27  func assertValidFile(infilename string) (os.FileInfo, error) {
    28  	fi, err := os.Stat(infilename)
    29  	if err != nil && os.IsNotExist(err) {
    30  		return fi, fmt.Errorf("could not archive missing file: %s", infilename)
    31  	}
    32  	return fi, err
    33  }
    34  
    35  func assertValidDir(indirname string) (os.FileInfo, error) {
    36  	fi, err := os.Stat(indirname)
    37  	if err != nil {
    38  		if os.IsNotExist(err) {
    39  			return fi, fmt.Errorf("could not archive missing directory: %s", indirname)
    40  		}
    41  		return fi, err
    42  	}
    43  	if !fi.IsDir() {
    44  		return fi, fmt.Errorf("could not archive directory that is a file: %s", indirname)
    45  	}
    46  	return fi, nil
    47  }