Skip to content

Commit

Permalink
upgrade urfave/cli to V2
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmack committed Feb 9, 2023
1 parent 9c3c3e3 commit 92ab319
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 168 deletions.
2 changes: 1 addition & 1 deletion cmd/gossamer/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Gossamer `cmd` Package

This package encapsulates the entry point to Gossamer - it uses the popular
[`cli` package from `urfave`](https://github.com/urfave/cli/blob/master/docs/v1/manual.md) to expose a command-line
[`cli` package from `urfave`](https://github.com/urfave/cli/blob/master/docs/v2/manual.md) to expose a command-line
interface (CLI). The Gossamer CLI accepts several subcommands, each of which is associated with an "action"; these
subcommands and their corresponding actions are defined in [`main.go`](main.go). When the Gossamer CLI is executed
without a subcommand, the `gossamerAction` is invoked.
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

// accountAction executes the action for the "account" subcommand
Expand Down
88 changes: 44 additions & 44 deletions cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/ChainSafe/gossamer/lib/genesis"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

var (
Expand All @@ -41,7 +41,7 @@ var (
// loadConfigFile loads a default config file if --chain is specified, a specific
// config if --config is specified, or the default gossamer config otherwise.
func loadConfigFile(ctx *cli.Context, cfg *ctoml.Config) (err error) {
cfgPath := ctx.GlobalString(ConfigFlag.Name)
cfgPath := ctx.String(ConfigFlag.Name)
if cfgPath == "" {
return loadConfig(cfg, defaultGssmrConfigPath)
}
Expand All @@ -68,7 +68,7 @@ func setupConfigFromChain(ctx *cli.Context) (*ctoml.Config, *dot.Config, error)
}

// check --chain flag and load configuration from defaults.go
if id := ctx.GlobalString(ChainFlag.Name); id != "" {
if id := ctx.String(ChainFlag.Name); id != "" {
switch id {
case gossamerName:
logger.Info("loading toml configuration from " + defaultGssmrConfigPath + "...")
Expand Down Expand Up @@ -460,7 +460,7 @@ func setDotGlobalConfigFromToml(tomlCfg *ctoml.Config, cfg *dot.GlobalConfig) {
// setDotGlobalConfigFromFlags sets dot.GlobalConfig using flag values from the cli context
func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) error {
// check --basepath flag and update node configuration
if basepath := ctx.GlobalString(strings.Split(BasePathFlag.Name, ",")[0]); basepath != "" {
if basepath := ctx.String(BasePathFlag.Name); basepath != "" {
cfg.BasePath = basepath
}

Expand All @@ -478,7 +478,7 @@ func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) error
cfg.PublishMetrics = ctx.Bool("publish-metrics")

// check --metrics-address flag and update node configuration
if metricsAddress := ctx.GlobalString(MetricsAddressFlag.Name); metricsAddress != "" {
if metricsAddress := ctx.String(MetricsAddressFlag.Name); metricsAddress != "" {
cfg.MetricsAddress = metricsAddress
}

Expand All @@ -494,7 +494,7 @@ func setDotGlobalConfigFromFlags(ctx *cli.Context, cfg *dot.GlobalConfig) error
cfg.NoTelemetry = ctx.Bool("no-telemetry")

var telemetryEndpoints []genesis.TelemetryEndpoint
for _, telemetryURL := range ctx.GlobalStringSlice(TelemetryURLFlag.Name) {
for _, telemetryURL := range ctx.StringSlice(TelemetryURLFlag.Name) {
splits := strings.Split(telemetryURL, " ")
if len(splits) != 2 {
return fmt.Errorf("%s must be in the format 'URL VERBOSITY'", TelemetryURLFlag.Name)
Expand All @@ -521,8 +521,8 @@ func setDotGlobalConfigName(ctx *cli.Context, tomlCfg *ctoml.Config, cfg *dot.Gl
initialised := dot.IsNodeInitialised(globalBasePath)

// consider the --name flag as higher priority
if ctx.GlobalString(NameFlag.Name) != "" {
cfg.Name = ctx.GlobalString(NameFlag.Name)
if ctx.String(NameFlag.Name) != "" {
cfg.Name = ctx.String(NameFlag.Name)
return nil
}

Expand Down Expand Up @@ -560,12 +560,12 @@ func setDotAccountConfig(ctx *cli.Context, tomlCfg ctoml.AccountConfig, cfg *dot
}

// check --key flag and update node configuration
if key := ctx.GlobalString(KeyFlag.Name); key != "" {
if key := ctx.String(KeyFlag.Name); key != "" {
cfg.Key = key
}

// check --unlock flag and update node configuration
if unlock := ctx.GlobalString(UnlockFlag.Name); unlock != "" {
if unlock := ctx.String(UnlockFlag.Name); unlock != "" {
cfg.Unlock = unlock
}

Expand All @@ -582,11 +582,11 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC

cfg.BABELead = tomlCfg.BABELead
if ctx.IsSet(BABELeadFlag.Name) {
cfg.BABELead = ctx.GlobalBool(BABELeadFlag.Name)
cfg.BABELead = ctx.Bool(BABELeadFlag.Name)
}

// check --roles flag and update node configuration
if roles := ctx.GlobalString(RolesFlag.Name); roles != "" {
if roles := ctx.String(RolesFlag.Name); roles != "" {
// convert string to byte
n, err := strconv.Atoi(roles)
b := common.Roles(n)
Expand Down Expand Up @@ -649,13 +649,13 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
cfg.DiscoveryInterval = time.Second * time.Duration(tomlCfg.DiscoveryInterval)

// check --port flag and update node configuration
if port := ctx.GlobalUint(PortFlag.Name); port != 0 {
if port := ctx.Uint(PortFlag.Name); port != 0 {
cfg.Port = uint16(port)
}

// check --bootnodes flag and update node configuration
if bootnodes := ctx.GlobalString(BootnodesFlag.Name); bootnodes != "" {
cfg.Bootnodes = strings.Split(ctx.GlobalString(BootnodesFlag.Name), ",")
if bootnodes := ctx.String(BootnodesFlag.Name); bootnodes != "" {
cfg.Bootnodes = strings.Split(ctx.String(BootnodesFlag.Name), ",")
}

// format bootnodes
Expand All @@ -664,32 +664,32 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot
}

// check --protocol flag and update node configuration
if protocol := ctx.GlobalString(ProtocolFlag.Name); protocol != "" {
if protocol := ctx.String(ProtocolFlag.Name); protocol != "" {
cfg.ProtocolID = protocol
}

// check --nobootstrap flag and update node configuration
if nobootstrap := ctx.GlobalBool(NoBootstrapFlag.Name); nobootstrap {
if nobootstrap := ctx.Bool(NoBootstrapFlag.Name); nobootstrap {
cfg.NoBootstrap = true
}

// check --nomdns flag and update node configuration
if nomdns := ctx.GlobalBool(strings.Split(NoMDNSFlag.Name, ",")[0]); nomdns {
if nomdns := ctx.Bool(NoMDNSFlag.Name); nomdns {
cfg.NoMDNS = true
}

// check --pubip flag and update node configuration
if pubip := ctx.GlobalString(PublicIPFlag.Name); pubip != "" {
if pubip := ctx.String(PublicIPFlag.Name); pubip != "" {
cfg.PublicIP = pubip
}

// check --pubdns flag and update node configuration
if pubdns := ctx.GlobalString(PublicDNSFlag.Name); pubdns != "" {
if pubdns := ctx.String(PublicDNSFlag.Name); pubdns != "" {
cfg.PublicDNS = pubdns
}

// check --node-key flag and update node configuration
if nodekey := ctx.GlobalString(NodeKeyFlag.Name); nodekey != "" {
if nodekey := ctx.String(NodeKeyFlag.Name); nodekey != "" {
cfg.NodeKey = nodekey
}

Expand Down Expand Up @@ -723,14 +723,14 @@ func setDotRPCConfig(ctx *cli.Context, tomlCfg ctoml.RPCConfig, cfg *dot.RPCConf
cfg.WSUnsafeExternal = tomlCfg.WSUnsafeExternal

// check --rpc flag and update node configuration
if enabled := ctx.GlobalBool(RPCEnabledFlag.Name); enabled || cfg.Enabled {
if enabled := ctx.Bool(RPCEnabledFlag.Name); enabled || cfg.Enabled {
cfg.Enabled = true
} else if ctx.IsSet(RPCEnabledFlag.Name) && !enabled {
cfg.Enabled = false
}

// check --rpc-external flag and update node configuration
if external := ctx.GlobalBool(RPCExternalFlag.Name); external {
if external := ctx.Bool(RPCExternalFlag.Name); external {
cfg.Enabled = true
cfg.External = true
} else if ctx.IsSet(RPCExternalFlag.Name) && !external {
Expand All @@ -739,53 +739,53 @@ func setDotRPCConfig(ctx *cli.Context, tomlCfg ctoml.RPCConfig, cfg *dot.RPCConf
}

// check --rpc-unsafe flag value
if rpcUnsafe := ctx.GlobalBool(RPCUnsafeEnabledFlag.Name); rpcUnsafe {
if rpcUnsafe := ctx.Bool(RPCUnsafeEnabledFlag.Name); rpcUnsafe {
cfg.Unsafe = true
}

// check --rpc-unsafe-external flag value
if externalUnsafe := ctx.GlobalBool(RPCUnsafeExternalFlag.Name); externalUnsafe {
if externalUnsafe := ctx.Bool(RPCUnsafeExternalFlag.Name); externalUnsafe {
cfg.Unsafe = true
cfg.UnsafeExternal = true
}

// check --ws-unsafe flag value
if wsUnsafe := ctx.GlobalBool(WSUnsafeEnabledFlag.Name); wsUnsafe {
if wsUnsafe := ctx.Bool(WSUnsafeEnabledFlag.Name); wsUnsafe {
cfg.WSUnsafe = true
}

// check --ws-unsafe-external flag value
if wsExternalUnsafe := ctx.GlobalBool(WSUnsafeExternalFlag.Name); wsExternalUnsafe {
if wsExternalUnsafe := ctx.Bool(WSUnsafeExternalFlag.Name); wsExternalUnsafe {
cfg.WSUnsafe = true
cfg.WSUnsafeExternal = true
}

// check --rpcport flag and update node configuration
if port := ctx.GlobalUint(strings.Split(RPCPortFlag.Name, ",")[0]); port != 0 {
if port := ctx.Uint(RPCPortFlag.Name); port != 0 {
cfg.Port = uint32(port)
}

// check --rpchost flag and update node configuration
if host := ctx.GlobalString(RPCHostFlag.Name); host != "" {
if host := ctx.String(RPCHostFlag.Name); host != "" {
cfg.Host = host
}

// check --rpcmods flag and update node configuration
if modules := ctx.GlobalString(RPCModulesFlag.Name); modules != "" {
cfg.Modules = strings.Split(ctx.GlobalString(RPCModulesFlag.Name), ",")
if modules := ctx.String(RPCModulesFlag.Name); modules != "" {
cfg.Modules = strings.Split(ctx.String(RPCModulesFlag.Name), ",")
}

if wsport := ctx.GlobalUint(strings.Split(WSPortFlag.Name, ",")[0]); wsport != 0 {
if wsport := ctx.Uint(WSPortFlag.Name); wsport != 0 {
cfg.WSPort = uint32(wsport)
}

if WS := ctx.GlobalBool(WSFlag.Name); WS || cfg.WS {
if WS := ctx.Bool(WSFlag.Name); WS || cfg.WS {
cfg.WS = true
} else if ctx.IsSet(WSFlag.Name) && !WS {
cfg.WS = false
}

if wsExternal := ctx.GlobalBool(WSExternalFlag.Name); wsExternal {
if wsExternal := ctx.Bool(WSExternalFlag.Name); wsExternal {
cfg.WS = true
cfg.WSExternal = true
} else if ctx.IsSet(WSExternalFlag.Name) && !wsExternal {
Expand Down Expand Up @@ -863,17 +863,17 @@ func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {
}

// check genesis id and use genesis id if --chain flag not set
if !ctx.GlobalIsSet(ChainFlag.Name) {
if !ctx.IsSet(ChainFlag.Name) {
cfg.Global.ID = gen.ID
}

// check genesis bootnodes and use genesis --bootnodes if name flag not set
if !ctx.GlobalIsSet(BootnodesFlag.Name) {
if !ctx.IsSet(BootnodesFlag.Name) {
cfg.Network.Bootnodes = common.BytesToStringArray(gen.Bootnodes)
}

// check genesis protocol and use genesis --protocol if name flag not set
if !ctx.GlobalIsSet(ProtocolFlag.Name) {
if !ctx.IsSet(ProtocolFlag.Name) {
cfg.Network.ProtocolID = gen.ProtocolID
}

Expand All @@ -896,8 +896,8 @@ func updateDotConfigFromGenesisData(ctx *cli.Context, cfg *dot.Config) error {

func setDotPprofConfig(ctx *cli.Context, tomlCfg ctoml.PprofConfig, cfg *dot.PprofConfig) {
// Flag takes precedence over TOML config, default is ignored.
if ctx.GlobalIsSet(PprofServerFlag.Name) {
cfg.Enabled = ctx.GlobalBool(PprofServerFlag.Name)
if ctx.IsSet(PprofServerFlag.Name) {
cfg.Enabled = ctx.Bool(PprofServerFlag.Name)
} else {
cfg.Enabled = tomlCfg.Enabled
}
Expand All @@ -919,24 +919,24 @@ func setDotPprofConfig(ctx *cli.Context, tomlCfg ctoml.PprofConfig, cfg *dot.Ppr
}

// check --pprofaddress flag and update node configuration
if address := ctx.GlobalString(PprofAddressFlag.Name); address != "" {
if address := ctx.String(PprofAddressFlag.Name); address != "" {
cfg.Settings.ListeningAddress = address
}

if rate := ctx.GlobalInt(PprofBlockRateFlag.Name); rate > 0 {
if rate := ctx.Int(PprofBlockRateFlag.Name); rate > 0 {
cfg.Settings.BlockProfileRate = rate
}

if rate := ctx.GlobalInt(PprofMutexRateFlag.Name); rate > 0 {
if rate := ctx.Int(PprofMutexRateFlag.Name); rate > 0 {
cfg.Settings.MutexProfileRate = rate
}

logger.Debug("pprof configuration: " + cfg.String())
}

func setStateConfig(ctx *cli.Context, tomlCfg ctoml.StateConfig, cfg *dot.StateConfig) {
if ctx.GlobalIsSet(RewindFlag.Name) {
cfg.Rewind = ctx.GlobalUint(RewindFlag.Name)
if ctx.IsSet(RewindFlag.Name) {
cfg.Rewind = ctx.Uint(RewindFlag.Name)
} else if tomlCfg.Rewind > 0 {
cfg.Rewind = tomlCfg.Rewind
}
Expand Down
20 changes: 11 additions & 9 deletions cmd/gossamer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/ChainSafe/gossamer/lib/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

// TestConfigFromChainFlag tests createDotConfig using the --chain flag
Expand Down Expand Up @@ -125,9 +125,6 @@ func TestGlobalConfigFromFlags(t *testing.T) {
MetricsAddress: "localhost:9876",
}

testApp := cli.NewApp()
testApp.Flags = RootFlags

testcases := map[string]struct {
args []string
expected dot.GlobalConfig
Expand Down Expand Up @@ -239,8 +236,12 @@ func TestGlobalConfigFromFlags(t *testing.T) {
}

for key, c := range testcases {
c := c // bypass scopelint false positive
c := c
t.Run(key, func(t *testing.T) {
t.Parallel()
testApp := cli.NewApp()
testApp.Writer = io.Discard
testApp.Flags = RootFlags
testApp.Action = func(ctx *cli.Context) error {
cfg, err := createDotConfig(ctx)
require.NoError(t, err)
Expand Down Expand Up @@ -406,9 +407,6 @@ func TestNetworkConfigFromFlags(t *testing.T) {
DiscoveryInterval: 10 * time.Second,
}

testApp := cli.NewApp()
testApp.Flags = StartupFlags

testcases := map[string]struct {
args []string
expected dot.NetworkConfig
Expand Down Expand Up @@ -505,8 +503,12 @@ func TestNetworkConfigFromFlags(t *testing.T) {
}

for key, c := range testcases {
c := c // bypass scopelint false positive
c := c
t.Run(key, func(t *testing.T) {
t.Parallel()
testApp := cli.NewApp()
testApp.Writer = io.Discard
testApp.Flags = StartupFlags
testApp.Action = func(ctx *cli.Context) error {
cfg, err := createDotConfig(ctx)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gossamer/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (
ctoml "github.com/ChainSafe/gossamer/dot/config/toml"
"github.com/ChainSafe/gossamer/lib/utils"

"github.com/urfave/cli"
"github.com/urfave/cli/v2"
)

// exportAction is the action for the "export" subcommand
func exportAction(ctx *cli.Context) error {
// use --config value as export destination
config := ctx.GlobalString(ConfigFlag.Name)
config := ctx.String(ConfigFlag.Name)

// check if --config value is set
if config == "" {
Expand Down
Loading

0 comments on commit 92ab319

Please sign in to comment.