|
| 1 | +//! Hyperlane CLI |
| 2 | +//! |
| 3 | +//! A command-line tool for Hyperlane framework. |
| 4 | +
|
| 5 | +mod command; |
| 6 | +mod config; |
| 7 | + |
| 8 | +use {command::*, config::*}; |
| 9 | + |
| 10 | +use std::{ |
| 11 | + env::args, |
| 12 | + process::{ExitStatus, Stdio, exit}, |
| 13 | +}; |
| 14 | + |
| 15 | +use tokio::process::Command; |
| 16 | + |
| 17 | +/// Print help message |
| 18 | +fn print_help() { |
| 19 | + println!("hyperlane-cli [COMMAND] [OPTIONS]"); |
| 20 | + println!(); |
| 21 | + println!("Commands:"); |
| 22 | + println!(" fmt Format Rust code using cargo fmt"); |
| 23 | + println!(" watch Watch files and run cargo run using cargo-watch"); |
| 24 | + println!(" -h, --help Print this help message"); |
| 25 | + println!(" -v, --version Print version information"); |
| 26 | + println!(); |
| 27 | + println!("Fmt Options:"); |
| 28 | + println!(" --check Check formatting without making changes"); |
| 29 | + println!(" --manifest-path <PATH> Path to Cargo.toml"); |
| 30 | +} |
| 31 | + |
| 32 | +/// Print version |
| 33 | +fn print_version() { |
| 34 | + println!("hyperlane-cli {}", env!("CARGO_PKG_VERSION")); |
| 35 | +} |
| 36 | + |
| 37 | +/// Execute fmt command |
| 38 | +/// |
| 39 | +/// # Arguments |
| 40 | +/// - `args`: The parsed arguments |
| 41 | +/// |
| 42 | +/// # Returns |
| 43 | +/// - `Result<(), std::io::Error>`: Success or error |
| 44 | +async fn execute_fmt(args: &Args) -> Result<(), std::io::Error> { |
| 45 | + let mut cmd: Command = Command::new("cargo"); |
| 46 | + cmd.arg("fmt"); |
| 47 | + if args.check { |
| 48 | + cmd.arg("--check"); |
| 49 | + } |
| 50 | + if let Some(ref manifest_path) = args.manifest_path { |
| 51 | + cmd.arg("--manifest-path").arg(manifest_path); |
| 52 | + } |
| 53 | + cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()); |
| 54 | + let status: ExitStatus = cmd.status().await?; |
| 55 | + if !status.success() { |
| 56 | + return Err(std::io::Error::other("cargo fmt failed")); |
| 57 | + } |
| 58 | + Ok(()) |
| 59 | +} |
| 60 | + |
| 61 | +/// Check if cargo-watch is installed |
| 62 | +/// |
| 63 | +/// # Returns |
| 64 | +/// - `bool`: True if cargo-watch is available |
| 65 | +async fn is_cargo_watch_installed() -> bool { |
| 66 | + Command::new("cargo-watch") |
| 67 | + .arg("--version") |
| 68 | + .stdout(Stdio::null()) |
| 69 | + .stderr(Stdio::null()) |
| 70 | + .status() |
| 71 | + .await |
| 72 | + .is_ok_and(|status: ExitStatus| status.success()) |
| 73 | +} |
| 74 | + |
| 75 | +/// Install cargo-watch using cargo install |
| 76 | +/// |
| 77 | +/// # Returns |
| 78 | +/// - `Result<(), std::io::Error>`: Success or error |
| 79 | +async fn install_cargo_watch() -> Result<(), std::io::Error> { |
| 80 | + println!("cargo-watch not found, installing..."); |
| 81 | + let mut cmd: Command = Command::new("cargo"); |
| 82 | + cmd.arg("install").arg("cargo-watch"); |
| 83 | + cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()); |
| 84 | + let status: ExitStatus = cmd.status().await?; |
| 85 | + if !status.success() { |
| 86 | + return Err(std::io::Error::other("failed to install cargo-watch")); |
| 87 | + } |
| 88 | + Ok(()) |
| 89 | +} |
| 90 | + |
| 91 | +/// Execute watch command using cargo-watch |
| 92 | +/// |
| 93 | +/// # Returns |
| 94 | +/// - `Result<(), std::io::Error>`: Success or error |
| 95 | +async fn execute_watch() -> Result<(), std::io::Error> { |
| 96 | + if !is_cargo_watch_installed().await { |
| 97 | + install_cargo_watch().await?; |
| 98 | + } |
| 99 | + let mut cmd: Command = Command::new("cargo-watch"); |
| 100 | + cmd.arg("--clear") |
| 101 | + .arg("--skip-local-deps") |
| 102 | + .arg("-q") |
| 103 | + .arg("-x") |
| 104 | + .arg("run"); |
| 105 | + cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit()); |
| 106 | + let status: ExitStatus = cmd.status().await?; |
| 107 | + if !status.success() { |
| 108 | + return Err(std::io::Error::other("cargo-watch failed")); |
| 109 | + } |
| 110 | + Ok(()) |
| 111 | +} |
| 112 | + |
| 113 | +#[tokio::main] |
| 114 | +async fn main() { |
| 115 | + let args: Args = parse_args(); |
| 116 | + match args.command { |
| 117 | + CommandType::Fmt => { |
| 118 | + if let Err(error) = execute_fmt(&args).await { |
| 119 | + eprintln!("fmt failed: {error}"); |
| 120 | + exit(1); |
| 121 | + } |
| 122 | + } |
| 123 | + CommandType::Watch => { |
| 124 | + if let Err(error) = execute_watch().await { |
| 125 | + eprintln!("watch failed: {error}"); |
| 126 | + exit(1); |
| 127 | + } |
| 128 | + } |
| 129 | + CommandType::Help => print_help(), |
| 130 | + CommandType::Version => print_version(), |
| 131 | + } |
| 132 | +} |
0 commit comments