github.com/daniel-garcia/glide@v0.0.0-20160218012856-2eab91fab790/action/ensure.go (about)

     1  package action
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path"
     8  	"strings"
     9  
    10  	"github.com/Masterminds/glide/cfg"
    11  	"github.com/Masterminds/glide/msg"
    12  	gpath "github.com/Masterminds/glide/path"
    13  )
    14  
    15  // EnsureConfig loads and returns a config file.
    16  //
    17  // Any error will cause an immediate exit, with an error printed to Stderr.
    18  func EnsureConfig() *cfg.Config {
    19  	yamlpath, err := gpath.Glide()
    20  	if err != nil {
    21  		msg.ExitCode(2)
    22  		msg.Die("Failed to find %s file in directory tree: %s", gpath.GlideFile, err)
    23  	}
    24  
    25  	yml, err := ioutil.ReadFile(yamlpath)
    26  	if err != nil {
    27  		msg.ExitCode(2)
    28  		msg.Die("Failed to load %s: %s", yamlpath, err)
    29  	}
    30  	conf, err := cfg.ConfigFromYaml(yml)
    31  	if err != nil {
    32  		msg.ExitCode(3)
    33  		msg.Die("Failed to parse %s: %s", yamlpath, err)
    34  	}
    35  
    36  	return conf
    37  }
    38  
    39  // EnsureCacheDir ensures the existence of the cache directory
    40  func EnsureCacheDir() {
    41  	msg.Warn("ensure.go: ensureCacheDir is not implemented.")
    42  }
    43  
    44  // EnsureGoVendor ensures that the Go version is correct.
    45  func EnsureGoVendor() {
    46  	// 6l was removed in 1.5, when vendoring was introduced.
    47  	cmd := exec.Command("go", "tool", "6l")
    48  	if _, err := cmd.CombinedOutput(); err == nil {
    49  		msg.Warn("You must install the Go 1.5 or greater toolchain to work with Glide.\n")
    50  		os.Exit(1)
    51  	}
    52  
    53  	// This works with 1.5 and >=1.6.
    54  	cmd = exec.Command("go", "env", "GO15VENDOREXPERIMENT")
    55  	if out, err := cmd.CombinedOutput(); err != nil {
    56  		msg.Err("Error looking for $GOVENDOREXPERIMENT: %s.\n", err)
    57  		os.Exit(1)
    58  	} else if strings.TrimSpace(string(out)) != "1" {
    59  		msg.Warn("To use Glide, you must set GO15VENDOREXPERIMENT=1\n")
    60  		os.Exit(1)
    61  	}
    62  
    63  	// Verify the setup isn't for the old version of glide. That is, this is
    64  	// no longer assuming the _vendor directory as the GOPATH. Inform of
    65  	// the change.
    66  	if _, err := os.Stat("_vendor/"); err == nil {
    67  		msg.Warn(`Your setup appears to be for the previous version of Glide.
    68  Previously, vendor packages were stored in _vendor/src/ and
    69  _vendor was set as your GOPATH. As of Go 1.5 the go tools
    70  recognize the vendor directory as a location for these
    71  files. Glide has embraced this. Please remove the _vendor
    72  directory or move the _vendor/src/ directory to vendor/.` + "\n")
    73  		os.Exit(1)
    74  	}
    75  }
    76  
    77  // EnsureVendorDir ensures that a vendor/ directory is present in the cwd.
    78  func EnsureVendorDir() {
    79  	fi, err := os.Stat(gpath.VendorDir)
    80  	if err != nil {
    81  		msg.Debug("Creating %s", gpath.VendorDir)
    82  		if err := os.MkdirAll(gpath.VendorDir, os.ModeDir|0755); err != nil {
    83  			msg.Die("Could not create %s: %s", gpath.VendorDir, err)
    84  		}
    85  	} else if !fi.IsDir() {
    86  		msg.Die("Vendor is not a directory")
    87  	}
    88  }
    89  
    90  // EnsureGopath fails if GOPATH is not set, or if $GOPATH/src is missing.
    91  //
    92  // Otherwise it returns the value of GOPATH.
    93  func EnsureGopath() string {
    94  	gps := gpath.Gopaths()
    95  	if len(gps) == 0 {
    96  		msg.Die("$GOPATH is not set.")
    97  	}
    98  
    99  	for _, gp := range gps {
   100  		_, err := os.Stat(path.Join(gp, "src"))
   101  		if err != nil {
   102  			msg.Warn("%s", err)
   103  			continue
   104  		}
   105  		return gp
   106  	}
   107  
   108  	msg.Err("Could not find any of %s/src.\n", strings.Join(gps, "/src, "))
   109  	msg.Info("As of Glide 0.5/Go 1.5, this is required.\n")
   110  	msg.Die("Wihtout src, cannot continue.")
   111  	return ""
   112  }