From 13e5bcf908fa5b0d96caa98988b9f6d54c13b00e Mon Sep 17 00:00:00 2001 From: kodejuice Date: Sat, 27 Feb 2021 06:54:38 +0100 Subject: [PATCH] check if command exists before attempting to create --- cobra/cmd/project.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cobra/cmd/project.go b/cobra/cmd/project.go index bd68a31d..a01b4962 100644 --- a/cobra/cmd/project.go +++ b/cobra/cmd/project.go @@ -83,7 +83,14 @@ func (p *Project) createLicenseFile() error { } func (c *Command) Create() error { - cmdFile, err := os.Create(fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName)) + fileName := fmt.Sprintf("%s/cmd/%s.go", c.AbsolutePath, c.CmdName) + + // check if Command exists + if _, err := os.Stat(fileName); err == nil { + return fmt.Errorf("command '%s' already exists", c.CmdName) + } + + cmdFile, err := os.Create(fileName) if err != nil { return err }