Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): pass Start options; add WithFlagSet option #2950

Merged
merged 7 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions cmd/celestia/bridge.go

This file was deleted.

51 changes: 0 additions & 51 deletions cmd/celestia/full.go

This file was deleted.

51 changes: 0 additions & 51 deletions cmd/celestia/light.go

This file was deleted.

19 changes: 19 additions & 0 deletions cmd/celestia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ import (
"os"

"github.com/spf13/cobra"
"github.com/spf13/pflag"

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

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)),
cmdnode.AuthCmd(flags...),
cmdnode.ResetStore(flags...),
cmdnode.RemoveConfigCmd(flags...),
cmdnode.UpdateConfigCmd(flags...),
)
}
}

func init() {
bridgeCmd := cmdnode.NewBridge(WithSubcommands())
lightCmd := cmdnode.NewLight(WithSubcommands())
fullCmd := cmdnode.NewFull(WithSubcommands())
rootCmd.AddCommand(
bridgeCmd,
lightCmd,
Expand Down
92 changes: 92 additions & 0 deletions cmd/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"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/rpc"
"github.com/celestiaorg/celestia-node/nodebuilder/state"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewBridge(options ...func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
MiscFlags(),
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
}
cmd := &cobra.Command{
Use: "bridge [subcommand]",
Args: cobra.NoArgs,
Short: "Manage your Bridge node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return PersistentPreRunEnv(cmd, node.Bridge, args)
},
}
for _, option := range options {
option(cmd, flags)
}
return cmd
}

func NewLight(options ...func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
header.Flags(),
MiscFlags(),
// NOTE: for now, state-related queries can only be accessed
// over an RPC connection with a celestia-core node.
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
}
cmd := &cobra.Command{
Use: "light [subcommand]",
Args: cobra.NoArgs,
Short: "Manage your Light node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return PersistentPreRunEnv(cmd, node.Light, args)
},
}
for _, option := range options {
option(cmd, flags)
}
return cmd
}

func NewFull(options ...func(*cobra.Command, []*pflag.FlagSet)) *cobra.Command {
flags := []*pflag.FlagSet{
NodeFlags(),
p2p.Flags(),
header.Flags(),
MiscFlags(),
// NOTE: for now, state-related queries can only be accessed
// over an RPC connection with a celestia-core node.
tuxcanfly marked this conversation as resolved.
Show resolved Hide resolved
core.Flags(),
rpc.Flags(),
gateway.Flags(),
state.Flags(),
}
cmd := &cobra.Command{
Use: "full [subcommand]",
Args: cobra.NoArgs,
Short: "Manage your Full node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return PersistentPreRunEnv(cmd, node.Full, args)
},
}
for _, option := range options {
option(cmd, flags)
}
return cmd
}
8 changes: 4 additions & 4 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
Expand All @@ -18,7 +17,7 @@ import (
)

// Start constructs a CLI command to start Celestia Node daemon of any type with the given flags.
func Start(fsets ...*flag.FlagSet) *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.
Expand Down Expand Up @@ -72,8 +71,9 @@ Options passed on start override configuration options only on start and are not
return nd.Stop(ctx)
},
}
for _, set := range fsets {
cmd.Flags().AddFlagSet(set)
// Apply each passed option to the command
for _, option := range options {
option(cmd)
}
return cmd
}
10 changes: 10 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/celestiaorg/celestia-node/nodebuilder/core"
"github.com/celestiaorg/celestia-node/nodebuilder/gateway"
Expand Down Expand Up @@ -125,3 +126,12 @@ func PersistentPreRunEnv(cmd *cobra.Command, nodeType node.Type, _ []string) err
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)
}
}
}
Loading