refactor(blockchain): reject unexpected chain args

Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
Virgil 2026-04-04 10:00:05 +00:00
parent f39140cf32
commit 9780153778
4 changed files with 14 additions and 0 deletions

View file

@ -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")

View file

@ -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) {

View file

@ -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)
},

View file

@ -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")