github.com/markdia/terraform@v0.5.1-0.20150508012022-f1ae920aa970/config/module/get_file.go (about)

     1  package module
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  // FileGetter is a Getter implementation that will download a module from
    11  // a file scheme.
    12  type FileGetter struct{}
    13  
    14  func (g *FileGetter) Get(dst string, u *url.URL) error {
    15  	// The source path must exist and be a directory to be usable.
    16  	if fi, err := os.Stat(u.Path); err != nil {
    17  		return fmt.Errorf("source path error: %s", err)
    18  	} else if !fi.IsDir() {
    19  		return fmt.Errorf("source path must be a directory")
    20  	}
    21  
    22  	fi, err := os.Lstat(dst)
    23  	if err != nil && !os.IsNotExist(err) {
    24  		return err
    25  	}
    26  
    27  	// If the destination already exists, it must be a symlink
    28  	if err == nil {
    29  		mode := fi.Mode()
    30  		if mode&os.ModeSymlink == 0 {
    31  			return fmt.Errorf("destination exists and is not a symlink")
    32  		}
    33  
    34  		// Remove the destination
    35  		if err := os.Remove(dst); err != nil {
    36  			return err
    37  		}
    38  	}
    39  
    40  	// Create all the parent directories
    41  	if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
    42  		return err
    43  	}
    44  
    45  	return os.Symlink(u.Path, dst)
    46  }