Skip to content

Commit

Permalink
feat(cmd): pass Start options; add WithFlagSet option (celestiaorg#2950)
Browse files Browse the repository at this point in the history
<!--
Thank you for submitting a pull request!

Please make sure you have reviewed our contributors guide before
submitting your
first PR.

Please ensure you've addressed or included references to any related
issues.

Tips:
- Use keywords like "closes" or "fixes" followed by an issue number to
automatically close related issues when the PR is merged (e.g., "closes
celestiaorg#123" or "fixes celestiaorg#123").
- Describe the changes made in the PR.
- Ensure the PR has one of the required tags (kind:fix, kind:misc,
kind:break!, kind:refactor, kind:feat, kind:deps, kind:docs, kind:ci,
kind:chore, kind:testing)

-->

Fixes celestiaorg#2936 

This PR updates the `Start` command to accept a variadic number of
`funcs` which can be used to customise the command.

This allows the caller to hook into and modify the command, for example
to add a `PreRun` hook, as required for `celestia-da`.

With this PR in place, the `celestia-da` requirement, which needs to add
some required flags and add a `PreRun` hook can be satisfied as:

```go
	bridgeCmd := bridge.NewCommand(WithPreRun())
	lightCmd := light.NewCommand(WithPreRun())
	fullCmd := full.NewCommand(WithPreRun())
```

Ideally this can be applied for other commands `Init`, `Auth` as well,
but it's not required for the current requirement.
  • Loading branch information
tuxcanfly authored Nov 23, 2023
1 parent d1883d6 commit dd3b8e1
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 153 deletions.
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
88 changes: 88 additions & 0 deletions cmd/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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(),
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(),
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)
}
}
}

0 comments on commit dd3b8e1

Please sign in to comment.