Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #785 from jmank88/getGOPATHs
Browse files Browse the repository at this point in the history
remove getGOPATHS; cleanup
  • Loading branch information
darkowlzz authored Jun 25, 2017
2 parents 9f3d2ec + ac8ec85 commit f16f7a8
Showing 1 changed file with 7 additions and 24 deletions.
31 changes: 7 additions & 24 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,27 @@ type Ctx struct {
Verbose bool // Enables more verbose logging.
}

// SetPaths sets the WorkingDir and GOPATHSs fields.
//
// ctx := &dep.Ctx{
// Out: log.New(os.Stdout, "", 0),
// Err: log.New(os.Stderr, "", 0),
// }
//
// err := ctx.SetPaths(workingDir, filepath.SplitList(os.Getenv("GOPATH"))
// if err != nil {
// // Empty GOPATH
// }
//
// SetPaths sets the WorkingDir and GOPATHs fields. If GOPATHs is empty, then
// the GOPATH environment variable (or the default GOPATH) is used instead.
func (c *Ctx) SetPaths(wd string, GOPATHs ...string) error {
if wd == "" {
return errors.New("cannot set Ctx.WorkingDir to an empty path")
}
c.WorkingDir = wd

if len(GOPATHs) == 0 {
GOPATHs = getGOPATHs(os.Environ())
GOPATH := os.Getenv("GOPATH")
if GOPATH == "" {
GOPATH = defaultGOPATH()
}
GOPATHs = filepath.SplitList(GOPATH)
}

c.GOPATHs = append(c.GOPATHs, GOPATHs...)

return nil
}

// getGOPATH returns the GOPATHs from the passed environment variables.
// If GOPATH is not defined, fallback to defaultGOPATH().
func getGOPATHs(env []string) []string {
GOPATH := os.Getenv("GOPATH")
if GOPATH == "" {
GOPATH = defaultGOPATH()
}

return filepath.SplitList(GOPATH)
}

// defaultGOPATH gets the default GOPATH that was added in 1.8
// copied from go/build/build.go
func defaultGOPATH() string {
Expand Down

0 comments on commit f16f7a8

Please sign in to comment.