Skip to content

Commit

Permalink
internal/util/projutil: add function to exit if not a Go project
Browse files Browse the repository at this point in the history
commands/operator-sdk/{add,generate}: exit if not a Go project for
'add {api,controller}', 'generate k8s' sub-commands
  • Loading branch information
estroz committed Nov 5, 2018
1 parent ab09a3d commit a8cbcf6
Show file tree
Hide file tree
Showing 4 changed files with 27 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
19 changes: 16 additions & 3 deletions internal/util/projutil/project_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
package projutil

import (
"fmt"
"log"
"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 +55,16 @@ func MustInProjectRoot() {
}
}

func MustGoProjectCmd(cmd *cobra.Command) {
t := GetOperatorType()
switch t {
case OperatorTypeGo:
default:
fmt.Fprintf(os.Stderr, "'%s' is meant for Go projects.\n", cmd.CommandPath())
os.Exit(1)
}
}

func MustGetwd() string {
wd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -82,8 +95,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 a8cbcf6

Please sign in to comment.