-
Notifications
You must be signed in to change notification settings - Fork 39
/
config.go
89 lines (79 loc) · 2.02 KB
/
config.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
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/stratosnet/sds/cmd/common"
"github.com/stratosnet/sds/pp/setting"
"github.com/stratosnet/sds/utils"
)
const (
createP2pKeyFlag = "create-p2p-key"
createWalletFlag = "create-wallet"
)
func genConfig(cmd *cobra.Command, _ []string) error {
path, err := cmd.Flags().GetString(common.Config)
if err != nil {
return errors.Wrap(err, "failed to get the configuration file path")
}
if path == common.DefaultConfigPath {
home, err := cmd.Flags().GetString(common.Home)
if err != nil {
return err
}
path = filepath.Join(home, path)
}
if _, err = os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(path), 0700)
}
if err != nil {
return err
}
err = setting.LoadConfig(path)
if err != nil {
fmt.Println("generating default config file")
err = setting.GenDefaultConfig()
if err != nil {
return errors.Wrap(err, "failed to generate config file at given path")
}
if err = setting.LoadConfig(path); err != nil {
return err
}
}
createP2pKey, err := cmd.Flags().GetBool(createP2pKeyFlag)
if err == nil && createP2pKey {
err = common.SetupP2PKey()
if err != nil {
err := errors.Wrap(err, "Couldn't setup PP node")
utils.ErrorLog(err)
return err
}
}
createWallet, err := cmd.Flags().GetBool(createWalletFlag)
if err == nil && createWallet {
err = SetupWalletKey()
if err != nil {
utils.ErrorLog(err)
return err
}
}
return nil
}
func SetupWalletKey() error {
if setting.Config.Keys.WalletAddress == "" {
fmt.Println("No wallet key specified in config. Attempting to create one...")
err := utils.SetupWallet(setting.Config.Home.AccountsPath, setting.HDPath, updateWalletConfig)
if err != nil {
utils.ErrorLog(err)
return err
}
}
return nil
}
func updateWalletConfig(walletKeyAddressString, password string) {
setting.Config.Keys.WalletAddress = walletKeyAddressString
setting.Config.Keys.WalletPassword = password
_ = setting.FlushConfig()
}