Skip to content

Commit

Permalink
fix(cmd/celestia): Only provide necessary flags to aux subcmds
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Feb 19, 2025
1 parent 5dbc2b7 commit 717c39c
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 95 deletions.
2 changes: 1 addition & 1 deletion cmd/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func AuthCmd(fsets ...*flag.FlagSet) *cobra.Command {
Short: "Signs and outputs a hex-encoded JWT token with the given permissions.",
Long: "Signs and outputs a hex-encoded JWT token with the given permissions. NOTE: only use this command when " +
"the node has already been initialized and started.",
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, _ []string) error {
return ParseMinimumFlags(cmd)
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
25 changes: 17 additions & 8 deletions cmd/celestia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@ import (
"github.com/spf13/pflag"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

func WithSubcommands() func(*cobra.Command, node.Type, []*pflag.FlagSet) {
return func(c *cobra.Command, tp node.Type, flags []*pflag.FlagSet) {
// WithSubcommands returns the set of commands that require the full flagset.
func WithSubcommands() func(*cobra.Command, []*pflag.FlagSet) {
return func(c *cobra.Command, flags []*pflag.FlagSet) {
c.AddCommand(
cmdnode.Init(flags...),
cmdnode.Start(cmdnode.WithFlagSet(flags)),
)
}
}

// WithAuxiliarySubcommands returns the set of commands that require only the
// minimum flagset for node store determination.
func WithAuxiliarySubcommands() func(*cobra.Command, []*pflag.FlagSet) {
return func(c *cobra.Command, flags []*pflag.FlagSet) {
c.AddCommand(
cmdnode.Init(tp, flags...),
cmdnode.Start(tp, cmdnode.WithFlagSet(flags)),
cmdnode.AuthCmd(flags...),
cmdnode.ResetStore(flags...),
cmdnode.RemoveConfigCmd(flags...),
Expand All @@ -25,9 +34,9 @@ func WithSubcommands() func(*cobra.Command, node.Type, []*pflag.FlagSet) {
}

func init() {
bridgeCmd := cmdnode.NewBridge(WithSubcommands())
lightCmd := cmdnode.NewLight(WithSubcommands())
fullCmd := cmdnode.NewFull(WithSubcommands())
bridgeCmd := cmdnode.NewBridge(WithSubcommands(), WithAuxiliarySubcommands())
lightCmd := cmdnode.NewLight(WithSubcommands(), WithAuxiliarySubcommands())
fullCmd := cmdnode.NewFull(WithSubcommands(), WithAuxiliarySubcommands())
rootCmd.AddCommand(
bridgeCmd,
lightCmd,
Expand Down
4 changes: 2 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func RemoveConfigCmd(fsets ...*flag.FlagSet) *cobra.Command {
Use: "config-remove",
Short: "Deletes the node's config",
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, _ []string) error {
return ParseMinimumFlags(cmd)
},
RunE: func(cmd *cobra.Command, _ []string) error {
Expand All @@ -34,7 +34,7 @@ func UpdateConfigCmd(fsets ...*flag.FlagSet) *cobra.Command {
Long: "Updates the node's outdated config with default values from newly-added fields. Check the config " +
" afterwards to ensure all old custom values were preserved.",
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, _ []string) error {
return ParseMinimumFlags(cmd)
},
RunE: func(cmd *cobra.Command, _ []string) error {
Expand Down
14 changes: 12 additions & 2 deletions cmd/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@ const (
nodeConfigFlag = "node.config"
)

// NodeFlags gives a set of hardcoded Node package flags.
func NodeFlags() *flag.FlagSet {
// NodeStoreFlag returns a flag set for setting the Node Store path.
func NodeStoreFlag() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
nodeStoreFlag,
"",
"The path to root/home directory of your Celestia Node Store",
)

return flags
}

// NodeFlags gives a set of hardcoded Node package flags.
func NodeFlags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.AddFlagSet(NodeStoreFlag())

flags.String(
nodeConfigFlag,
"",
Expand Down
13 changes: 5 additions & 8 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import (
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

// Init constructs a CLI command to initialize Celestia Node of any type with the given flags.
func Init(tp node.Type, fsets ...*flag.FlagSet) *cobra.Command {
func Init(fsets ...*flag.FlagSet) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialization for Celestia Node. Passed flags have persisted effect.",
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return PersistentPreRunEnv(cmd, tp, args)
},
Use: "init",
Short: "Initialization for Celestia Node. Passed flags have persisted effect.",
Args: cobra.NoArgs,
PreRunE: PreRunEnv,
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()

Expand Down
88 changes: 29 additions & 59 deletions cmd/node.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,66 @@
package cmd

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"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"
"github.com/celestiaorg/celestia-node/nodebuilder/pruner"
"github.com/celestiaorg/celestia-node/nodebuilder/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"
)

func NewBridge(options ...func(*cobra.Command, node.Type, []*pflag.FlagSet)) *cobra.Command {
flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
MiscFlags(),
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
pruner.Flags(),
}
cmd := &cobra.Command{
Use: "bridge [subcommand]",
Args: cobra.NoArgs,
Short: "Manage your Bridge node",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := WithNodeType(cmd.Context(), node.Bridge)
cmd.SetContext(ctx)
},
}
func NewBridge(addFullFlags, addMinFlags func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
return createTopLevelCmd(node.Bridge, addFullFlags, addMinFlags)
}

for _, option := range options {
option(cmd, node.Bridge, flags)
}
return cmd
func NewFull(addFullFlags, addMinFlags func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
return createTopLevelCmd(node.Full, addFullFlags, addMinFlags)
}

func NewLight(options ...func(*cobra.Command, node.Type, []*pflag.FlagSet)) *cobra.Command {
flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
header.Flags(),
MiscFlags(),
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
pruner.Flags(),
}
cmd := &cobra.Command{
Use: "light [subcommand]",
Args: cobra.NoArgs,
Short: "Manage your Light node",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := WithNodeType(cmd.Context(), node.Light)
cmd.SetContext(ctx)
},
}
for _, option := range options {
option(cmd, node.Light, flags)
}
return cmd
func NewLight(addFullFlags, addMinFlags func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
return createTopLevelCmd(node.Light, addFullFlags, addMinFlags)
}

func NewFull(options ...func(*cobra.Command, node.Type, []*pflag.FlagSet)) *cobra.Command {
func createTopLevelCmd(
nodeType node.Type,
addFullFlags,
addMinFlags func(*cobra.Command, []*pflag.FlagSet),
) *cobra.Command {
minFlags := []*pflag.FlagSet{
NodeStoreFlag(),
p2p.NetworkFlag(),
}

flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
header.Flags(),
MiscFlags(),
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
pruner.Flags(),
}

cmd := &cobra.Command{
Use: "full [subcommand]",
Use: fmt.Sprintf("%s [subcommand]", strings.ToLower(nodeType.String())),
Args: cobra.NoArgs,
Short: "Manage your Full node",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ctx := WithNodeType(cmd.Context(), node.Full)
Short: fmt.Sprintf("Manage your %s node", strings.ToLower(nodeType.String())),
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
ctx := WithNodeType(cmd.Context(), nodeType)
cmd.SetContext(ctx)
},
}
for _, option := range options {
option(cmd, node.Full, flags)
}

addFullFlags(cmd, flags)
addMinFlags(cmd, minFlags)

return cmd
}
2 changes: 1 addition & 1 deletion cmd/reset_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func ResetStore(fsets ...*flag.FlagSet) *cobra.Command {
Use: "unsafe-reset-store",
Short: "Resets the node's store to a new state. Leaves the keystore and config intact.",
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
PreRunE: func(cmd *cobra.Command, _ []string) error {
return ParseMinimumFlags(cmd)
},
RunE: func(cmd *cobra.Command, _ []string) error {
Expand Down
7 changes: 2 additions & 5 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ import (
"github.com/celestiaorg/celestia-app/v3/app/encoding"

"github.com/celestiaorg/celestia-node/nodebuilder"
"github.com/celestiaorg/celestia-node/nodebuilder/node"
)

// Start constructs a CLI command to start Celestia Node daemon of any type with the given flags.
func Start(tp node.Type, options ...func(*cobra.Command)) *cobra.Command {
func Start(options ...func(*cobra.Command)) *cobra.Command {
cmd := &cobra.Command{
Use: "start",
Short: `Starts Node daemon. First stopping signal gracefully stops the Node and second terminates it.
Options passed on start override configuration options only on start and are not persisted in config.`,
Aliases: []string{"run", "daemon"},
Args: cobra.NoArgs,
SilenceUsage: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
return PersistentPreRunEnv(cmd, tp, args)
},
PreRunE: PreRunEnv,
RunE: func(cmd *cobra.Command, _ []string) (err error) {
ctx := cmd.Context()

Expand Down
4 changes: 3 additions & 1 deletion cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ func ParseMinimumFlags(cmd *cobra.Command) error {
return nil
}

func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) error {
func PreRunEnv(cmd *cobra.Command, _ []string) error {
var (
ctx = cmd.Context()
err error
)

nodeType := NodeType(cmd.Context())

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
Expand Down
25 changes: 17 additions & 8 deletions nodebuilder/p2p/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,27 @@ const (
mutualFlag = "p2p.mutual"
)

func NetworkFlag() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
networkFlag,
DefaultNetwork.String(),
fmt.Sprintf("The name of the network to connect to, e.g. %s. Must be passed on "+
"both init and start to take effect. Assumes mainnet (%s) unless otherwise specified.",
listAvailableNetworks(),
DefaultNetwork.String()),
)

return flags
}

// Flags gives a set of p2p flags.
func Flags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.AddFlagSet(NetworkFlag())

flags.StringSlice(
mutualFlag,
nil,
Expand All @@ -30,14 +47,6 @@ Such connection is immune to peer scoring slashing and connection module trimmin
Peers must bidirectionally point to each other. (Format: multiformats.io/multiaddr)
`,
)
flags.String(
networkFlag,
DefaultNetwork.String(),
fmt.Sprintf("The name of the network to connect to, e.g. %s. Must be passed on "+
"both init and start to take effect. Assumes mainnet (%s) unless otherwise specified.",
listAvailableNetworks(),
DefaultNetwork.String()),
)

return flags
}
Expand Down

0 comments on commit 717c39c

Please sign in to comment.