Skip to content

Commit 7e29baf

Browse files
author
Luis Davim
committed
GOPATH is a multi-value environment variable
1 parent dd9e0fd commit 7e29baf

File tree

5 files changed

+42
-21
lines changed

5 files changed

+42
-21
lines changed

build/build_kubebuilder.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ set -x
2525
OWNER="sigs.k8s.io"
2626
REPO="kubebuilder"
2727

28-
GO_PKG_OWNER=$GOPATH/src/$OWNER
28+
GO_PKG_OWNER=${GOPATH/:*}/src/$OWNER
2929
GO_PKG_PATH=$GO_PKG_OWNER/$REPO
3030

3131
mkdir -p $GO_PKG_OWNER

cmd/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import (
2020
gobuild "go/build"
2121
"log"
2222
"os"
23-
"path/filepath"
24-
"strings"
23+
"regexp"
2524

2625
"github.com/spf13/cobra"
2726

27+
toolsutil "sigs.k8s.io/controller-tools/pkg/crd/util"
2828
"sigs.k8s.io/kubebuilder/cmd/util"
2929
"sigs.k8s.io/kubebuilder/cmd/version"
3030
)
@@ -34,18 +34,23 @@ func main() {
3434
if len(gopath) == 0 {
3535
gopath = gobuild.Default.GOPATH
3636
}
37-
util.GoSrc = filepath.Join(gopath, "src")
3837

3938
wd, err := os.Getwd()
4039
if err != nil {
4140
log.Fatal(err)
4241
}
4342

44-
if !strings.HasPrefix(filepath.Dir(wd), util.GoSrc) {
43+
if !toolsutil.IsUnderGoSrcPath(wd) {
4544
log.Fatalf("kubebuilder must be run from the project root under $GOPATH/src/<package>. "+
4645
"\nCurrent GOPATH=%s. \nCurrent directory=%s", gopath, wd)
4746
}
48-
util.Repo = strings.Replace(wd, util.GoSrc+string(filepath.Separator), "", 1)
47+
util.Repo, err = toolsutil.DirToGoPkg(wd)
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
re := regexp.MustCompile(`(^.*\/src)(\/.*$)`)
53+
util.GoSrc = re.ReplaceAllString(wd, "$1")
4954

5055
rootCmd := defaultCommand()
5156

common.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ cd "$base_dir" || {
4141
exit 1
4242
}
4343

44+
go_workspace=''
45+
for p in ${GOPATH//:/ }; do
46+
if [[ $PWD/ = $p/* ]]; then
47+
go_workspace=$p
48+
fi
49+
done
50+
51+
if [ -z $go_workspace ]; then
52+
echo 'Current directory is not in $GOPATH' >&2
53+
exit 1
54+
fi
55+
4456
# k8s_version=1.11.0
4557
k8s_version=1.13.1
4658
goarch=amd64
@@ -130,7 +142,7 @@ function build_kb {
130142
}
131143

132144
function prepare_testdir_under_gopath {
133-
kb_test_dir=$GOPATH/src/sigs.k8s.io/kubebuilder-test
145+
kb_test_dir=${go_workspace}/src/sigs.k8s.io/kubebuilder-test
134146
header_text "preparing test directory $kb_test_dir"
135147
rm -rf "$kb_test_dir" && mkdir -p "$kb_test_dir" && cd "$kb_test_dir"
136148
header_text "running kubebuilder commands in test directory $kb_test_dir"

pkg/scaffold/project/project.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,24 @@ func (Project) repoFromGopathAndWd(gopath string, getwd func() (string, error))
7676
if len(gopath) == 0 {
7777
gopath = build.Default.GOPATH
7878
}
79-
goSrc := filepath.Join(gopath, "src")
8079

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

87-
// Figure out the repo name by removing $GOPATH/src from the working directory - e.g.
88-
// '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools'
89-
repo := ""
90-
for wd != goSrc {
91-
repo = filepath.Join(filepath.Base(wd), repo)
92-
wd = filepath.Dir(wd)
86+
// Figure out the repo name by removing $GOPATH/src from the working directory - e.g.
87+
// '$GOPATH/src/kubernetes-sigs/controller-tools' becomes 'kubernetes-sigs/controller-tools'
88+
repo := ""
89+
for wd != goSrc {
90+
repo = filepath.Join(filepath.Base(wd), repo)
91+
wd = filepath.Dir(wd)
92+
}
93+
return repo, nil
94+
}
9395
}
94-
return repo, nil
96+
97+
return "", fmt.Errorf("working directory must be a project directory under "+
98+
"$GOPATH/src/<project-package>\n- GOPATH=%s\n- WD=%s", gopath, wd)
9599
}

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ prepare_testdir_under_gopath
127127
dump_cache
128128
test_create_namespaced_coretype_controller
129129

130-
cd $GOPATH/src/sigs.k8s.io/kubebuilder
130+
cd ${go_workspace}/src/sigs.k8s.io/kubebuilder
131131

132132
go test ./cmd/... ./pkg/...
133133

0 commit comments

Comments
 (0)