Skip to content
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

Exit if not a Go project #672

Merged
merged 1 commit into from
Nov 7, 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
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 @@ -82,8 +93,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