-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
nodes_commands.go
68 lines (56 loc) · 1.65 KB
/
nodes_commands.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
package cmd
import (
"fmt"
"strings"
"github.com/urfave/cli"
)
func initCosmosNodeSubCmd(s *Shell) cli.Command {
return nodeCommand("Cosmos", NewCosmosNodeClient(s))
}
func initStarkNetNodeSubCmd(s *Shell) cli.Command {
return nodeCommand("StarkNet", NewStarkNetNodeClient(s))
}
func initEVMNodeSubCmd(s *Shell) cli.Command {
return nodeCommand("EVM", NewEVMNodeClient(s))
}
func initSolanaNodeSubCmd(s *Shell) cli.Command {
return nodeCommand("Solana", NewSolanaNodeClient(s))
}
// nodeCommand returns a cli.Command with subcommands for the given NodeClient.
// A string cli.Flag for "name" is automatically included.
func nodeCommand(typ string, client NodeClient) cli.Command {
lower := strings.ToLower(typ)
return cli.Command{
Name: lower,
Usage: fmt.Sprintf("Commands for handling %s node configuration", typ),
Subcommands: cli.Commands{
{
Name: "list",
Usage: fmt.Sprintf("List all existing %s nodes", typ),
Action: client.IndexNodes,
},
},
}
}
// NodeClient is a generic client interface for any of node.
type NodeClient interface {
IndexNodes(c *cli.Context) error
}
type nodeClient[P TableRenderer] struct {
*Shell
path string
}
// newNodeClient returns a new NodeClient for a particular type of NodeStatus.
// P is a TableRenderer for []types.NodeStatus.
func newNodeClient[P TableRenderer](s *Shell, name string) NodeClient {
return &nodeClient[P]{
Shell: s,
path: "/v2/nodes/" + name,
}
}
// IndexNodes returns all nodes.
func (cli *nodeClient[P2]) IndexNodes(c *cli.Context) (err error) {
var p P2
return cli.getPage(cli.path, c.Int("page"), &p)
}
var nodeHeaders = []string{"Name", "Chain ID", "State", "Config"}