Skip to content

Commit

Permalink
Merge pull request #672 from estroz/fix-670
Browse files Browse the repository at this point in the history
Exit if not a Go project
  • Loading branch information
estroz committed Nov 7, 2018
2 parents fc9b6b1 + 2e54ddc commit ef664ed
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions commands/operator-sdk/cmd/add/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Example:
}

func apiRun(cmd *cobra.Command, args []string) {
// Only Go projects can add apis.
projutil.MustGoProjectCmd(cmd)

// Create and validate new resource
projutil.MustInProjectRoot()
r, err := scaffold.NewResource(apiVersion, kind)
Expand Down
3 changes: 3 additions & 0 deletions commands/operator-sdk/cmd/add/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Example:
}

func controllerRun(cmd *cobra.Command, args []string) {
// Only Go projects can add controllers.
projutil.MustGoProjectCmd(cmd)

projutil.MustInProjectRoot()
// Create and validate new resource
r, err := scaffold.NewResource(apiVersion, kind)
Expand Down
5 changes: 5 additions & 0 deletions commands/operator-sdk/cmd/generate/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,16 @@ func k8sFunc(cmd *cobra.Command, args []string) {
if len(args) != 0 {
log.Fatalf("k8s command doesn't accept any arguments.")
}

// Only Go projects can generate k8s deepcopy code.
projutil.MustGoProjectCmd(cmd)

K8sCodegen()
}

// K8sCodegen performs deepcopy code-generation for all custom resources under pkg/apis
func K8sCodegen() {

projutil.MustInProjectRoot()
repoPkg := projutil.CheckAndGetCurrPkg()
outputPkg := filepath.Join(repoPkg, "pkg/generated")
Expand Down
17 changes: 14 additions & 3 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import (
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
)

const (
SrcDir = "src"
gopkgToml = "./Gopkg.toml"
mainFile = "./cmd/manager/main.go"
buildDockerfile = "./build/Dockerfile"
)

Expand Down Expand Up @@ -52,6 +54,15 @@ func MustInProjectRoot() {
}
}

func MustGoProjectCmd(cmd *cobra.Command) {
t := GetOperatorType()
switch t {
case OperatorTypeGo:
default:
log.Fatalf("'%s' can only be run for Go operators.", cmd.CommandPath())
}
}

func MustGetwd() string {
wd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -90,8 +101,8 @@ func CheckAndGetCurrPkg() string {
// This function should be called after verifying the user is in project root
// e.g: "go", "ansible"
func GetOperatorType() OperatorType {
// Assuming that if Gopkg.toml exists then this is a Go operator
_, err := os.Stat(gopkgToml)
// Assuming that if main.go exists then this is a Go operator
_, err := os.Stat(mainFile)
if err != nil && os.IsNotExist(err) {
return OperatorTypeAnsible
}
Expand Down

0 comments on commit ef664ed

Please sign in to comment.