This is an automated email from the ASF dual-hosted git repository.

msciabarra pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/openserverless-cli.git

commit f45f2027d3fbc220ec9cbe32ff5d40e30f87ac9a
Author: Michele Sciabarra <[email protected]>
AuthorDate: Mon Aug 5 14:23:13 2024 +0200

    add: -reset tool
---
 common.go | 23 +++++++++++++++++++++++
 main.go   | 27 +++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/common.go b/common.go
index 9f3481c..607b199 100644
--- a/common.go
+++ b/common.go
@@ -18,6 +18,7 @@
 package openserverless
 
 import (
+       "bufio"
        "encoding/json"
        "fmt"
        "log"
@@ -199,3 +200,25 @@ func touch(dir string, name string) error {
        f.Close()
        return nil
 }
+
+// confirm prompts the user for a yes/no answer and returns true if the answer 
is "yes".
+func confirm(prompt string) bool {
+       reader := bufio.NewReader(os.Stdin)
+       for {
+               fmt.Print(prompt)
+               response, err := reader.ReadString('\n')
+               if err != nil {
+                       fmt.Printf("Error reading input: %v\n", err)
+                       return false
+               }
+
+               response = strings.TrimSpace(strings.ToLower(response))
+               if response == "yes" || response == "y" {
+                       return true
+               } else if response == "no" || response == "n" {
+                       return false
+               }
+
+               fmt.Println("Please type yes or no and then press enter.")
+       }
+}
diff --git a/main.go b/main.go
index b8d8e29..d198667 100644
--- a/main.go
+++ b/main.go
@@ -148,6 +148,33 @@ func executeMainToolsAndExit(cmd string, args []string, 
opsHome string) int {
                        log.Fatal("unable to set OPS_OLARIS...", err.Error())
                }
 
+       case "reset":
+               home := os.Getenv("OPS_HOME")
+               if home == "" {
+                       log.Fatal("cannot determine the ops home dir")
+                       return 1
+               }
+               info, err := os.Stat(home)
+               if os.IsNotExist(err) {
+                       fmt.Printf("%s does not exists - nothing to to do\n", 
home)
+                       return 1
+               }
+               if err != nil {
+                       log.Fatal("error in reading the ops home dir", 
err.Error())
+               }
+               if !info.IsDir() {
+                       log.Fatal("cannot reset, not a directory", home)
+               }
+               if !confirm(fmt.Sprintf("I am going to remove the subfolder %s, 
are you sure", home)) {
+                       log.Fatal("reset aborted")
+               }
+               err = os.RemoveAll(home)
+               if err != nil {
+                       log.Fatal("ops reset error:", err.Error())
+               }
+               fmt.Println("ops reset complete")
+               return 0
+
        case "retry":
                if err := tools.ExpBackoffRetry(args[1:]); err != nil {
                        log.Fatalf("error: %s", err.Error())

Reply via email to