-
Notifications
You must be signed in to change notification settings - Fork 923
/
flags_node.go
100 lines (88 loc) · 2.34 KB
/
flags_node.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
90
91
92
93
94
95
96
97
98
99
100
package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
)
const (
nodeStoreFlag = "node.store"
nodeConfigFlag = "node.config"
)
// NodeFlags gives a set of hardcoded Node package flags.
func NodeFlags() *flag.FlagSet {
flags := &flag.FlagSet{}
flags.String(
nodeStoreFlag,
"",
"The path to root/home directory of your Celestia Node Store",
)
flags.String(
nodeConfigFlag,
"",
"Path to a customized node config TOML file",
)
return flags
}
// ParseNodeFlags parses Node flags from the given cmd and applies values to Env.
func ParseNodeFlags(ctx context.Context, cmd *cobra.Command, network p2p.Network) (context.Context, error) {
store := cmd.Flag(nodeStoreFlag).Value.String()
if store == "" {
tp := NodeType(ctx)
var err error
store, err = DefaultNodeStorePath(tp.String(), network.String())
if err != nil {
return ctx, err
}
}
ctx = WithStorePath(ctx, store)
nodeConfig := cmd.Flag(nodeConfigFlag).Value.String()
if nodeConfig != "" {
// try to load config from given path
cfg, err := nodebuilder.LoadConfig(nodeConfig)
if err != nil {
return ctx, fmt.Errorf("cmd: while parsing '%s': %w", nodeConfigFlag, err)
}
ctx = WithNodeConfig(ctx, cfg)
} else {
// check if config already exists at the store path and load it
path := StorePath(ctx)
expanded, err := homedir.Expand(filepath.Clean(path))
if err != nil {
return ctx, err
}
cfg, err := nodebuilder.LoadConfig(filepath.Join(expanded, "config.toml"))
if err == nil {
ctx = WithNodeConfig(ctx, cfg)
}
}
return ctx, nil
}
// DefaultNodeStorePath constructs the default node store path using the given
// node type and network.
func DefaultNodeStorePath(tp, network string) (string, error) {
home := os.Getenv("CELESTIA_HOME")
if home == "" {
var err error
home, err = os.UserHomeDir()
if err != nil {
return "", err
}
}
if network == p2p.Mainnet.String() {
return fmt.Sprintf("%s/.celestia-%s", home, strings.ToLower(tp)), nil
}
// only include network name in path for testnets and custom networks
return fmt.Sprintf(
"%s/.celestia-%s-%s",
home,
strings.ToLower(tp),
strings.ToLower(network),
), nil
}