github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/helper/vagrant/dev_layered.go (about)

     1  package vagrant
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"sort"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/otto/app"
    10  )
    11  
    12  // DevLayered returns a Layered setup for development.
    13  //
    14  // This automatically prepares any layers for foundations. Your custom
    15  // layers are then appended to the foundation layers. If you have no layers,
    16  // no modification is necessary to the returned value. It is ready to go
    17  // as-is.
    18  func DevLayered(ctx *app.Context, layers []*Layer) (*Layered, error) {
    19  	// Basic result
    20  	result := &Layered{
    21  		DataDir: filepath.Join(ctx.GlobalDir, "vagrant-layered"),
    22  		Layers:  make([]*Layer, 0, len(ctx.FoundationDirs)+len(layers)),
    23  	}
    24  
    25  	// Find all the foundation layers
    26  	layersDir := filepath.Join(ctx.Dir, "foundation-layers")
    27  	dir, err := os.Open(layersDir)
    28  	if err != nil {
    29  		// If we don't have foundation layers, we're done!
    30  		if os.IsNotExist(err) {
    31  			result.Layers = layers
    32  			return result, nil
    33  		}
    34  
    35  		return nil, err
    36  	}
    37  
    38  	// Read the directory names
    39  	dirs, err := dir.Readdirnames(-1)
    40  	dir.Close()
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	// Sort the directories so we get the right order
    46  	sort.Strings(dirs)
    47  
    48  	// Go through each directory and add the layer
    49  	for _, dir := range dirs {
    50  		parts := strings.SplitN(dir, "-", 2)
    51  		id := parts[1]
    52  
    53  		result.Layers = append(result.Layers, &Layer{
    54  			ID:          id,
    55  			Vagrantfile: filepath.Join(layersDir, dir, "Vagrantfile"),
    56  		})
    57  	}
    58  
    59  	// Add our final layers
    60  	if len(layers) > 0 {
    61  		result.Layers = append(result.Layers, layers...)
    62  	}
    63  
    64  	return result, nil
    65  }