Skip to content

Commit 479e886

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 479e886

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
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.MustGoProject("add api")
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.MustGoProject("add controller")
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

+3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ func k8sFunc(cmd *cobra.Command, args []string) {
4848

4949
// K8sCodegen performs deepcopy code-generation for all custom resources under pkg/apis
5050
func K8sCodegen() {
51+
// Only Go projects can generate k8s deepcopy code.
52+
projutil.MustGoProject("generate k8s")
53+
5154
projutil.MustInProjectRoot()
5255
repoPkg := projutil.CheckAndGetCurrPkg()
5356
outputPkg := filepath.Join(repoPkg, "pkg/generated")

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

+7
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func MustInProjectRoot() {
5252
}
5353
}
5454

55+
// MustGoProject checks if the current project is a Go project, and exits if not.
56+
func MustGoProject(cmd string) {
57+
if GetOperatorType() != OperatorTypeGo {
58+
log.Fatalf("'operator-sdk %s' can only be run for Go projects", cmd)
59+
}
60+
}
61+
5562
func MustGetwd() string {
5663
wd, err := os.Getwd()
5764
if err != nil {

0 commit comments

Comments
 (0)