commands/operator-sdk: check existing project
@@ -59,6 +59,7 @@ func newFunc(cmd *cobra.Command, args []string) {
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("new command needs 1 argument."))
}
parse(args)
+ mustBeNewProject()
verifyFlags()
g := generator.NewGenerator(apiVersion, kind, projectName, repoPath())
err := g.Render()
@@ -75,6 +76,22 @@ func parse(args []string) {
}
}
+// mustBeNewProject checks if the given project exists under the current diretory.
+// it exits with error when the project exists.
+func mustBeNewProject() {
+ fp := filepath.Join(mustGetwd(), projectName)
+ stat, err := os.Stat(fp)
+ if err != nil && os.IsNotExist(err) {
+ return
+ }
+ if err != nil {
+ cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine if project (%v) exists", projectName))
+ }
+ if stat.IsDir() {
+ cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("project (%v) exists. please use a different project name or delete the existing one", projectName))
+ }
+}
+
// repoPath checks if this project's repository path is rooted under $GOPATH and returns project's repository path.
func repoPath() string {
gp := os.Getenv(gopath)
