Skip to content

Commit 48ba936

Browse files
nrobert13estroz
authored andcommitted
decompose GOPATH and set it according to the current working dir. (#712)
* decompose GOPATH and set it according to the current working dir. there are different places in the project where the value of env var GOPATH is used, and cannot handle composed GOPATHs. this will determine the correct GOPATH based on the current workin dir and set it during the execution. * get rid of restoreGopath and move the GOPATH check in GetGopath restoreGopath is not required, as SetEnv doesn't affect the parent process's enviroment
1 parent 25c0819 commit 48ba936

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

commands/operator-sdk/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ import (
1919
"os"
2020

2121
"github.com/operator-framework/operator-sdk/commands/operator-sdk/cmd"
22+
"github.com/operator-framework/operator-sdk/internal/util/projutil"
2223
)
2324

2425
func main() {
26+
gopath := projutil.GetGopath()
27+
projutil.SetGopath(gopath)
28+
2529
if err := cmd.NewRootCmd().Execute(); err != nil {
2630
fmt.Fprintln(os.Stderr, err)
2731
os.Exit(-1)

internal/util/projutil/project_util.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,8 @@ func MustGetwd() string {
7575
// e.g: "github.com/example-inc/app-operator"
7676
func CheckAndGetCurrPkg() string {
7777
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
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,27 @@ func GetOperatorType() OperatorType {
10893
}
10994
return OperatorTypeGo
11095
}
96+
97+
func GetGopath() string {
98+
gopath, ok := os.LookupEnv(GopathEnv)
99+
if !ok || len(gopath) == 0 {
100+
log.Fatal("get current pkg failed: GOPATH env not set")
101+
}
102+
return gopath
103+
}
104+
105+
func SetGopath(currentGopath string) {
106+
var newGopath string
107+
cwdInGopath := false
108+
wd := MustGetwd()
109+
for _, newGopath = range strings.Split(currentGopath, ":") {
110+
if strings.HasPrefix(filepath.Dir(wd), newGopath) {
111+
cwdInGopath = true
112+
break
113+
}
114+
}
115+
if !cwdInGopath {
116+
log.Fatalf("check current pkg failed: must run from gopath")
117+
}
118+
os.Setenv(GopathEnv, newGopath)
119+
}

0 commit comments

Comments
 (0)