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