Skip to content
This repository has been archived by the owner on Feb 26, 2019. It is now read-only.

Commit

Permalink
Enforce requirement to be inside of a go src directory.
Browse files Browse the repository at this point in the history
A lot of time is usually spent tracking down bug reports where users are
using godep outside of their $GOPATH. This should help with that, at least
until there it time to properly test godep use outside of a $GOPATH and
fix the issues.
  • Loading branch information
Edward Muller committed Mar 18, 2016
1 parent a62a914 commit 670c16b
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 16 deletions.
3 changes: 2 additions & 1 deletion diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Shows the difference, in a unified diff format, between the
current set of dependencies and those generated on a
previous 'go save' execution.
`,
Run: runDiff,
Run: runDiff,
OnlyInGOPATH: true,
}

func runDiff(cmd *Command, args []string) {
Expand Down
3 changes: 2 additions & 1 deletion get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ If -t is given, dependencies of test files are also downloaded and installed.
For more about specifying packages, see 'go help packages'.
`,
Run: runGet,
Run: runGet,
OnlyInGOPATH: true,
}

var getT bool
Expand Down
3 changes: 2 additions & 1 deletion go.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Any go tool command can run this way, but "godep go get"
is unnecessary and has been disabled. Instead, use
"godep go install".
`,
Run: runGo,
Run: runGo,
OnlyInGOPATH: true,
}

// Find the godep GOPATH for this file tree and run the go tool.
Expand Down
46 changes: 40 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"go/build"
"io"
"log"
"os"
Expand Down Expand Up @@ -43,6 +44,9 @@ type Command struct {

// Flag is a set of flags specific to this command.
Flag flag.FlagSet

// OnlyInGOPATH limits this command to being run only while inside of a GOPATH
OnlyInGOPATH bool
}

// UsageExit prints usage information and exits.
Expand Down Expand Up @@ -110,14 +114,17 @@ func main() {
log.Fatal(err)
}

VendorExperiment = determineVendor(majorGoVersion)

// sep is the signature set of path elements that
// precede the original path of an imported package.
sep = defaultSep(VendorExperiment)

for _, cmd := range commands {
if cmd.Name == args[0] {
if cmd.OnlyInGOPATH {
checkInGOPATH()
}

VendorExperiment = determineVendor(majorGoVersion)
// sep is the signature set of path elements that
// precede the original path of an imported package.
sep = defaultSep(VendorExperiment)

cmd.Flag.BoolVar(&verbose, "v", false, "enable verbose output")
cmd.Flag.BoolVar(&debug, "d", false, "enable debug output")
cmd.Flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file")
Expand Down Expand Up @@ -147,6 +154,33 @@ func main() {
os.Exit(2)
}

func subPath(sub, path string) bool {
ls := strings.ToLower(sub)
lp := strings.ToLower(path)
if ls == lp {
return false
}
return strings.HasPrefix(ls, lp)
}

func checkInGOPATH() {
pwd, err := os.Getwd()
if err != nil {
log.Fatal("Unable to determine current working directory", err)
}
dirs := build.Default.SrcDirs()
for _, p := range dirs {
if ok := subPath(pwd, p); ok {
return
}
}

log.Println("godep should only be used inside a valid go package directory.")
log.Println("You are probably outside of your $GOPATH.")
log.Printf("\tCurrent Directory: %s\n", pwd)
log.Fatalf("\t$GOPATH: %s\n", os.Getenv("GOPATH"))
}

var usageTemplate = `
Godep is a tool for managing Go package dependencies.
Expand Down
31 changes: 31 additions & 0 deletions match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,34 @@ func TestMatchPattern(t *testing.T) {
}
}
}

func TestSubPath(t *testing.T) {
cases := []struct {
sub string
dir string
want bool
}{
//Basic
{`/Users/emuller/go/src/github.com/tools/godep`, `/Users/emuller/go`, true},
//Case insensitive filesystem used in dir
{`/Users/emuller/go/src/github.com/tools/godep`, `/Users/Emuller/go`, true},
{`/Users/emuller/go/Src/github.com/tools/godep`, `/Users/Emuller/go`, true},
//spaces
{`/Users/e muller/go/Src/github.com/tools/godep`, `/Users/E muller/go`, true},
// ()
{`/Users/e muller/(Personal)/go/Src/github.com/tools/godep`, `/Users/E muller/(Personal)/go`, true},
//Not even close, but same length
{`/foo`, `/bar`, false},
// Same, so not sub path (same path)
{`/foo`, `/foo`, false},
// Windows with different cases
{`c:\foo\bar`, `C:\foo`, true},
}

for _, test := range cases {
ok := subPath(test.sub, test.dir)
if ok != test.want {
t.Errorf("subdir(%s,%s) = %v want %v", test.sub, test.dir, ok, test.want)
}
}
}
3 changes: 2 additions & 1 deletion path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ the environment.
For more about how GOPATH works, see 'go help gopath'.
`,
Run: runPath,
Run: runPath,
OnlyInGOPATH: true,
}

// Print the gopath that points to
Expand Down
3 changes: 2 additions & 1 deletion restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var cmdRestore = &Command{
Restore checks out the Godeps-specified version of each package in GOPATH.
`,
Run: runRestore,
Run: runRestore,
OnlyInGOPATH: true,
}

// Three phases:
Expand Down
3 changes: 2 additions & 1 deletion save.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ also saved.
For more about specifying packages, see 'go help packages'.
`,
Run: runSave,
Run: runSave,
OnlyInGOPATH: true,
}

var (
Expand Down
8 changes: 5 additions & 3 deletions update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ var cmdUpdate = &Command{
Long: `
Update changes the named dependency packages to use the
revision of each currently installed in GOPATH. New code will
be copied into the Godeps workspace or vendor folder and the
be copied into the Godeps workspace or vendor folder and the
new revision will be written to the manifest.
If -goversion is specified, update the recorded go version.
If -goversion is specified, update the recorded go version.
For more about specifying packages, see 'go help packages'.
`,
Run: runUpdate,
Run: runUpdate,
OnlyInGOPATH: true,
}

var (
updateGoVer bool
)
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"runtime"
)

const version = 58
const version = 59

var cmdVersion = &Command{
Name: "version",
Expand Down

0 comments on commit 670c16b

Please sign in to comment.