Skip to content

Commit b3885b9

Browse files
author
Shawn Hurley
authored
Revert "decompose GOPATH and set it according to the current working dir." (#721)
* Revert "decompose GOPATH and set it according to the current working dir. (#712)" This reverts commit 48ba936.
1 parent bd6ea65 commit b3885b9

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

commands/operator-sdk/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,9 @@ 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"
2322
)
2423

2524
func main() {
26-
gopath := projutil.GetGopath()
27-
projutil.SetGopath(gopath)
28-
2925
if err := cmd.NewRootCmd().Execute(); err != nil {
3026
fmt.Fprintln(os.Stderr, err)
3127
os.Exit(-1)

internal/util/projutil/project_util.go

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,23 @@ func MustGetwd() string {
7575
// e.g: "github.com/example-inc/app-operator"
7676
func CheckAndGetCurrPkg() string {
7777
gopath := os.Getenv(GopathEnv)
78-
goSrc := filepath.Join(gopath, SrcDir)
78+
if len(gopath) == 0 {
79+
log.Fatalf("get current pkg failed: GOPATH env not set")
80+
}
81+
var goSrc string
82+
cwdInGopath := false
7983
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+
}
8095
currPkg := strings.Replace(wd, goSrc+string(filepath.Separator), "", 1)
8196
// strip any "/" prefix from the repo path.
8297
return strings.TrimPrefix(currPkg, string(filepath.Separator))
@@ -93,27 +108,3 @@ func GetOperatorType() OperatorType {
93108
}
94109
return OperatorTypeGo
95110
}
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)