From cf7f1672cd511657989cf73caec843950f64001f Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 10:13:15 +0000 Subject: [PATCH] refactor(blockchain): centralise chain app name for AX examples Co-Authored-By: Virgil --- chain_commands.go | 17 ++++++++++++----- cmd/core-chain/main.go | 4 ++-- commands_test.go | 10 +++++++--- explorer_command.go | 2 +- sync_command.go | 12 +++++++----- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/chain_commands.go b/chain_commands.go index 82b39de..3091600 100644 --- a/chain_commands.go +++ b/chain_commands.go @@ -16,7 +16,11 @@ import ( "github.com/spf13/cobra" ) +// AppName is the CLI binary name used in executable examples and root wiring. +const AppName = "core-chain" + const defaultChainSeed = "seeds.lthn.io:36942" +const chainCommandPath = AppName + " chain" // AddChainCommands registers the `chain` command group on a Cobra root. // @@ -36,11 +40,14 @@ func AddChainCommands(root *cobra.Command) { ) chainCmd := &cobra.Command{ - Use: "chain", - Short: "Manage the Lethean blockchain node", - Long: "Manage the Lethean blockchain node: sync and explore.", - Example: "core-chain chain explorer --data-dir ~/.lethean/chain\ncore-chain chain sync\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", - Args: cobra.NoArgs, + Use: "chain", + Short: "Manage the Lethean blockchain node", + Long: "Manage the Lethean blockchain node: sync and explore.", + Example: chainCommandPath + " explorer --data-dir ~/.lethean/chain\n" + + chainCommandPath + " sync\n" + + chainCommandPath + " sync --daemon\n" + + chainCommandPath + " sync --stop", + Args: cobra.NoArgs, } chainCmd.PersistentFlags().StringVar(&dataDir, "data-dir", defaultChainDataDirPath(), "blockchain data directory") diff --git a/cmd/core-chain/main.go b/cmd/core-chain/main.go index 2563f77..3214500 100644 --- a/cmd/core-chain/main.go +++ b/cmd/core-chain/main.go @@ -6,12 +6,12 @@ package main import ( - cli "dappco.re/go/core/cli/pkg/cli" blockchain "dappco.re/go/core/blockchain" + cli "dappco.re/go/core/cli/pkg/cli" ) func main() { - cli.WithAppName("core-chain") + cli.WithAppName(blockchain.AppName) cli.Main( cli.WithCommands("chain", blockchain.AddChainCommands), ) diff --git a/commands_test.go b/commands_test.go index 423cb3d..f82f7e7 100644 --- a/commands_test.go +++ b/commands_test.go @@ -22,7 +22,7 @@ func TestAddChainCommands_Good_RegistersParent(t *testing.T) { assert.Equal(t, "chain", chainCmd.Name()) assert.Equal(t, "Manage the Lethean blockchain node", chainCmd.Short) assert.Equal(t, "Manage the Lethean blockchain node: sync and explore.", chainCmd.Long) - assert.Equal(t, "core-chain chain explorer --data-dir ~/.lethean/chain\ncore-chain chain sync\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", chainCmd.Example) + assert.Equal(t, chainCommandPath+" explorer --data-dir ~/.lethean/chain\n"+chainCommandPath+" sync\n"+chainCommandPath+" sync --daemon\n"+chainCommandPath+" sync --stop", chainCmd.Example) } func TestAddChainCommands_Good_HasSubcommands(t *testing.T) { @@ -60,7 +60,7 @@ func TestNewChainExplorerCommand_Good_Metadata(t *testing.T) { assert.Equal(t, "explorer", cmd.Use) assert.Equal(t, "Inspect blocks in a terminal UI", cmd.Short) assert.Equal(t, "Browse blocks and transactions in a terminal UI while the node syncs.", cmd.Long) - assert.Equal(t, "core-chain chain explorer --data-dir ~/.lethean/chain", cmd.Example) + assert.Equal(t, chainCommandPath+" explorer --data-dir ~/.lethean/chain", cmd.Example) assert.Equal(t, "Chain Explorer", chainExplorerFrameTitle) assert.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command") } @@ -75,10 +75,14 @@ func TestNewChainSyncCommand_Good_Metadata(t *testing.T) { assert.Equal(t, "sync", cmd.Use) assert.Equal(t, "Synchronise from P2P peers", cmd.Short) assert.Equal(t, "Synchronise the blockchain from P2P peers in foreground or daemon mode.", cmd.Long) - assert.Equal(t, "core-chain chain sync\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", cmd.Example) + assert.Equal(t, chainCommandPath+" sync\n"+chainCommandPath+" sync --daemon\n"+chainCommandPath+" sync --stop", cmd.Example) assert.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command") } +func TestAppName_Good(t *testing.T) { + assert.Equal(t, "core-chain", AppName) +} + func TestAddChainCommands_Bad_RejectsUnexpectedArgs(t *testing.T) { root := &cobra.Command{Use: "test"} AddChainCommands(root) diff --git a/explorer_command.go b/explorer_command.go index d2d23c9..6bbed49 100644 --- a/explorer_command.go +++ b/explorer_command.go @@ -37,7 +37,7 @@ func newChainExplorerCommand(dataDir, seed *string, testnet *bool) *cobra.Comman Use: "explorer", Short: "Inspect blocks in a terminal UI", Long: "Browse blocks and transactions in a terminal UI while the node syncs.", - Example: "core-chain chain explorer --data-dir ~/.lethean/chain", + Example: chainCommandPath + " explorer --data-dir ~/.lethean/chain", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { return runChainExplorer(*dataDir, *seed, *testnet) diff --git a/sync_command.go b/sync_command.go index dbe2e1c..e931357 100644 --- a/sync_command.go +++ b/sync_command.go @@ -39,11 +39,13 @@ func newChainSyncCommand(dataDir, seed *string, testnet *bool) *cobra.Command { ) cmd := &cobra.Command{ - Use: "sync", - Short: "Synchronise from P2P peers", - Long: "Synchronise the blockchain from P2P peers in foreground or daemon mode.", - Example: "core-chain chain sync\ncore-chain chain sync --daemon\ncore-chain chain sync --stop", - Args: cobra.NoArgs, + Use: "sync", + Short: "Synchronise from P2P peers", + Long: "Synchronise the blockchain from P2P peers in foreground or daemon mode.", + Example: chainCommandPath + " sync\n" + + chainCommandPath + " sync --daemon\n" + + chainCommandPath + " sync --stop", + Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { if runAsDaemon && stopDaemon { return fmt.Errorf("blockchain: --daemon and --stop are mutually exclusive")