Skip to content

Commit 62302fd

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 62302fd

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

commands/operator-sdk/cmd/add/api.go

Lines changed: 3 additions & 0 deletions
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(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

Lines changed: 3 additions & 0 deletions
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(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

Lines changed: 5 additions & 0 deletions
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.MustGoProject(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

Lines changed: 18 additions & 3 deletions
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,18 @@ func MustInProjectRoot() {
5255
}
5356
}
5457

58+
func MustGoProject(cmd *cobra.Command) {
59+
t := GetOperatorType()
60+
switch t {
61+
case OperatorTypeGo:
62+
default:
63+
s := fmt.Sprintf("'%s' is meant for Go projects.\n", cmd.CommandPath())
64+
s += fmt.Sprintf("Migrate your project to a hybrid structure before running this command for %s operators.\n", strings.Title(t))
65+
fmt.Fprintf(os.Stderr, s)
66+
os.Exit(1)
67+
}
68+
}
69+
5570
func MustGetwd() string {
5671
wd, err := os.Getwd()
5772
if err != nil {
@@ -82,8 +97,8 @@ func CheckAndGetCurrPkg() string {
8297
// This function should be called after verifying the user is in project root
8398
// e.g: "go", "ansible"
8499
func GetOperatorType() OperatorType {
85-
// Assuming that if Gopkg.toml exists then this is a Go operator
86-
_, err := os.Stat(gopkgToml)
100+
// Assuming that if main.go exists then this is a Go operator
101+
_, err := os.Stat(mainFile)
87102
if err != nil && os.IsNotExist(err) {
88103
return OperatorTypeAnsible
89104
}

0 commit comments

Comments
 (0)