Skip to content

Commit eace682

Browse files
authored
commands/operator-sdk/cmd: set composed GOPATH for Go projects (fix #712) (#723)
* internal/util/projectutil: rename Go pkg check function; only call SetGopath() in pkg check function * remove unecessary repoPath(), better log messages * set and return processed path
1 parent 15b332d commit eace682

File tree

7 files changed

+44
-50
lines changed

7 files changed

+44
-50
lines changed

commands/operator-sdk/cmd/add/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func apiRun(cmd *cobra.Command, args []string) {
7676
absProjectPath := projutil.MustGetwd()
7777

7878
cfg := &input.Config{
79-
Repo: projutil.CheckAndGetCurrPkg(),
79+
Repo: projutil.CheckAndGetProjectGoPkg(),
8080
AbsProjectPath: absProjectPath,
8181
}
8282

commands/operator-sdk/cmd/add/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func controllerRun(cmd *cobra.Command, args []string) {
6868
}
6969

7070
cfg := &input.Config{
71-
Repo: projutil.CheckAndGetCurrPkg(),
71+
Repo: projutil.CheckAndGetProjectGoPkg(),
7272
AbsProjectPath: projutil.MustGetwd(),
7373
}
7474

commands/operator-sdk/cmd/build.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
146146

147147
// Don't need to build go code if Ansible Operator
148148
if mainExists() {
149-
managerDir := filepath.Join(projutil.CheckAndGetCurrPkg(), scaffold.ManagerDir)
149+
managerDir := filepath.Join(projutil.CheckAndGetProjectGoPkg(), scaffold.ManagerDir)
150150
outputBinName := filepath.Join(wd, scaffold.BuildBinDir, filepath.Base(wd))
151151
buildCmd := exec.Command("go", "build", "-o", outputBinName, managerDir)
152152
buildCmd.Env = goBuildEnv
@@ -192,7 +192,7 @@ func buildFunc(cmd *cobra.Command, args []string) {
192192

193193
absProjectPath := projutil.MustGetwd()
194194
cfg := &input.Config{
195-
Repo: projutil.CheckAndGetCurrPkg(),
195+
Repo: projutil.CheckAndGetProjectGoPkg(),
196196
AbsProjectPath: absProjectPath,
197197
ProjectName: filepath.Base(wd),
198198
}

commands/operator-sdk/cmd/generate/k8s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func k8sFunc(cmd *cobra.Command, args []string) {
5454
func K8sCodegen() {
5555

5656
projutil.MustInProjectRoot()
57-
repoPkg := projutil.CheckAndGetCurrPkg()
57+
repoPkg := projutil.CheckAndGetProjectGoPkg()
5858
outputPkg := filepath.Join(repoPkg, "pkg/generated")
5959
apisPkg := filepath.Join(repoPkg, scaffold.ApisDir)
6060
groupVersions, err := parseGroupVersions()

commands/operator-sdk/cmd/new.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ var (
6868
)
6969

7070
const (
71-
gopath = "GOPATH"
72-
src = "src"
7371
dep = "dep"
7472
ensureCmd = "ensure"
7573
)
@@ -120,7 +118,7 @@ func mustBeNewProject() {
120118

121119
func doScaffold() {
122120
cfg := &input.Config{
123-
Repo: filepath.Join(projutil.CheckAndGetCurrPkg(), projectName),
121+
Repo: filepath.Join(projutil.CheckAndGetProjectGoPkg(), projectName),
124122
AbsProjectPath: filepath.Join(projutil.MustGetwd(), projectName),
125123
ProjectName: projectName,
126124
}
@@ -221,28 +219,6 @@ func doAnsibleScaffold() {
221219
}
222220
}
223221

224-
// repoPath checks if this project's repository path is rooted under $GOPATH and returns project's repository path.
225-
// repoPath field on generator is used primarily in generation of Go operator. For Ansible we will set it to cwd
226-
func repoPath() string {
227-
// We only care about GOPATH constraint checks if we are a Go operator
228-
wd := projutil.MustGetwd()
229-
if operatorType == projutil.OperatorTypeGo {
230-
gp := os.Getenv(gopath)
231-
if len(gp) == 0 {
232-
log.Fatal("$GOPATH env not set")
233-
}
234-
// check if this project's repository path is rooted under $GOPATH
235-
if !strings.HasPrefix(wd, gp) {
236-
log.Fatalf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp)
237-
}
238-
// compute the repo path by stripping "$GOPATH/src/" from the path of the current directory.
239-
rp := filepath.Join(string(wd[len(filepath.Join(gp, src)):]), projectName)
240-
// strip any "/" prefix from the repo path.
241-
return strings.TrimPrefix(rp, string(filepath.Separator))
242-
}
243-
return wd
244-
}
245-
246222
func verifyFlags() {
247223
if operatorType != projutil.OperatorTypeGo && operatorType != projutil.OperatorTypeAnsible {
248224
log.Fatal("--type can only be `go` or `ansible`")

internal/util/projutil/project_util.go

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,12 @@ func MustGetwd() string {
7171
return wd
7272
}
7373

74-
// CheckAndGetCurrPkg checks if this project's repository path is rooted under $GOPATH and returns the current directory's import path
74+
// CheckAndGetProjectGoPkg checks if this project's repository path is rooted under $GOPATH and returns the current directory's import path
7575
// e.g: "github.com/example-inc/app-operator"
76-
func CheckAndGetCurrPkg() string {
77-
gopath := os.Getenv(GopathEnv)
78-
if len(gopath) == 0 {
79-
log.Fatalf("get current pkg failed: GOPATH env not set")
80-
}
81-
var goSrc string
82-
cwdInGopath := false
76+
func CheckAndGetProjectGoPkg() string {
77+
gopath := SetGopath(GetGopath())
78+
goSrc := filepath.Join(gopath, SrcDir)
8379
wd := MustGetwd()
84-
for _, path := range strings.Split(gopath, ":") {
85-
goSrc = filepath.Join(path, SrcDir)
86-
87-
if strings.HasPrefix(filepath.Dir(wd), goSrc) {
88-
cwdInGopath = true
89-
break
90-
}
91-
}
92-
if !cwdInGopath {
93-
log.Fatalf("check current pkg failed: must run from gopath")
94-
}
9580
currPkg := strings.Replace(wd, goSrc+string(filepath.Separator), "", 1)
9681
// strip any "/" prefix from the repo path.
9782
return strings.TrimPrefix(currPkg, string(filepath.Separator))
@@ -108,3 +93,35 @@ func GetOperatorType() OperatorType {
10893
}
10994
return OperatorTypeGo
11095
}
96+
97+
// GetGopath gets GOPATH and makes sure it is set and non-empty.
98+
func GetGopath() string {
99+
gopath, ok := os.LookupEnv(GopathEnv)
100+
if !ok || len(gopath) == 0 {
101+
log.Fatal("GOPATH env not set")
102+
}
103+
return gopath
104+
}
105+
106+
// SetGopath sets GOPATH=currentGopath after processing a path list,
107+
// if any, then returns the set path.
108+
func SetGopath(currentGopath string) string {
109+
var newGopath string
110+
cwdInGopath := false
111+
wd := MustGetwd()
112+
for _, newGopath = range strings.Split(currentGopath, ":") {
113+
if strings.HasPrefix(filepath.Dir(wd), newGopath) {
114+
cwdInGopath = true
115+
break
116+
}
117+
}
118+
if !cwdInGopath {
119+
log.Fatalf("project not in $GOPATH")
120+
return ""
121+
}
122+
if err := os.Setenv(GopathEnv, newGopath); err != nil {
123+
log.Fatal(err)
124+
return ""
125+
}
126+
return newGopath
127+
}

test/e2e/memcached_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2829
"github.com/operator-framework/operator-sdk/test/e2e/e2eutil"
2930
framework "github.com/operator-framework/operator-sdk/test/e2e/framework"
3031

@@ -42,7 +43,7 @@ func TestMemcached(t *testing.T) {
4243
f := framework.Global
4344
ctx := f.NewTestCtx(t)
4445
defer ctx.Cleanup(t)
45-
gopath, ok := os.LookupEnv("GOPATH")
46+
gopath, ok := os.LookupEnv(projutil.GopathEnv)
4647
if !ok {
4748
t.Fatalf("$GOPATH not set")
4849
}

0 commit comments

Comments
 (0)