gopkg.in/tools/godep.v21@v21.0.0-20151104013723-2cf1d6e3f557/go.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  	"os/exec"
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  var cmdGo = &Command{
    13  	Usage: "go command [arguments]",
    14  	Short: "run the go tool with saved dependencies",
    15  	Long: `
    16  Go runs the go tool with a modified GOPATH giving access to
    17  dependencies saved in Godeps.
    18  
    19  Any go tool command can run this way, but "godep go get"
    20  is unnecessary and has been disabled. Instead, use
    21  "godep go install".
    22  `,
    23  	Run: runGo,
    24  }
    25  
    26  // Find the godep GOPATH for this file tree and run the go tool.
    27  func runGo(cmd *Command, args []string) {
    28  	gopath := prepareGopath()
    29  	if s := os.Getenv("GOPATH"); s != "" {
    30  		gopath += string(os.PathListSeparator) + os.Getenv("GOPATH")
    31  	}
    32  	if len(args) > 0 && args[0] == "get" {
    33  		log.Printf("invalid subcommand: %q", "go get")
    34  		fmt.Fprintln(os.Stderr, "Use 'godep go install' instead.")
    35  		fmt.Fprintln(os.Stderr, "Run 'godep help go' for usage.")
    36  		os.Exit(2)
    37  	}
    38  	c := exec.Command("go", args...)
    39  	c.Env = append(envNoGopath(), "GOPATH="+gopath)
    40  	c.Stdin = os.Stdin
    41  	c.Stdout = os.Stdout
    42  	c.Stderr = os.Stderr
    43  	err := c.Run()
    44  	if err != nil {
    45  		log.Fatalln("go", err)
    46  	}
    47  }
    48  
    49  // prepareGopath reads dependency information from the filesystem
    50  // entry name, fetches any necessary code, and returns a gopath
    51  // causing the specified dependencies to be used.
    52  func prepareGopath() (gopath string) {
    53  	dir, isDir := findGodeps()
    54  	if dir == "" {
    55  		log.Fatalln("No Godeps found (or in any parent directory)")
    56  	}
    57  	if !isDir {
    58  		log.Fatalln(strings.TrimSpace(needSource))
    59  	}
    60  	return filepath.Join(dir, "Godeps", "_workspace")
    61  }
    62  
    63  // findGodeps looks for a directory entry "Godeps" in the
    64  // current directory or any parent, and returns the containing
    65  // directory and whether the entry itself is a directory.
    66  // If Godeps can't be found, findGodeps returns "".
    67  // For any other error, it exits the program.
    68  func findGodeps() (dir string, isDir bool) {
    69  	wd, err := os.Getwd()
    70  	if err != nil {
    71  		log.Fatalln(err)
    72  	}
    73  	return findInParents(wd, "Godeps")
    74  }
    75  
    76  // isRoot returns true iff a path is a root.
    77  // On Unix: "/".
    78  // On Windows: "C:\", "D:\", ...
    79  func isRoot(p string) bool {
    80  	p = filepath.Clean(p)
    81  	volume := filepath.VolumeName(p)
    82  
    83  	p = strings.TrimPrefix(p, volume)
    84  	p = filepath.ToSlash(p)
    85  
    86  	return p == "/"
    87  }
    88  
    89  // findInParents returns the path to the directory containing name
    90  // in dir or any ancestor, and whether name itself is a directory.
    91  // If name cannot be found, findInParents returns the empty string.
    92  func findInParents(dir, name string) (container string, isDir bool) {
    93  	for {
    94  		fi, err := os.Stat(filepath.Join(dir, name))
    95  		if os.IsNotExist(err) && isRoot(dir) {
    96  			return "", false
    97  		}
    98  		if os.IsNotExist(err) {
    99  			dir = filepath.Dir(dir)
   100  			continue
   101  		}
   102  		if err != nil {
   103  			log.Fatalln(err)
   104  		}
   105  		return dir, fi.IsDir()
   106  	}
   107  }
   108  
   109  func envNoGopath() (a []string) {
   110  	for _, s := range os.Environ() {
   111  		if !strings.HasPrefix(s, "GOPATH=") {
   112  			a = append(a, s)
   113  		}
   114  	}
   115  	return a
   116  }
   117  
   118  const needSource = `
   119  outdated Godeps missing source code
   120  
   121  This dependency list was created with an old version of godep.
   122  
   123  To work around this, you have two options:
   124  1. Run 'godep restore', and try again.
   125  2. Ask the maintainer to switch to a newer version of godep,
   126  then try again with the updated package.
   127  `