From f16d971002706969d67f4fa3653dc73523756a24 Mon Sep 17 00:00:00 2001 From: srexrg Date: Thu, 27 Nov 2025 19:57:34 +0530 Subject: [PATCH] Prompt before overwriting main.go during init --- cmd/project.go | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/cmd/project.go b/cmd/project.go index ec8980e..84c735c 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -1,8 +1,10 @@ package cmd import ( + "bufio" "fmt" "os" + "strings" "text/template" "github.com/spf13/cobra" @@ -36,7 +38,15 @@ func (p *Project) Create() error { } // create main.go - mainFile, err := os.Create(fmt.Sprintf("%s/main.go", p.AbsolutePath)) + mainPath := fmt.Sprintf("%s/main.go", p.AbsolutePath) + ok, err := confirmMainOverwrite(mainPath) + if err != nil { + return err + } + if !ok { + return fmt.Errorf("aborted: %s already exists", mainPath) + } + mainFile, err := os.Create(mainPath) if err != nil { return err } @@ -68,6 +78,24 @@ func (p *Project) Create() error { return p.createLicenseFile() } +func confirmMainOverwrite(path string) (bool, error) { + if _, err := os.Stat(path); err != nil { + if os.IsNotExist(err) { + return true, nil + } + return false, err + } + + fmt.Printf("%s already exists. Overwrite? [y/N]: ", path) + reader := bufio.NewReader(os.Stdin) + response, err := reader.ReadString('\n') + if err != nil { + return false, err + } + response = strings.TrimSpace(strings.ToLower(response)) + return response == "y" || response == "yes", nil +} + func (p *Project) createLicenseFile() error { data := map[string]interface{}{ "copyright": copyrightLine(),