Skip to content

Commit

Permalink
Extract common parsing logic into single function. (celestiaorg#1394)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoytRen authored Dec 7, 2022
1 parent 3210ca2 commit d1144b6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 142 deletions.
44 changes: 1 addition & 43 deletions cmd/celestia/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,48 +43,6 @@ var bridgeCmd = &cobra.Command{
Args: cobra.NoArgs,
Short: "Manage your Bridge node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
err error
)

ctx = cmdnode.WithNodeType(ctx, node.Bridge)

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}

cfg := cmdnode.NodeConfig(ctx)

err = p2p.ParseFlags(cmd, &cfg.P2P)
if err != nil {
return err
}

err = core.ParseFlags(cmd, &cfg.Core)
if err != nil {
return err
}

ctx, err = cmdnode.ParseMiscFlags(ctx, cmd)
if err != nil {
return err
}

rpc.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)

// set config
ctx = cmdnode.WithNodeConfig(ctx, &cfg)
cmd.SetContext(ctx)
return nil
return persistentPreRunEnv(cmd, node.Bridge, args)
},
}
50 changes: 1 addition & 49 deletions cmd/celestia/full.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:dupl
package main

import (
Expand Down Expand Up @@ -51,53 +50,6 @@ var fullCmd = &cobra.Command{
Args: cobra.NoArgs,
Short: "Manage your Full node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
err error
)

ctx = cmdnode.WithNodeType(ctx, node.Full)

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}

cfg := cmdnode.NodeConfig(ctx)

err = p2p.ParseFlags(cmd, &cfg.P2P)
if err != nil {
return err
}

err = core.ParseFlags(cmd, &cfg.Core)
if err != nil {
return err
}

err = header.ParseFlags(cmd, &cfg.Header)
if err != nil {
return err
}

ctx, err = cmdnode.ParseMiscFlags(ctx, cmd)
if err != nil {
return err
}

rpc.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)

// set config
ctx = cmdnode.WithNodeConfig(ctx, &cfg)
cmd.SetContext(ctx)
return nil
return persistentPreRunEnv(cmd, node.Full, args)
},
}
51 changes: 1 addition & 50 deletions cmd/celestia/light.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//nolint:dupl
package main

import (
Expand Down Expand Up @@ -51,54 +50,6 @@ var lightCmd = &cobra.Command{
Args: cobra.NoArgs,
Short: "Manage your Light node",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var (
ctx = cmd.Context()
err error
)

ctx = cmdnode.WithNodeType(ctx, node.Light)

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

// loads existing config into the environment
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}

cfg := cmdnode.NodeConfig(ctx)

err = p2p.ParseFlags(cmd, &cfg.P2P)
if err != nil {
return err
}

err = core.ParseFlags(cmd, &cfg.Core)
if err != nil {
return err
}

err = header.ParseFlags(cmd, &cfg.Header)
if err != nil {
return err
}

ctx, err = cmdnode.ParseMiscFlags(ctx, cmd)
if err != nil {
return err
}

rpc.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)

// set config
ctx = cmdnode.WithNodeConfig(ctx, &cfg)
cmd.SetContext(ctx)
return nil
return persistentPreRunEnv(cmd, node.Light, args)
},
}
68 changes: 68 additions & 0 deletions cmd/celestia/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"github.com/spf13/cobra"

cmdnode "github.com/celestiaorg/celestia-node/cmd"
"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"
)

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

ctx = cmdnode.WithNodeType(ctx, nodeType)

parsedNetwork, err := p2p.ParseNetwork(cmd)
if err != nil {
return err
}
ctx = cmdnode.WithNetwork(ctx, parsedNetwork)

// loads existing config into the environment
ctx, err = cmdnode.ParseNodeFlags(ctx, cmd, cmdnode.Network(ctx))
if err != nil {
return err
}

cfg := cmdnode.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 = cmdnode.ParseMiscFlags(ctx, cmd)
if err != nil {
return err
}

rpc.ParseFlags(cmd, &cfg.RPC)
gateway.ParseFlags(cmd, &cfg.Gateway)
state.ParseFlags(cmd, &cfg.State)

// set config
ctx = cmdnode.WithNodeConfig(ctx, &cfg)
cmd.SetContext(ctx)
return nil
}

0 comments on commit d1144b6

Please sign in to comment.