Skip to content

Commit 2e54ddc

Browse files
committed
internal/util/projutil: add function to exit if not a Go project
commands/operator-sdk/{add,generate}: exit if not a Go project for 'add {api,controller}', 'generate k8s' sub-commands
1 parent ab09a3d commit 2e54ddc

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

Diff for: commands/operator-sdk/cmd/add/api.go

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ Example:
6363
}
6464

6565
func apiRun(cmd *cobra.Command, args []string) {
66+
// Only Go projects can add apis.
67+
projutil.MustGoProjectCmd(cmd)
68+
6669
// Create and validate new resource
6770
projutil.MustInProjectRoot()
6871
r, err := scaffold.NewResource(apiVersion, kind)

Diff for: commands/operator-sdk/cmd/add/controller.go

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ Example:
5757
}
5858

5959
func controllerRun(cmd *cobra.Command, args []string) {
60+
// Only Go projects can add controllers.
61+
projutil.MustGoProjectCmd(cmd)
62+
6063
projutil.MustInProjectRoot()
6164
// Create and validate new resource
6265
r, err := scaffold.NewResource(apiVersion, kind)

Diff for: commands/operator-sdk/cmd/generate/k8s.go

+5
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ func k8sFunc(cmd *cobra.Command, args []string) {
4343
if len(args) != 0 {
4444
log.Fatalf("k8s command doesn't accept any arguments.")
4545
}
46+
47+
// Only Go projects can generate k8s deepcopy code.
48+
projutil.MustGoProjectCmd(cmd)
49+
4650
K8sCodegen()
4751
}
4852

4953
// K8sCodegen performs deepcopy code-generation for all custom resources under pkg/apis
5054
func K8sCodegen() {
55+
5156
projutil.MustInProjectRoot()
5257
repoPkg := projutil.CheckAndGetCurrPkg()
5358
outputPkg := filepath.Join(repoPkg, "pkg/generated")

Diff for: internal/util/projutil/project_util.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import (
1919
"os"
2020
"path/filepath"
2121
"strings"
22+
23+
"github.com/spf13/cobra"
2224
)
2325

2426
const (
2527
SrcDir = "src"
26-
gopkgToml = "./Gopkg.toml"
28+
mainFile = "./cmd/manager/main.go"
2729
buildDockerfile = "./build/Dockerfile"
2830
)
2931

@@ -52,6 +54,15 @@ func MustInProjectRoot() {
5254
}
5355
}
5456

57+
func MustGoProjectCmd(cmd *cobra.Command) {
58+
t := GetOperatorType()
59+
switch t {
60+
case OperatorTypeGo:
61+
default:
62+
log.Fatalf("'%s' can only be run for Go operators.", cmd.CommandPath())
63+
}
64+
}
65+
5566
func MustGetwd() string {
5667
wd, err := os.Getwd()
5768
if err != nil {
@@ -82,8 +93,8 @@ func CheckAndGetCurrPkg() string {
8293
// This function should be called after verifying the user is in project root
8394
// e.g: "go", "ansible"
8495
func GetOperatorType() OperatorType {
85-
// Assuming that if Gopkg.toml exists then this is a Go operator
86-
_, err := os.Stat(gopkgToml)
96+
// Assuming that if main.go exists then this is a Go operator
97+
_, err := os.Stat(mainFile)
8798
if err != nil && os.IsNotExist(err) {
8899
return OperatorTypeAnsible
89100
}

0 commit comments

Comments
 (0)