From 97801537781d51bfba34f49d34322d2802bc29d2 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 10:00:05 +0000 Subject: [PATCH] refactor(blockchain): reject unexpected chain args Co-Authored-By: Charon --- chain_commands.go | 1 + commands_test.go | 11 +++++++++++ explorer_command.go | 1 + sync_command.go | 1 + 4 files changed, 14 insertions(+) diff --git a/chain_commands.go b/chain_commands.go index 1ecf3c9..0e2e18c 100644 --- a/chain_commands.go +++ b/chain_commands.go @@ -40,6 +40,7 @@ func AddChainCommands(root *cobra.Command) { 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 --daemon\ncore-chain chain sync --stop", + Args: cobra.NoArgs, } chainCmd.PersistentFlags().StringVar(&dataDir, "data-dir", defaultChainDataDirPath(), "blockchain data directory") diff --git a/commands_test.go b/commands_test.go index 0d7bfd8..9c1a074 100644 --- a/commands_test.go +++ b/commands_test.go @@ -62,6 +62,7 @@ func TestNewChainExplorerCommand_Good_Metadata(t *testing.T) { 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, "Chain Explorer", chainExplorerFrameTitle) + assert.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command") } func TestNewChainSyncCommand_Good_Metadata(t *testing.T) { @@ -75,6 +76,16 @@ func TestNewChainSyncCommand_Good_Metadata(t *testing.T) { 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.ErrorContains(t, cmd.Args(cmd, []string{"unexpected"}), "unknown command") +} + +func TestAddChainCommands_Bad_RejectsUnexpectedArgs(t *testing.T) { + root := &cobra.Command{Use: "test"} + AddChainCommands(root) + + chainCmd, _, err := root.Find([]string{"chain"}) + require.NoError(t, err) + require.ErrorContains(t, chainCmd.Args(chainCmd, []string{"unexpected"}), "unknown command") } func TestNewChainSyncCommand_Bad_RejectsConflictingFlags(t *testing.T) { diff --git a/explorer_command.go b/explorer_command.go index 4706847..d2d23c9 100644 --- a/explorer_command.go +++ b/explorer_command.go @@ -38,6 +38,7 @@ func newChainExplorerCommand(dataDir, seed *string, testnet *bool) *cobra.Comman 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", + 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 7813513..dbe2e1c 100644 --- a/sync_command.go +++ b/sync_command.go @@ -43,6 +43,7 @@ func newChainSyncCommand(dataDir, seed *string, testnet *bool) *cobra.Command { 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, RunE: func(cmd *cobra.Command, args []string) error { if runAsDaemon && stopDaemon { return fmt.Errorf("blockchain: --daemon and --stop are mutually exclusive")