From b81a9bc7a09baa8f699241fa959e466849b9b453 Mon Sep 17 00:00:00 2001 From: alpharush <0xalpharush@protonmail.com> Date: Thu, 15 Aug 2024 12:34:42 -0500 Subject: [PATCH] feat: prompt user for overwrite if file already exists (#444) * feat: prompt user for overwrite if file already exists Fixes #375 --------- Co-authored-by: to4to --- cmd/init.go | 20 +++++++++++++++++++- go.mod | 1 + go.sum | 4 ++-- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 4f845517..35cb918b 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -2,11 +2,12 @@ package cmd import ( "fmt" - "github.com/crytic/medusa/logging/colors" "os" "path/filepath" "strings" + "github.com/crytic/medusa/logging/colors" + "github.com/crytic/medusa/compilation" "github.com/crytic/medusa/fuzzing/config" "github.com/spf13/cobra" @@ -136,6 +137,23 @@ func cmdRunInit(cmd *cobra.Command, args []string) error { return err } + if _, err = os.Stat(outputPath); err == nil { + // Prompt user for overwrite confirmation + fmt.Print("The file already exists. Overwrite? (y/n): ") + var response string + if _, err := fmt.Scan(&response); err != nil { + // Handle the error (e.g., log it, return an error) + cmdLogger.Error("Failed to scan input", err) + return err + } + + if response != "y" && response != "Y" { + fmt.Println("Operation canceled.") + return nil + } + + } + // Write our project configuration err = projectConfig.WriteToFile(outputPath) if err != nil { diff --git a/go.mod b/go.mod index 05b14e8a..f5a85d40 100644 --- a/go.mod +++ b/go.mod @@ -64,6 +64,7 @@ require ( github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect + github.com/onsi/gomega v1.33.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect diff --git a/go.sum b/go.sum index d20fe464..d01119b9 100644 --- a/go.sum +++ b/go.sum @@ -150,8 +150,9 @@ github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108 github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= -github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk= +github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= @@ -262,7 +263,6 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=