diff --git a/src/clparse.rs b/src/clparse.rs index c7166c69..26209e65 100644 --- a/src/clparse.rs +++ b/src/clparse.rs @@ -1,5 +1,4 @@ -//! @brief Command line setup and parse - +#![allow(unused_imports)] use { clap::{ crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches, SubCommand, @@ -88,7 +87,8 @@ pub fn parse_command_line() -> ArgMatches<'static> { .possible_values(&["mainnet", "testnet", "devnet"]) .default_value("mainnet") .help("Network to deploy on"), - ) .subcommand( + ) + .subcommand( SubCommand::with_name("examples") .about("Show usage examples for OSVM CLI commands") .arg( @@ -500,5 +500,6 @@ pub fn parse_command_line() -> ArgMatches<'static> { .help("Enable transaction history (increases storage requirements)") ) ) + ) .get_matches() } diff --git a/src/utils/examples.rs b/src/utils/examples.rs index b2dae0a3..24e5ac4b 100644 --- a/src/utils/examples.rs +++ b/src/utils/examples.rs @@ -1,347 +1,348 @@ -//! CLI examples module -//! -//! This module provides practical usage examples for OSVM CLI commands, -//! organized by category to help users learn common workflows and best practices. - -use crate::utils::color; -use std::collections::HashMap; - -/// Category of examples -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub enum ExampleCategory { - /// Basic commands - Basic, - /// SVM management examples - SvmManagement, - /// Node deployment examples - NodeDeployment, - /// Node monitoring examples - NodeMonitoring, - /// Workflow examples (multiple commands) - Workflow, -} - -impl ExampleCategory { - /// Get the display name for a category - pub fn display_name(&self) -> &'static str { - match self { - ExampleCategory::Basic => "Basic Commands", - ExampleCategory::SvmManagement => "SVM Management", - ExampleCategory::NodeDeployment => "Node Deployment", - ExampleCategory::NodeMonitoring => "Node Monitoring and Management", - ExampleCategory::Workflow => "Common Workflows", - } - } - - /// Get a description for a category - pub fn description(&self) -> &'static str { - match self { - ExampleCategory::Basic => "Fundamental commands to get started with OSVM CLI", - ExampleCategory::SvmManagement => "Commands for managing and inspecting SVMs (Solana Virtual Machines)", - ExampleCategory::NodeDeployment => "Commands for deploying validator and RPC nodes to servers", - ExampleCategory::NodeMonitoring => "Commands for monitoring and managing deployed nodes", - ExampleCategory::Workflow => "Multi-step command sequences for common operations", - } - } -} - -/// Example command with explanation -#[derive(Debug, Clone)] -pub struct Example { - /// Title of the example - pub title: &'static str, - /// Command to run - pub command: &'static str, - /// Explanation of what the command does - pub explanation: &'static str, - /// Category this example belongs to - pub category: ExampleCategory, -} - -/// Get all examples -pub fn get_all_examples() -> Vec { - vec![ - // Basic Commands - Example { - title: "Run with increased verbosity", - command: "osvm -v svm list", - explanation: "Run a command with increased verbosity level. Use -vv or -vvv for even more details", - category: ExampleCategory::Basic, - }, - Example { - title: "Specify a different RPC URL", - command: "osvm --url https://api.mainnet-beta.solana.com svm list", - explanation: "Run a command using a specific Solana RPC endpoint", - category: ExampleCategory::Basic, - }, - Example { - title: "Disable colored output", - command: "osvm --no-color svm list", - explanation: "Run a command with colored output disabled", - category: ExampleCategory::Basic, - }, - - // SVM Management - Example { - title: "List all available SVMs", - command: "osvm svm list", - explanation: "Shows all SVMs available in the network with their basic details", - category: ExampleCategory::SvmManagement, - }, - Example { - title: "Get detailed information about an SVM", - command: "osvm svm get solana", - explanation: "Displays comprehensive information about the specified SVM including networks, requirements, and installation options", - category: ExampleCategory::SvmManagement, - }, - Example { - title: "Launch interactive SVM dashboard", - command: "osvm svm dashboard", - explanation: "Starts an interactive dashboard showing real-time information about all SVMs", - category: ExampleCategory::SvmManagement, - }, - Example { - title: "Install an SVM on a remote host", - command: "osvm svm install solana --host user@123.45.67.89", - explanation: "Installs the specified SVM on the remote host as a validator node on mainnet (default settings)", - category: ExampleCategory::SvmManagement, - }, - - // Node Deployment - Example { - title: "Deploy a validator node", - command: "osvm user@host.example.com --svm solana --node-type validator --network mainnet", - explanation: "Deploys a Solana validator node on the mainnet to the specified remote host", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy an RPC node on testnet", - command: "osvm user@host.example.com --svm solana --node-type rpc --network testnet", - explanation: "Deploys a Solana RPC node on the testnet to the specified remote host", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy a Sonic RPC node", - command: "osvm rpc sonic root@host.example.com --network mainnet", - explanation: "Deploys a Sonic RPC node on the mainnet to the specified remote host using the sonicfromnewyoke/solana-rpc repository", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy multiple SVMs to a single host", - command: "osvm user@host.example.com --svm solana,sonic,eclipse --node-type validator --network mainnet", - explanation: "Deploys validator nodes for multiple SVMs to a single host, all running on mainnet", - category: ExampleCategory::NodeDeployment, - }, - // Enhanced Solana Validator Deployment Examples - Example { - title: "Deploy an enhanced Solana validator node", - command: "osvm solana validator user@host.example.com --network mainnet", - explanation: "Deploys a Solana validator node with enhanced features on the mainnet", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy Solana validator with disk configuration", - command: "osvm solana validator user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1", - explanation: "Deploys a Solana validator with optimized 3-disk configuration (OS, Ledger, Accounts/Snapshots)", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy Solana validator with Jito client", - command: "osvm solana validator user@host.example.com --network mainnet --client-type jito --version v1.18.23-jito", - explanation: "Deploys a Solana validator using the Jito client with a specific version", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy Solana validator with hot-swap capability", - command: "osvm solana validator user@host.example.com --network mainnet --hot-swap", - explanation: "Deploys a Solana validator with hot-swap capability for high availability", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy fully-optimized Solana validator", - command: "osvm solana validator user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1 --client-type jito --hot-swap --metrics-config \"host=https://metrics.solana.com:8086,db=mainnet-beta\"", - explanation: "Deploys a Solana validator with all optimizations: disk configuration, Jito client, hot-swap capability, and metrics", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy enhanced Solana RPC node", - command: "osvm solana rpc user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1", - explanation: "Deploys a Solana RPC node with optimized disk configuration", - category: ExampleCategory::NodeDeployment, - }, - Example { - title: "Deploy Solana RPC node with transaction history", - command: "osvm solana rpc user@host.example.com --network mainnet --enable-history", - explanation: "Deploys a Solana RPC node with transaction history enabled (increases storage requirements)", - category: ExampleCategory::NodeDeployment, - }, - - // Node Monitoring - Example { - title: "List all deployed nodes", - command: "osvm nodes list", - explanation: "Shows all nodes currently managed by OSVM CLI", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Filter nodes by SVM type", - command: "osvm nodes list --svm solana", - explanation: "Lists only nodes running the specified SVM", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Filter nodes by network and status", - command: "osvm nodes list --network mainnet --status running", - explanation: "Lists only nodes on mainnet that are currently running", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Get node status", - command: "osvm nodes status solana-validator-mainnet-192.168.1.1", - explanation: "Checks and displays the current status of the specified node", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Get detailed node information", - command: "osvm nodes get solana-validator-mainnet-192.168.1.1", - explanation: "Shows comprehensive information about the specified node", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "View node logs", - command: "osvm nodes logs solana-validator-mainnet-192.168.1.1 --lines 200", - explanation: "Shows the last 200 lines of logs from the specified node", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Follow node logs in real-time", - command: "osvm nodes logs solana-validator-mainnet-192.168.1.1 --follow", - explanation: "Streams logs from the node in real-time (ctrl+c to exit)", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Launch interactive node dashboard", - command: "osvm nodes dashboard", - explanation: "Starts an interactive dashboard showing real-time information about all nodes", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Restart a node", - command: "osvm nodes restart solana-validator-mainnet-192.168.1.1", - explanation: "Safely restarts the specified node", - category: ExampleCategory::NodeMonitoring, - }, - Example { - title: "Stop a node", - command: "osvm nodes stop solana-validator-mainnet-192.168.1.1", - explanation: "Safely stops the specified node", - category: ExampleCategory::NodeMonitoring, - }, - - // Workflow Examples - Example { - title: "Set up a new validator node (full workflow)", - command: "osvm svm get solana\nosvm user@host.example.com --svm solana --node-type validator --network mainnet\nosvm nodes get solana-validator-mainnet-host.example.com\nosvm nodes logs solana-validator-mainnet-host.example.com --follow", - explanation: "Complete workflow: First check SVM info, then deploy a validator node, verify the deployment status, and monitor the logs as it starts up", - category: ExampleCategory::Workflow, - }, - Example { - title: "Migrate a validator to a new host", - command: "osvm nodes stop solana-validator-mainnet-oldhost.example.com\nosvm user@newhost.example.com --svm solana --node-type validator --network mainnet\nosvm nodes get solana-validator-mainnet-newhost.example.com", - explanation: "Workflow for migrating a validator: Stop the existing node, deploy a new one on the new host, and verify it's running correctly", - category: ExampleCategory::Workflow, - }, - ] -} - -/// Get examples by category -pub fn get_examples_by_category() -> HashMap> { - let mut categories = HashMap::new(); - - for example in get_all_examples() { - categories - .entry(example.category) - .or_insert_with(Vec::new) - .push(example); - } - - categories -} - -/// Display examples for all categories -pub fn display_all_examples() { - println!("\n{}", color::heading("OSVM CLI Examples")); - println!("{}", color::subheading("=================")); - - println!("\n{}", color::important("This command provides examples of common OSVM CLI usage patterns organized by category.")); - println!("Use the {} flag to see examples from a specific category.", color::command("--category")); - - let categories = get_examples_by_category(); - - // Determine the correct order to display categories in - let category_order = [ - ExampleCategory::Basic, - ExampleCategory::SvmManagement, - ExampleCategory::NodeDeployment, - ExampleCategory::NodeMonitoring, - ExampleCategory::Workflow, - ]; - - for category in category_order.iter() { - if let Some(examples) = categories.get(category) { - display_category(*category, examples); - } - } - - println!("\n{}", color::important("TIP: Add the -v flag to any command to see more detailed output.")); -} - -/// Display examples for a specific category -pub fn display_category(category: ExampleCategory, examples: &[Example]) { - println!("\n{}", color::heading(category.display_name())); - println!("{}", color::secondary(&"-".repeat(category.display_name().len()))); - println!("{}", color::important(category.description())); - println!(); - - for (i, example) in examples.iter().enumerate() { - println!("{} {}", color::key(&format!("{}.", i + 1)), color::bold(example.title)); - - // For workflow examples, split multi-line commands - if category == ExampleCategory::Workflow { - for (i, cmd) in example.command.split('\n').enumerate() { - println!(" {}: {}", color::secondary(&format!("Step {}", i + 1)), color::command(cmd)); - } - } else { - println!(" {}", color::command(example.command)); - } - - println!(" {}", color::secondary(example.explanation)); - println!(); - } -} - -/// Display examples for a specific category by name -pub fn display_category_by_name(category_name: &str) { - let category = match category_name.to_lowercase().as_str() { - "basic" => ExampleCategory::Basic, - "svm" | "svmmanagement" => ExampleCategory::SvmManagement, - "node" | "nodedeployment" | "deployment" => ExampleCategory::NodeDeployment, - "monitoring" | "nodemonitoring" => ExampleCategory::NodeMonitoring, - "workflow" | "workflows" => ExampleCategory::Workflow, - _ => { - println!("\n{}", color::error(&format!("Unknown category: {}", category_name))); - println!("Available categories: basic, svm, node, monitoring, workflow"); - return; - } - }; - - let categories = get_examples_by_category(); - - if let Some(examples) = categories.get(&category) { - println!("\n{}", color::heading("OSVM CLI Examples")); - println!("{}", color::subheading("=================")); - display_category(category, examples); - } else { - println!("\n{}", color::error(&format!("No examples found for category: {}", category_name))); - } -} + //! CLI examples module + //! + //! This module provides practical usage examples for OSVM CLI commands, + //! organized by category to help users learn common workflows and best practices. + + use crate::utils::color; + use std::collections::HashMap; + + /// Category of examples + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] + pub enum ExampleCategory { + /// Basic commands + Basic, + /// SVM management examples + SvmManagement, + /// Node deployment examples + NodeDeployment, + /// Node monitoring examples + NodeMonitoring, + /// Workflow examples (multiple commands) + Workflow, + } + + impl ExampleCategory { + /// Get the display name for a category + pub fn display_name(&self) -> &'static str { + match self { + ExampleCategory::Basic => "Basic Commands", + ExampleCategory::SvmManagement => "SVM Management", + ExampleCategory::NodeDeployment => "Node Deployment", + ExampleCategory::NodeMonitoring => "Node Monitoring and Management", + ExampleCategory::Workflow => "Common Workflows", + } + } + + /// Get a description for a category + pub fn description(&self) -> &'static str { + match self { + ExampleCategory::Basic => "Fundamental commands to get started with OSVM CLI", + ExampleCategory::SvmManagement => "Commands for managing and inspecting SVMs (Solana Virtual Machines)", + ExampleCategory::NodeDeployment => "Commands for deploying validator and RPC nodes to servers", + ExampleCategory::NodeMonitoring => "Commands for monitoring and managing deployed nodes", + ExampleCategory::Workflow => "Multi-step command sequences for common operations", + } + } + } + + /// Example command with explanation + #[derive(Debug, Clone)] + pub struct Example { + /// Title of the example + pub title: &'static str, + /// Command to run + pub command: &'static str, + /// Explanation of what the command does + pub explanation: &'static str, + /// Category this example belongs to + pub category: ExampleCategory, + } + + /// Get all examples + pub fn get_all_examples() -> Vec { + vec![ + // Basic Commands + Example { + title: "Run with increased verbosity", + command: "osvm -v svm list", + explanation: "Run a command with increased verbosity level. Use -vv or -vvv for even more details", + category: ExampleCategory::Basic, + }, + Example { + title: "Specify a different RPC URL", + command: "osvm --url https://api.mainnet-beta.solana.com svm list", + explanation: "Run a command using a specific Solana RPC endpoint", + category: ExampleCategory::Basic, + }, + Example { + title: "Disable colored output", + command: "osvm --no-color svm list", + explanation: "Run a command with colored output disabled", + category: ExampleCategory::Basic, + }, + + // SVM Management + Example { + title: "List all available SVMs", + command: "osvm svm list", + explanation: "Shows all SVMs available in the network with their basic details", + category: ExampleCategory::SvmManagement, + }, + Example { + title: "Get detailed information about an SVM", + command: "osvm svm get solana", + explanation: "Displays comprehensive information about the specified SVM including networks, requirements, and installation options", + category: ExampleCategory::SvmManagement, + }, + Example { + title: "Launch interactive SVM dashboard", + command: "osvm svm dashboard", + explanation: "Starts an interactive dashboard showing real-time information about all SVMs", + category: ExampleCategory::SvmManagement, + }, + Example { + title: "Install an SVM on a remote host", + command: "osvm svm install solana --host user@123.45.67.89", + explanation: "Installs the specified SVM on the remote host as a validator node on mainnet (default settings)", + category: ExampleCategory::SvmManagement, + }, + + // Node Deployment + Example { + title: "Deploy a validator node", + command: "osvm user@host.example.com --svm solana --node-type validator --network mainnet", + explanation: "Deploys a Solana validator node on the mainnet to the specified remote host", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy an RPC node on testnet", + command: "osvm user@host.example.com --svm solana --node-type rpc --network testnet", + explanation: "Deploys a Solana RPC node on the testnet to the specified remote host", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy a Sonic RPC node", + command: "osvm rpc sonic root@host.example.com --network mainnet", + explanation: "Deploys a Sonic RPC node on the mainnet to the specified remote host using the sonicfromnewyoke/solana-rpc repository", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy multiple SVMs to a single host", + command: "osvm user@host.example.com --svm solana,sonic,eclipse --node-type validator --network mainnet", + explanation: "Deploys validator nodes for multiple SVMs to a single host, all running on mainnet", + category: ExampleCategory::NodeDeployment, + }, + // Enhanced Solana Validator Deployment Examples + Example { + title: "Deploy an enhanced Solana validator node", + command: "osvm solana validator user@host.example.com --network mainnet", + explanation: "Deploys a Solana validator node with enhanced features on the mainnet", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy Solana validator with disk configuration", + command: "osvm solana validator user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1", + explanation: "Deploys a Solana validator with optimized 3-disk configuration (OS, Ledger, Accounts/Snapshots)", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy Solana validator with Jito client", + command: "osvm solana validator user@host.example.com --network mainnet --client-type jito --version v1.18.23-jito", + explanation: "Deploys a Solana validator using the Jito client with a specific version", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy Solana validator with hot-swap capability", + command: "osvm solana validator user@host.example.com --network mainnet --hot-swap", + explanation: "Deploys a Solana validator with hot-swap capability for high availability", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy fully-optimized Solana validator", + command: "osvm solana validator user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1 --client-type jito --hot-swap --metrics-config \"host=https://metrics.solana.com:8086,db=mainnet-beta\"", + explanation: "Deploys a Solana validator with all optimizations: disk configuration, Jito client, hot-swap capability, and metrics", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy enhanced Solana RPC node", + command: "osvm solana rpc user@host.example.com --network mainnet --ledger-disk /dev/nvme0n1 --accounts-disk /dev/nvme1n1", + explanation: "Deploys a Solana RPC node with optimized disk configuration", + category: ExampleCategory::NodeDeployment, + }, + Example { + title: "Deploy Solana RPC node with transaction history", + command: "osvm solana rpc user@host.example.com --network mainnet --enable-history", + explanation: "Deploys a Solana RPC node with transaction history enabled (increases storage requirements)", + category: ExampleCategory::NodeDeployment, + }, + + // Node Monitoring + Example { + title: "List all deployed nodes", + command: "osvm nodes list", + explanation: "Shows all nodes currently managed by OSVM CLI", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Filter nodes by SVM type", + command: "osvm nodes list --svm solana", + explanation: "Lists only nodes running the specified SVM", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Filter nodes by network and status", + command: "osvm nodes list --network mainnet --status running", + explanation: "Lists only nodes on mainnet that are currently running", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Get node status", + command: "osvm nodes status solana-validator-mainnet-192.168.1.1", + explanation: "Checks and displays the current status of the specified node", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Get detailed node information", + command: "osvm nodes get solana-validator-mainnet-192.168.1.1", + explanation: "Shows comprehensive information about the specified node", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "View node logs", + command: "osvm nodes logs solana-validator-mainnet-192.168.1.1 --lines 200", + explanation: "Shows the last 200 lines of logs from the specified node", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Follow node logs in real-time", + command: "osvm nodes logs solana-validator-mainnet-192.168.1.1 --follow", + explanation: "Streams logs from the node in real-time (ctrl+c to exit)", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Launch interactive node dashboard", + command: "osvm nodes dashboard", + explanation: "Starts an interactive dashboard showing real-time information about all nodes", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Restart a node", + command: "osvm nodes restart solana-validator-mainnet-192.168.1.1", + explanation: "Safely restarts the specified node", + category: ExampleCategory::NodeMonitoring, + }, + Example { + title: "Stop a node", + command: "osvm nodes stop solana-validator-mainnet-192.168.1.1", + explanation: "Safely stops the specified node", + category: ExampleCategory::NodeMonitoring, + }, + + // Workflow Examples + Example { + title: "Set up a new validator node (full workflow)", + command: "osvm svm get solana\nosvm user@host.example.com --svm solana --node-type validator --network mainnet\nosvm nodes get solana-validator-mainnet-host.example.com\nosvm nodes logs solana-validator-mainnet-host.example.com --follow", + explanation: "Complete workflow: First check SVM info, then deploy a validator node, verify the deployment status, and monitor the logs as it starts up", + category: ExampleCategory::Workflow, + }, + Example { + title: "Migrate a validator to a new host", + command: "osvm nodes stop solana-validator-mainnet-oldhost.example.com\nosvm user@newhost.example.com --svm solana --node-type validator --network mainnet\nosvm nodes get solana-validator-mainnet-newhost.example.com", + explanation: "Workflow for migrating a validator: Stop the existing node, deploy a new one on the new host, and verify it's running correctly", + category: ExampleCategory::Workflow, + }, + ] + } + + /// Get examples by category + pub fn get_examples_by_category() -> HashMap> { + let mut categories = HashMap::new(); + + for example in get_all_examples() { + categories + .entry(example.category) + .or_insert_with(Vec::new) + .push(example); + } + + categories + } + + /// Display examples for all categories + pub fn display_all_examples() { + println!("\n{}", color::heading("OSVM CLI Examples")); + println!("{}", color::subheading("=================")); + + println!("\n{}", color::important("This command provides examples of common OSVM CLI usage patterns organized by category.")); + println!("Use the {} flag to see examples from a specific category.", color::command("--category")); + + let categories = get_examples_by_category(); + + // Determine the correct order to display categories in + let category_order = [ + ExampleCategory::Basic, + ExampleCategory::SvmManagement, + ExampleCategory::NodeDeployment, + ExampleCategory::NodeMonitoring, + ExampleCategory::Workflow, + ]; + + for category in category_order.iter() { + if let Some(examples) = categories.get(category) { + display_category(*category, examples); + } + } + + println!("\n{}", color::important("TIP: Add the -v flag to any command to see more detailed output.")); + } + + /// Display examples for a specific category + pub fn display_category(category: ExampleCategory, examples: &[Example]) { + println!("\n{}", color::heading(category.display_name())); + println!("{}", color::secondary(&"-".repeat(category.display_name().len()))); + println!("{}", color::important(category.description())); + println!(); + + for (i, example) in examples.iter().enumerate() { + println!("{} {}", color::key(&format!("{}.", i + 1)), color::bold(example.title)); + + // For workflow examples, split multi-line commands + if category == ExampleCategory::Workflow { + for (i, cmd) in example.command.split('\n').enumerate() { + println!(" {}: {}", color::secondary(&format!("Step {}", i + 1)), color::command(cmd)); + } + } else { + println!(" {}", color::command(example.command)); + } + + println!(" {}", color::secondary(example.explanation)); + println!(); + } + } + + /// Display examples for a specific category by name + pub fn display_category_by_name(category_name: &str) { + let category = match category_name.to_lowercase().as_str() { + "basic" => ExampleCategory::Basic, + "svm" | "svmmanagement" => ExampleCategory::SvmManagement, + "node" | "nodedeployment" | "deployment" => ExampleCategory::NodeDeployment, + "monitoring" | "nodemonitoring" => ExampleCategory::NodeMonitoring, + "workflow" | "workflows" => ExampleCategory::Workflow, + _ => { + println!("\n{}", color::error(&format!("Unknown category: {}", category_name))); + println!("Available categories: basic, svm, node, monitoring, workflow"); + return; + } + }; + + let categories = get_examples_by_category(); + + if let Some(examples) = categories.get(&category) { + println!("\n{}", color::heading("OSVM CLI Examples")); + println!("{}", color::subheading("=================")); + display_category(category, examples); + } else { + println!("\n{}", color::error(&format!("No examples found for category: {}", category_name))); + } + } + diff --git a/tests/e2e/common.rs b/tests/e2e/common.rs index c98ad57b..a81b25f5 100644 --- a/tests/e2e/common.rs +++ b/tests/e2e/common.rs @@ -95,4 +95,4 @@ impl MockServer { .with_body(format!(r#"{{"error":"SVM not found: {}"}}"#, svm_name)) .create() } -} \ No newline at end of file +} diff --git a/tests/e2e/node_tests.rs b/tests/e2e/node_tests.rs index 3ccc32a7..7c45b022 100644 --- a/tests/e2e/node_tests.rs +++ b/tests/e2e/node_tests.rs @@ -1,4 +1,4 @@ -//! End-to-end tests for node-related commands +#![End-to-end tests for node-related commands use crate::tests::e2e::common::{run_osvm_command, run_osvm_command_string, output_contains, create_temp_dir, create_mock_config, MockServer}; use predicates::prelude::*; @@ -8,15 +8,15 @@ use serial_test::serial; #[serial] fn test_nodes_list() { let output = run_osvm_command_string(&["nodes", "list"]); - + // Verify the output contains expected headers assert!(output_contains(&output, "OSVM - Node Management")); assert!(output_contains(&output, "Managed SVM Nodes:")); - + // The output might show "No nodes are currently managed" if no nodes are configured // or it might show a list of nodes if some are configured assert!( - output_contains(&output, "No nodes are currently managed") || + output_contains(&output, "No nodes are currently managed") || output_contains(&output, "ID") && output_contains(&output, "SVM") && output_contains(&output, "TYPE") ); } @@ -27,15 +27,15 @@ fn test_nodes_list_with_filters() { // Test with network filter let output = run_osvm_command_string(&["nodes", "list", "--network", "mainnet"]); assert!(output_contains(&output, "OSVM - Node Management")); - + // Test with type filter let output = run_osvm_command_string(&["nodes", "list", "--type", "validator"]); assert!(output_contains(&output, "OSVM - Node Management")); - + // Test with status filter let output = run_osvm_command_string(&["nodes", "list", "--status", "running"]); assert!(output_contains(&output, "OSVM - Node Management")); - + // Test with JSON output let output = run_osvm_command_string(&["nodes", "list", "--json"]); // JSON output should start with a curly brace or square bracket @@ -50,7 +50,7 @@ fn test_nodes_dashboard() { let assert = run_osvm_command() .args(&["nodes", "dashboard"]) .assert(); - + // The dashboard might not work in a test environment, so we're just checking // that the command is recognized assert.stderr(predicate::str::contains("Unknown command").not()); @@ -62,7 +62,7 @@ fn test_nodes_get_invalid() { let assert = run_osvm_command() .args(&["nodes", "get", "invalid_node_id"]) .assert(); - + // Verify the command fails with a non-zero exit code assert.failure() .stderr(predicate::str::contains("Node not found").or(predicate::str::contains("Error:"))); @@ -73,16 +73,16 @@ fn test_nodes_get_invalid() { fn test_examples_command() { // Test the examples command let output = run_osvm_command_string(&["examples"]); - + // Verify the output contains examples - assert!(output_contains(&output, "OSVM CLI Examples") || + assert!(output_contains(&output, "OSVM CLI Examples") || output_contains(&output, "Available SVMs in the chain:") || output_contains(&output, "Basic Commands")); - + // Test examples with category filter let output = run_osvm_command_string(&["examples", "--category", "basic"]); assert!(output_contains(&output, "Basic Commands")); - + // Test listing categories let output = run_osvm_command_string(&["examples", "--list-categories"]); assert!(output_contains(&output, "Available example categories:")); @@ -93,16 +93,16 @@ fn test_examples_command() { fn test_verbose_output() { // Test with verbose flag let output = run_osvm_command_string(&["--verbose", "svm", "list"]); - + // Verbose output should include JSON RPC URL or other verbose information - assert!(output_contains(&output, "JSON RPC URL:") || + assert!(output_contains(&output, "JSON RPC URL:") || output_contains(&output, "Available SVMs in the chain:")); - + // Test with very verbose flag let output = run_osvm_command_string(&["-vv", "svm", "list"]); - + // Very verbose output should include keypair info or other very verbose information - assert!(output_contains(&output, "Using keypair:") || + assert!(output_contains(&output, "Using keypair:") || output_contains(&output, "Available SVMs in the chain:")); } @@ -111,7 +111,7 @@ fn test_verbose_output() { fn test_no_color_flag() { // Test with no-color flag let output = run_osvm_command_string(&["--no-color", "svm", "list"]); - + // Output should still contain the expected text, but without color codes assert!(output_contains(&output, "Available SVMs in the chain:")); } @@ -122,10 +122,10 @@ fn test_with_custom_config() { // Create a temporary directory and config file let temp_dir = create_temp_dir(); let config_path = create_mock_config(&temp_dir); - + // Run the command with the config file let output = run_osvm_command_string(&["-C", config_path.to_str().unwrap(), "nodes", "list"]); - + // Verify the output contains expected headers assert!(output_contains(&output, "OSVM - Node Management")); } @@ -137,10 +137,10 @@ fn test_help_command() { let assert = run_osvm_command() .arg("--help") .assert(); - + // Verify the output contains help information assert.success() .stdout(predicate::str::contains("USAGE:")) .stdout(predicate::str::contains("FLAGS:")) .stdout(predicate::str::contains("SUBCOMMANDS:")); -} \ No newline at end of file +} diff --git a/tests/e2e/svm_tests.rs b/tests/e2e/svm_tests.rs index 01bdef64..fb64c8ef 100644 --- a/tests/e2e/svm_tests.rs +++ b/tests/e2e/svm_tests.rs @@ -1,4 +1,4 @@ -//! End-to-end tests for SVM-related commands +#![End-to-end tests for SVM-related commands] use crate::tests::e2e::common::{run_osvm_command, run_osvm_command_string, output_contains}; @@ -89,4 +89,4 @@ fn test_svm_with_url() { // Verify the output contains expected headers assert!(output_contains(&output, "Available SVMs in the chain:")); -} \ No newline at end of file +}