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

remove getGOPATHS; cleanup #785

Merged
merged 2 commits into from
Jun 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 7 additions & 24 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,20 @@ 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)
}
for _, gp := range GOPATHs {
c.GOPATHs = append(c.GOPATHs, filepath.ToSlash(gp))
Expand All @@ -71,17 +65,6 @@ func (c *Ctx) SetPaths(wd string, GOPATHs ...string) error {
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