-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
79 lines (70 loc) · 1.87 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package utils
import (
"os"
"os/signal"
"os/user"
"path/filepath"
"runtime"
"gopkg.in/urfave/cli.v1"
ethUtils "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/log"
"github.com/blockservice/echoin/vm/ethereum"
)
// StartNode will start up the node.
func StartNode(stack *ethereum.Node) {
if err := stack.Start(); err != nil {
ethUtils.Fatalf("Error starting protocol stack: %v", err)
}
go func() {
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt)
defer signal.Stop(sigc)
<-sigc
log.Info("Got interrupt, shutting down...")
go stack.Stop() // nolint: errcheck
for i := 10; i > 0; i-- {
<-sigc
if i > 1 {
log.Warn("Already shutting down, interrupt more to panic.", "times", i-1)
}
}
}()
}
// HomeDir returns the user's home most likely home directory
// #unstable
func HomeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}
// DefaultDataDir tries to guess the default directory for ethermint data
// #unstable
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := HomeDir()
if home != "" {
if runtime.GOOS == "darwin" {
return filepath.Join(home, "Library", "Ethermint")
} else if runtime.GOOS == "windows" {
return filepath.Join(home, "AppData", "Roaming", "Ethermint")
} else {
return filepath.Join(home, ".ethermint")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}
// ResetAll will remove the data directory.
func ResetAll(ctx *cli.Context) error {
dbDir := filepath.Join(MakeDataDir(ctx), "ethermint")
if err := os.RemoveAll(dbDir); err != nil {
log.Debug("Could not reset ethermint. Failed to remove %+v", dbDir)
return err
}
log.Info("Successfully removed all data", "dir", dbDir)
return nil
}