Skip to content

Commit a8cbcf6

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 a8cbcf6

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

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)

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)

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")

internal/util/projutil/project_util.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
package projutil
1616

1717
import (
18+
"fmt"
1819
"log"
1920
"os"
2021
"path/filepath"
2122
"strings"
23+
24+
"github.com/spf13/cobra"
2225
)
2326

2427
const (
2528
SrcDir = "src"
26-
gopkgToml = "./Gopkg.toml"
29+
mainFile = "./cmd/manager/main.go"
2730
buildDockerfile = "./build/Dockerfile"
2831
)
2932

@@ -52,6 +55,16 @@ func MustInProjectRoot() {
5255
}
5356
}
5457

58+
func MustGoProjectCmd(cmd *cobra.Command) {
59+
t := GetOperatorType()
60+
switch t {
61+
case OperatorTypeGo:
62+
default:
63+
fmt.Fprintf(os.Stderr, "'%s' is meant for Go projects.\n", cmd.CommandPath())
64+
os.Exit(1)
65+
}
66+
}
67+
5568
func MustGetwd() string {
5669
wd, err := os.Getwd()
5770
if err != nil {
@@ -82,8 +95,8 @@ func CheckAndGetCurrPkg() string {
8295
// This function should be called after verifying the user is in project root
8396
// e.g: "go", "ansible"
8497
func GetOperatorType() OperatorType {
85-
// Assuming that if Gopkg.toml exists then this is a Go operator
86-
_, err := os.Stat(gopkgToml)
98+
// Assuming that if main.go exists then this is a Go operator
99+
_, err := os.Stat(mainFile)
87100
if err != nil && os.IsNotExist(err) {
88101
return OperatorTypeAnsible
89102
}

0 commit comments

Comments
 (0)