Skip to content

Commit

Permalink
GOPATH is a multi-value environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Davim committed Jan 30, 2019
1 parent dd9e0fd commit 7e29baf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build/build_kubebuilder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ set -x
OWNER="sigs.k8s.io"
REPO="kubebuilder"

GO_PKG_OWNER=$GOPATH/src/$OWNER
GO_PKG_OWNER=${GOPATH/:*}/src/$OWNER
GO_PKG_PATH=$GO_PKG_OWNER/$REPO

mkdir -p $GO_PKG_OWNER
Expand Down
15 changes: 10 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
gobuild "go/build"
"log"
"os"
"path/filepath"
"strings"
"regexp"

"github.com/spf13/cobra"

toolsutil "sigs.k8s.io/controller-tools/pkg/crd/util"
"sigs.k8s.io/kubebuilder/cmd/util"
"sigs.k8s.io/kubebuilder/cmd/version"
)
Expand All @@ -34,18 +34,23 @@ func main() {
if len(gopath) == 0 {
gopath = gobuild.Default.GOPATH
}
util.GoSrc = filepath.Join(gopath, "src")

wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

if !strings.HasPrefix(filepath.Dir(wd), util.GoSrc) {
if !toolsutil.IsUnderGoSrcPath(wd) {
log.Fatalf("kubebuilder must be run from the project root under $GOPATH/src/<package>. "+
"\nCurrent GOPATH=%s. \nCurrent directory=%s", gopath, wd)
}
util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1)
util.Repo, err = toolsutil.DirToGoPkg(wd)
if err != nil {
log.Fatal(err)
}

re := regexp.MustCompile(`(^.*\/src)(\/.*$)`)
util.GoSrc = re.ReplaceAllString(wd, "$1")

rootCmd := defaultCommand()

Expand Down
14 changes: 13 additions & 1 deletion common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ cd "$base_dir" || {
exit 1
}

go_workspace=''
for p in ${GOPATH//:/ }; do
if [[ $PWD/ = $p/* ]]; then
go_workspace=$p
fi
done

if [ -z $go_workspace ]; then
echo 'Current directory is not in $GOPATH' >&2
exit 1
fi

# k8s_version=1.11.0
k8s_version=1.13.1
goarch=amd64
Expand Down Expand Up @@ -130,7 +142,7 @@ function build_kb {
}

function prepare_testdir_under_gopath {
kb_test_dir=$GOPATH/src/sigs.k8s.io/kubebuilder-test
kb_test_dir=${go_workspace}/src/sigs.k8s.io/kubebuilder-test
header_text "preparing test directory $kb_test_dir"
rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir"
header_text "running kubebuilder commands in test directory $kb_test_dir"
Expand Down
30 changes: 17 additions & 13 deletions pkg/scaffold/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,24 @@ func (Project) repoFromGopathAndWd(gopath string, getwd func() (string, error))
if len(gopath) == 0 {
gopath = build.Default.GOPATH
}
goSrc := filepath.Join(gopath, "src")

// Make sure the GOPATH is set and the working dir is under the GOPATH
if !strings.HasPrefix(filepath.Dir(wd), goSrc) {
return "", fmt.Errorf("working directory must be a project directory under "+
"$GOPATH/src/<project-package>\n- GOPATH=%s\n- WD=%s", gopath, wd)
}
for _, workspace := range filepath.SplitList(gopath) {
goSrc := filepath.Join(workspace, "src")

// Make sure the GOPATH is set and the working dir is under the GOPATH
if strings.HasPrefix(filepath.Dir(wd), goSrc) {

// Figure out the repo name by removing $GOPATH/src from the working directory - e.g.
// '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools'
repo := ""
for wd != goSrc {
repo = filepath.Join(filepath.Base(wd), repo)
wd = filepath.Dir(wd)
// Figure out the repo name by removing $GOPATH/src from the working directory - e.g.
// '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools'
repo := ""
for wd != goSrc {
repo = filepath.Join(filepath.Base(wd), repo)
wd = filepath.Dir(wd)
}
return repo, nil
}
}
return repo, nil

return "", fmt.Errorf("working directory must be a project directory under "+
"$GOPATH/src/<project-package>\n- GOPATH=%s\n- WD=%s", gopath, wd)
}
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ prepare_testdir_under_gopath
dump_cache
test_create_namespaced_coretype_controller

cd $GOPATH/src/sigs.k8s.io/kubebuilder
cd ${go_workspace}/src/sigs.k8s.io/kubebuilder

go test ./cmd/... ./pkg/...

Expand Down

0 comments on commit 7e29baf

Please sign in to comment.