refactor(blockchain): centralise chain app name for AX examples

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 10:13:15 +00:00
parent 31dedcf9a2
commit cf7f1672cd
5 changed files with 29 additions and 16 deletions

View file

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

View file

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

View file

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

View file

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

View file

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