Skip to content

Commit ac6ba96

Browse files
ggarriEnchanterIO
authored andcommitted
#98 Adding confirmation before removing folder
1 parent a704199 commit ac6ba96

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

cmd/lightchain/init.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,19 @@ func newNodeCfgFromCmd(cmd *cobra.Command) (node.Config, network.Network, error)
9999
return node.Config{}, "", fmt.Errorf("unable to initialize lightchain node. %s", err.Error())
100100
}
101101

102-
if forceInit {
103-
logger.Info(fmt.Sprintf("Forcing '%s' data dir removal...", dataDir))
104-
if err := fs.RemoveAll(dataDir); err != nil {
105-
return node.Config{}, "", fmt.Errorf("unable to remove data dir '%s'. %s", dataDir, err)
106-
}
107-
} else {
102+
if !forceInit {
108103
return node.Config{}, "", fmt.Errorf("unable to initialize lightchain node. %s already exists", dataDir)
109104
}
105+
106+
isConfirmed := fs.AskForConfirmation(fmt.Sprintf("Are you sure to erase %s ?", dataDir))
107+
if !isConfirmed {
108+
return node.Config{}, "", fmt.Errorf("unable to initialize lightchain node. %s already exists", dataDir)
109+
}
110+
111+
logger.Info(fmt.Sprintf("Removing '%s' folder ...", dataDir))
112+
if err := fs.RemoveAll(dataDir); err != nil {
113+
return node.Config{}, "", fmt.Errorf("unable to remove data dir '%s'. %s", dataDir, err)
114+
}
110115
}
111116

112117

fs/io.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fs
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"log"
7+
"os"
8+
"strings"
9+
)
10+
11+
func AskForConfirmation(s string) bool {
12+
reader := bufio.NewReader(os.Stdin)
13+
14+
for {
15+
fmt.Printf("%s [y(yes)/n(no)]: ", s)
16+
17+
response, err := reader.ReadString('\n')
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
response = strings.ToLower(strings.TrimSpace(response))
23+
24+
if response == "y" || response == "yes" {
25+
return true
26+
} else if response == "n" || response == "no" {
27+
return false
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)