github.com/alde/glide@v0.0.0-20160309204240-d5fc6b676a75/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  	// Check if this is go15, which requires GO15VENDOREXPERIMENT
    54  	// Any release after go15 does not require that env var.
    55  	cmd = exec.Command("go", "version")
    56  	if out, err := cmd.CombinedOutput(); err != nil {
    57  		msg.Err("Error getting version: %s.\n", err)
    58  		os.Exit(1)
    59  	} else if strings.HasPrefix(string(out), "go version 1.5") {
    60  		// This works with 1.5 and 1.6.
    61  		cmd = exec.Command("go", "env", "GO15VENDOREXPERIMENT")
    62  		if out, err := cmd.CombinedOutput(); err != nil {
    63  			msg.Err("Error looking for $GOVENDOREXPERIMENT: %s.\n", err)
    64  			os.Exit(1)
    65  		} else if strings.TrimSpace(string(out)) != "1" {
    66  			msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1")
    67  			os.Exit(1)
    68  		}
    69  	}
    70  
    71  	// In the case where vendoring is explicitly disabled, balk.
    72  	if os.Getenv("GO15VENDOREXPERIMENT") == "0" {
    73  		msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1")
    74  		os.Exit(1)
    75  	}
    76  
    77  	// Verify the setup isn't for the old version of glide. That is, this is
    78  	// no longer assuming the _vendor directory as the GOPATH. Inform of
    79  	// the change.
    80  	if _, err := os.Stat("_vendor/"); err == nil {
    81  		msg.Warn(`Your setup appears to be for the previous version of Glide.
    82  Previously, vendor packages were stored in _vendor/src/ and
    83  _vendor was set as your GOPATH. As of Go 1.5 the go tools
    84  recognize the vendor directory as a location for these
    85  files. Glide has embraced this. Please remove the _vendor
    86  directory or move the _vendor/src/ directory to vendor/.` + "\n")
    87  		os.Exit(1)
    88  	}
    89  }
    90  
    91  // EnsureVendorDir ensures that a vendor/ directory is present in the cwd.
    92  func EnsureVendorDir() {
    93  	fi, err := os.Stat(gpath.VendorDir)
    94  	if err != nil {
    95  		msg.Debug("Creating %s", gpath.VendorDir)
    96  		if err := os.MkdirAll(gpath.VendorDir, os.ModeDir|0755); err != nil {
    97  			msg.Die("Could not create %s: %s", gpath.VendorDir, err)
    98  		}
    99  	} else if !fi.IsDir() {
   100  		msg.Die("Vendor is not a directory")
   101  	}
   102  }
   103  
   104  // EnsureGopath fails if GOPATH is not set, or if $GOPATH/src is missing.
   105  //
   106  // Otherwise it returns the value of GOPATH.
   107  func EnsureGopath() string {
   108  	gps := gpath.Gopaths()
   109  	if len(gps) == 0 {
   110  		msg.Die("$GOPATH is not set.")
   111  	}
   112  
   113  	for _, gp := range gps {
   114  		_, err := os.Stat(path.Join(gp, "src"))
   115  		if err != nil {
   116  			msg.Warn("%s", err)
   117  			continue
   118  		}
   119  		return gp
   120  	}
   121  
   122  	msg.Err("Could not find any of %s/src.\n", strings.Join(gps, "/src, "))
   123  	msg.Info("As of Glide 0.5/Go 1.5, this is required.\n")
   124  	msg.Die("Wihtout src, cannot continue.")
   125  	return ""
   126  }