Skip to content

Commit

Permalink
dev-doctor: use default GOPATH when missing from env
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Perrin <alex@kaworu.ch>
  • Loading branch information
kaworu committed Sep 14, 2021
1 parent e0da2e4 commit 33afb32
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
14 changes: 13 additions & 1 deletion tools/dev-doctor/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package main

import "os/exec"
import (
"go/build"
"os"
"os/exec"
)

// sudo returns cmd run with sudo. cmd is modified in place.
func sudo(cmd *exec.Cmd) (*exec.Cmd, error) {
Expand All @@ -15,3 +19,11 @@ func sudo(cmd *exec.Cmd) (*exec.Cmd, error) {
cmd.Path = sudoPath
return cmd, nil
}

// goPath returns the environment $GOPATH, or the default when empty or unset.
func goPath() string {
if gp := os.Getenv("GOPATH"); gp != "" {
return gp
}
return build.Default.GOPATH
}
2 changes: 1 addition & 1 deletion tools/dev-doctor/rootcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) {
osArchCheck{},
unameCheck{},
rootDirCheck{
rootDir: "$GOPATH/src/github.com/cilium/cilium",
rootDir: goPath() + "/src/github.com/cilium/cilium",
},
&binaryCheck{
name: "make",
Expand Down
9 changes: 2 additions & 7 deletions tools/dev-doctor/rootdircheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ func (c rootDirCheck) Name() string {
}

func (c rootDirCheck) Run() (checkResult, string) {
goPath := os.Getenv("GOPATH")
if goPath == "" {
return checkWarning, "$GOPATH is not set"
}

dir, err := os.Getwd()
if err != nil {
return checkError, fmt.Sprintf("cannot get working directory: %s", err)
Expand All @@ -40,8 +35,8 @@ func (c rootDirCheck) Run() (checkResult, string) {
case err == nil && info.Mode().IsDir():
if dir != os.ExpandEnv(c.rootDir) {
foundDir := dir
if strings.HasPrefix(dir, goPath+"/") {
foundDir = "$GOPATH/" + dir[len(goPath)+1:]
if strings.HasPrefix(dir, goPath()+"/") {
foundDir = "$GOPATH/" + dir[len(goPath())+1:]
}
return checkWarning, fmt.Sprintf("found %s, want %s", foundDir, c.rootDir)
}
Expand Down

0 comments on commit 33afb32

Please sign in to comment.