Skip to content

commands/operator-sdk/cmd: set composed GOPATH for Go projects (fix #712) #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion commands/operator-sdk/cmd/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func apiRun(cmd *cobra.Command, args []string) {
absProjectPath := projutil.MustGetwd()

cfg := &input.Config{
Repo: projutil.CheckAndGetCurrPkg(),
Repo: projutil.CheckAndGetProjectGoPkg(),
AbsProjectPath: absProjectPath,
}

Expand Down
2 changes: 1 addition & 1 deletion commands/operator-sdk/cmd/add/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func controllerRun(cmd *cobra.Command, args []string) {
}

cfg := &input.Config{
Repo: projutil.CheckAndGetCurrPkg(),
Repo: projutil.CheckAndGetProjectGoPkg(),
AbsProjectPath: projutil.MustGetwd(),
}

Expand Down
4 changes: 2 additions & 2 deletions commands/operator-sdk/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func buildFunc(cmd *cobra.Command, args []string) {

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

absProjectPath := projutil.MustGetwd()
cfg := &input.Config{
Repo: projutil.CheckAndGetCurrPkg(),
Repo: projutil.CheckAndGetProjectGoPkg(),
AbsProjectPath: absProjectPath,
ProjectName: filepath.Base(wd),
}
Expand Down
2 changes: 1 addition & 1 deletion commands/operator-sdk/cmd/generate/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func k8sFunc(cmd *cobra.Command, args []string) {
func K8sCodegen() {

projutil.MustInProjectRoot()
repoPkg := projutil.CheckAndGetCurrPkg()
repoPkg := projutil.CheckAndGetProjectGoPkg()
outputPkg := filepath.Join(repoPkg, "pkg/generated")
apisPkg := filepath.Join(repoPkg, scaffold.ApisDir)
groupVersions, err := parseGroupVersions()
Expand Down
26 changes: 1 addition & 25 deletions commands/operator-sdk/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ var (
)

const (
gopath = "GOPATH"
src = "src"
dep = "dep"
ensureCmd = "ensure"
)
Expand Down Expand Up @@ -120,7 +118,7 @@ func mustBeNewProject() {

func doScaffold() {
cfg := &input.Config{
Repo: filepath.Join(projutil.CheckAndGetCurrPkg(), projectName),
Repo: filepath.Join(projutil.CheckAndGetProjectGoPkg(), projectName),
AbsProjectPath: filepath.Join(projutil.MustGetwd(), projectName),
ProjectName: projectName,
}
Expand Down Expand Up @@ -221,28 +219,6 @@ func doAnsibleScaffold() {
}
}

// repoPath checks if this project's repository path is rooted under $GOPATH and returns project's repository path.
// repoPath field on generator is used primarily in generation of Go operator. For Ansible we will set it to cwd
func repoPath() string {
// We only care about GOPATH constraint checks if we are a Go operator
wd := projutil.MustGetwd()
if operatorType == projutil.OperatorTypeGo {
gp := os.Getenv(gopath)
if len(gp) == 0 {
log.Fatal("$GOPATH env not set")
}
// check if this project's repository path is rooted under $GOPATH
if !strings.HasPrefix(wd, gp) {
log.Fatalf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp)
}
// compute the repo path by stripping "$GOPATH/src/" from the path of the current directory.
rp := filepath.Join(string(wd[len(filepath.Join(gp, src)):]), projectName)
// strip any "/" prefix from the repo path.
return strings.TrimPrefix(rp, string(filepath.Separator))
}
return wd
}

func verifyFlags() {
if operatorType != projutil.OperatorTypeGo && operatorType != projutil.OperatorTypeAnsible {
log.Fatal("--type can only be `go` or `ansible`")
Expand Down
55 changes: 36 additions & 19 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,12 @@ func MustGetwd() string {
return wd
}

// CheckAndGetCurrPkg checks if this project's repository path is rooted under $GOPATH and returns the current directory's import path
// CheckAndGetProjectGoPkg checks if this project's repository path is rooted under $GOPATH and returns the current directory's import path
// e.g: "github.com/example-inc/app-operator"
func CheckAndGetCurrPkg() string {
gopath := os.Getenv(GopathEnv)
if len(gopath) == 0 {
log.Fatalf("get current pkg failed: GOPATH env not set")
}
var goSrc string
cwdInGopath := false
func CheckAndGetProjectGoPkg() string {
gopath := SetGopath(GetGopath())
goSrc := filepath.Join(gopath, SrcDir)
wd := MustGetwd()
for _, path := range strings.Split(gopath, ":") {
goSrc = filepath.Join(path, SrcDir)

if strings.HasPrefix(filepath.Dir(wd), goSrc) {
cwdInGopath = true
break
}
}
if !cwdInGopath {
log.Fatalf("check current pkg failed: must run from gopath")
}
currPkg := strings.Replace(wd, goSrc+string(filepath.Separator), "", 1)
// strip any "/" prefix from the repo path.
return strings.TrimPrefix(currPkg, string(filepath.Separator))
Expand All @@ -108,3 +93,35 @@ func GetOperatorType() OperatorType {
}
return OperatorTypeGo
}

// GetGopath gets GOPATH and makes sure it is set and non-empty.
func GetGopath() string {
gopath, ok := os.LookupEnv(GopathEnv)
if !ok || len(gopath) == 0 {
log.Fatal("GOPATH env not set")
}
return gopath
}

// SetGopath sets GOPATH=currentGopath after processing a path list,
// if any, then returns the set path.
func SetGopath(currentGopath string) string {
var newGopath string
cwdInGopath := false
wd := MustGetwd()
for _, newGopath = range strings.Split(currentGopath, ":") {
if strings.HasPrefix(filepath.Dir(wd), newGopath) {
cwdInGopath = true
break
}
}
if !cwdInGopath {
log.Fatalf("project not in $GOPATH")
return ""
}
if err := os.Setenv(GopathEnv, newGopath); err != nil {
log.Fatal(err)
return ""
}
return newGopath
}
3 changes: 2 additions & 1 deletion test/e2e/memcached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"
"time"

"github.com/operator-framework/operator-sdk/internal/util/projutil"
"github.com/operator-framework/operator-sdk/test/e2e/e2eutil"
framework "github.com/operator-framework/operator-sdk/test/e2e/framework"

Expand All @@ -42,7 +43,7 @@ func TestMemcached(t *testing.T) {
f := framework.Global
ctx := f.NewTestCtx(t)
defer ctx.Cleanup(t)
gopath, ok := os.LookupEnv("GOPATH")
gopath, ok := os.LookupEnv(projutil.GopathEnv)
if !ok {
t.Fatalf("$GOPATH not set")
}
Expand Down