-
Notifications
You must be signed in to change notification settings - Fork 923
/
util.go
137 lines (116 loc) · 3.09 KB
/
util.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package cmd
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/gateway"
"github.com/celestiaorg/celestia-node/nodebuilder/header"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
"github.com/celestiaorg/celestia-node/nodebuilder/p2p"
rpc_cfg "github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
"github.com/celestiaorg/celestia-node/share"
)
func PrintOutput(data interface{}, err error, formatData func(interface{}) interface{}) error {
switch {
case err != nil:
data = err.Error()
case formatData != nil:
data = formatData(data)
}
resp := struct {
Result interface{} `json:"result"`
}{
Result: data,
}
bytes, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return err
}
fmt.Fprintln(os.Stdout, string(bytes))
return nil
}
// ParseV0Namespace parses a namespace from a base64 or hex string. The param
// is expected to be the user-specified portion of a v0 namespace ID (i.e. the
// last 10 bytes).
func ParseV0Namespace(param string) (share.Namespace, error) {
userBytes, err := DecodeToBytes(param)
if err != nil {
return nil, err
}
// if the namespace ID is <= 10 bytes, left pad it with 0s
return share.NewBlobNamespaceV0(userBytes)
}
// DecodeToBytes decodes a Base64 or hex input string into a byte slice.
func DecodeToBytes(param string) ([]byte, error) {
if strings.HasPrefix(param, "0x") {
decoded, err := hex.DecodeString(param[2:])
if err != nil {
return nil, fmt.Errorf("error decoding namespace ID: %w", err)
}
return decoded, nil
}
// otherwise, it's just a base64 string
decoded, err := base64.StdEncoding.DecodeString(param)
if err != nil {
return nil, fmt.Errorf("error decoding namespace ID: %w", err)
}
return decoded, nil
}
func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) error {
var (
ctx = cmd.Context()
err error
)
ctx = WithNodeType(ctx, nodeType)
parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = WithNetwork(ctx, parsedNetwork)
// loads existing config into the environment
ctx, err = ParseNodeFlags(ctx, cmd, Network(ctx))
if err != nil {
return err
}
cfg := NodeConfig(ctx)
err = p2p.ParseFlags(cmd, &cfg.P2P)
if err != nil {
return err
}
err = core.ParseFlags(cmd, &cfg.Core)
if err != nil {
return err
}
if nodeType != node.Bridge {
err = header.ParseFlags(cmd, &cfg.Header)
if err != nil {
return err
}
}
ctx, err = ParseMiscFlags(ctx, cmd)
if err != nil {
return err
}
rpc_cfg.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)
// set config
ctx = WithNodeConfig(ctx, &cfg)
cmd.SetContext(ctx)
return nil
}
// WithFlagSet adds the given flagset to the command.
func WithFlagSet(fset []*flag.FlagSet) func(*cobra.Command) {
return func(c *cobra.Command) {
for _, set := range fset {
c.Flags().AddFlagSet(set)
}
}
}