github.com/s-urbaniak/glide@v0.0.0-20160527141859-5f5e941b1fc4/action/ensure.go (about)

     1  package action
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"path"
     8  	"path/filepath"
     9  	"strings"
    10  
    11  	"github.com/Masterminds/glide/cfg"
    12  	"github.com/Masterminds/glide/msg"
    13  	gpath "github.com/Masterminds/glide/path"
    14  	"github.com/Masterminds/glide/util"
    15  )
    16  
    17  // EnsureConfig loads and returns a config file.
    18  //
    19  // Any error will cause an immediate exit, with an error printed to Stderr.
    20  func EnsureConfig() *cfg.Config {
    21  	yamlpath, err := gpath.Glide()
    22  	if err != nil {
    23  		msg.ExitCode(2)
    24  		msg.Die("Failed to find %s file in directory tree: %s", gpath.GlideFile, err)
    25  	}
    26  
    27  	yml, err := ioutil.ReadFile(yamlpath)
    28  	if err != nil {
    29  		msg.ExitCode(2)
    30  		msg.Die("Failed to load %s: %s", yamlpath, err)
    31  	}
    32  	conf, err := cfg.ConfigFromYaml(yml)
    33  	if err != nil {
    34  		msg.ExitCode(3)
    35  		msg.Die("Failed to parse %s: %s", yamlpath, err)
    36  	}
    37  
    38  	b := filepath.Dir(yamlpath)
    39  	buildContext, err := util.GetBuildContext()
    40  	if err != nil {
    41  		msg.Die("Failed to build an import context while ensuring config: %s", err)
    42  	}
    43  	cwd, err := os.Getwd()
    44  	if err != nil {
    45  		msg.Err("Unable to get the current working directory")
    46  	} else {
    47  		// Determining a package name requires a relative path
    48  		b, err = filepath.Rel(b, cwd)
    49  		if err == nil {
    50  			name := buildContext.PackageName(b)
    51  			if name != conf.Name {
    52  				msg.Warn("The name listed in the config file (%s) does not match the current location (%s)", conf.Name, name)
    53  			}
    54  		} else {
    55  			msg.Warn("Problem finding the config file path (%s) relative to the current directory (%s): %s", b, cwd, err)
    56  		}
    57  	}
    58  
    59  	return conf
    60  }
    61  
    62  // EnsureGoVendor ensures that the Go version is correct.
    63  func EnsureGoVendor() {
    64  	// 6l was removed in 1.5, when vendoring was introduced.
    65  	cmd := exec.Command(goExecutable(), "tool", "6l")
    66  	if _, err := cmd.CombinedOutput(); err == nil {
    67  		msg.Warn("You must install the Go 1.5 or greater toolchain to work with Glide.\n")
    68  		os.Exit(1)
    69  	}
    70  
    71  	// Check if this is go15, which requires GO15VENDOREXPERIMENT
    72  	// Any release after go15 does not require that env var.
    73  	cmd = exec.Command(goExecutable(), "version")
    74  	if out, err := cmd.CombinedOutput(); err != nil {
    75  		msg.Err("Error getting version: %s.\n", err)
    76  		os.Exit(1)
    77  	} else if strings.HasPrefix(string(out), "go version 1.5") {
    78  		// This works with 1.5 and 1.6.
    79  		cmd = exec.Command(goExecutable(), "env", "GO15VENDOREXPERIMENT")
    80  		if out, err := cmd.CombinedOutput(); err != nil {
    81  			msg.Err("Error looking for $GOVENDOREXPERIMENT: %s.\n", err)
    82  			os.Exit(1)
    83  		} else if strings.TrimSpace(string(out)) != "1" {
    84  			msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1")
    85  			os.Exit(1)
    86  		}
    87  	}
    88  
    89  	// In the case where vendoring is explicitly disabled, balk.
    90  	if os.Getenv("GO15VENDOREXPERIMENT") == "0" {
    91  		msg.Err("To use Glide, you must set GO15VENDOREXPERIMENT=1")
    92  		os.Exit(1)
    93  	}
    94  
    95  	// Verify the setup isn't for the old version of glide. That is, this is
    96  	// no longer assuming the _vendor directory as the GOPATH. Inform of
    97  	// the change.
    98  	if _, err := os.Stat("_vendor/"); err == nil {
    99  		msg.Warn(`Your setup appears to be for the previous version of Glide.
   100  Previously, vendor packages were stored in _vendor/src/ and
   101  _vendor was set as your GOPATH. As of Go 1.5 the go tools
   102  recognize the vendor directory as a location for these
   103  files. Glide has embraced this. Please remove the _vendor
   104  directory or move the _vendor/src/ directory to vendor/.` + "\n")
   105  		os.Exit(1)
   106  	}
   107  }
   108  
   109  // EnsureVendorDir ensures that a vendor/ directory is present in the cwd.
   110  func EnsureVendorDir() {
   111  	fi, err := os.Stat(gpath.VendorDir)
   112  	if err != nil {
   113  		msg.Debug("Creating %s", gpath.VendorDir)
   114  		if err := os.MkdirAll(gpath.VendorDir, os.ModeDir|0755); err != nil {
   115  			msg.Die("Could not create %s: %s", gpath.VendorDir, err)
   116  		}
   117  	} else if !fi.IsDir() {
   118  		msg.Die("Vendor is not a directory")
   119  	}
   120  }
   121  
   122  // EnsureGopath fails if GOPATH is not set, or if $GOPATH/src is missing.
   123  //
   124  // Otherwise it returns the value of GOPATH.
   125  func EnsureGopath() string {
   126  	gps := gpath.Gopaths()
   127  	if len(gps) == 0 {
   128  		msg.Die("$GOPATH is not set.")
   129  	}
   130  
   131  	for _, gp := range gps {
   132  		_, err := os.Stat(path.Join(gp, "src"))
   133  		if err != nil {
   134  			msg.Warn("%s", err)
   135  			continue
   136  		}
   137  		return gp
   138  	}
   139  
   140  	msg.Err("Could not find any of %s/src.\n", strings.Join(gps, "/src, "))
   141  	msg.Info("As of Glide 0.5/Go 1.5, this is required.\n")
   142  	msg.Die("Without src, cannot continue.")
   143  	return ""
   144  }
   145  
   146  // goExecutable checks for a set environment variable of GLIDE_GO_EXECUTABLE
   147  // for the go executable name. The Google App Engine SDK ships with a python
   148  // wrapper called goapp
   149  //
   150  // Example usage: GLIDE_GO_EXECUTABLE=goapp glide install
   151  func goExecutable() string {
   152  	goExecutable := os.Getenv("GLIDE_GO_EXECUTABLE")
   153  	if len(goExecutable) <= 0 {
   154  		goExecutable = "go"
   155  	}
   156  
   157  	return goExecutable
   158  }