Skip to content

Commit a5b48de

Browse files
committed
feat: v0.1.2
1 parent a367092 commit a5b48de

14 files changed

Lines changed: 188 additions & 104 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hyperlane-cli"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["root@ltpp.vip"]

src/command/enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// Available commands
22
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
3-
pub enum CommandType {
3+
pub(crate) enum CommandType {
44
/// Format code using cargo fmt
55
Fmt,
66
/// Watch files using cargo-watch

src/command/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
mod r#enum;
22

3-
pub use r#enum::*;
3+
pub(crate) use r#enum::*;

src/config/fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::*;
55
/// # Returns
66
///
77
/// - `Args`: Parsed arguments
8-
pub fn parse_args() -> Args {
8+
pub(crate) fn parse_args() -> Args {
99
let raw_args: Vec<String> = args().collect();
1010
let mut command: CommandType = CommandType::Help;
1111
let mut check: bool = false;

src/config/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mod r#fn;
22
mod r#struct;
33

4-
pub use {r#fn::*, r#struct::*};
4+
pub(crate) use {r#fn::*, r#struct::*};

src/fmt/fn.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use crate::*;
2+
3+
/// Check if cargo-clippy is installed
4+
///
5+
/// # Returns
6+
///
7+
/// - `bool`: True if cargo-clippy is available
8+
async fn is_cargo_clippy_installed() -> bool {
9+
Command::new("cargo")
10+
.arg("clippy")
11+
.arg("--version")
12+
.stdout(Stdio::null())
13+
.stderr(Stdio::null())
14+
.status()
15+
.await
16+
.is_ok_and(|status: ExitStatus| status.success())
17+
}
18+
19+
/// Install cargo-clippy using rustup
20+
///
21+
/// # Returns
22+
///
23+
/// - `Result<(), std::io::Error>`: Success or error
24+
async fn install_cargo_clippy() -> Result<(), std::io::Error> {
25+
println!("cargo-clippy not found, installing...");
26+
let mut cmd: Command = Command::new("rustup");
27+
cmd.arg("component").arg("add").arg("clippy");
28+
cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
29+
let status: ExitStatus = cmd.status().await?;
30+
if !status.success() {
31+
return Err(std::io::Error::other("failed to install cargo-clippy"));
32+
}
33+
Ok(())
34+
}
35+
36+
/// Execute clippy fix command
37+
///
38+
/// # Arguments
39+
/// - `args`: The parsed arguments
40+
///
41+
/// # Returns
42+
/// - `Result<(), std::io::Error>`: Success or error
43+
async fn execute_clippy_fix(args: &Args) -> Result<(), std::io::Error> {
44+
if !is_cargo_clippy_installed().await {
45+
install_cargo_clippy().await?;
46+
}
47+
let mut cmd: Command = Command::new("cargo");
48+
cmd.arg("clippy")
49+
.arg("--fix")
50+
.arg("--workspace")
51+
.arg("--all-targets")
52+
.arg("--allow-dirty");
53+
if let Some(ref manifest_path) = args.manifest_path {
54+
cmd.arg("--manifest-path").arg(manifest_path);
55+
}
56+
cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
57+
let status: ExitStatus = cmd.status().await?;
58+
if !status.success() {
59+
return Err(std::io::Error::other("cargo clippy --fix failed"));
60+
}
61+
Ok(())
62+
}
63+
64+
/// Execute fmt command
65+
///
66+
/// # Arguments
67+
/// - `args`: The parsed arguments
68+
///
69+
/// # Returns
70+
///
71+
/// - `Result<(), std::io::Error>`: Success or error
72+
pub(crate) async fn execute_fmt(args: &Args) -> Result<(), std::io::Error> {
73+
let mut cmd: Command = Command::new("cargo");
74+
cmd.arg("fmt");
75+
if args.check {
76+
cmd.arg("--check");
77+
}
78+
if let Some(ref manifest_path) = args.manifest_path {
79+
cmd.arg("--manifest-path").arg(manifest_path);
80+
}
81+
cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
82+
let status: ExitStatus = cmd.status().await?;
83+
if !status.success() {
84+
return Err(std::io::Error::other("cargo fmt failed"));
85+
}
86+
if !args.check {
87+
execute_clippy_fix(args).await?;
88+
}
89+
Ok(())
90+
}

src/fmt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod r#fn;
2+
3+
pub(crate) use r#fn::*;

src/help/fn.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Print help message
2+
pub(crate) fn print_help() {
3+
println!("hyperlane-cli [COMMAND] [OPTIONS]");
4+
println!();
5+
println!("Commands:");
6+
println!(" fmt Format Rust code using cargo fmt");
7+
println!(" watch Watch files and run cargo run using cargo-watch");
8+
println!(" -h, --help Print this help message");
9+
println!(" -v, --version Print version information");
10+
println!();
11+
println!("Fmt Options:");
12+
println!(" --check Check formatting without making changes");
13+
println!(" --manifest-path <PATH> Path to Cargo.toml");
14+
}

src/help/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod r#fn;
2+
3+
pub(crate) use r#fn::*;

src/main.rs

Lines changed: 7 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -4,111 +4,19 @@
44
55
mod command;
66
mod config;
7+
mod fmt;
8+
mod help;
9+
mod version;
10+
mod watch;
711

8-
use {command::*, config::*};
12+
pub(crate) use {command::*, config::*, fmt::*, help::*, version::*, watch::*};
913

10-
use std::{
14+
pub(crate) use std::{
1115
env::args,
1216
process::{ExitStatus, Stdio, exit},
1317
};
1418

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-
}
19+
pub(crate) use tokio::process::Command;
11220

11321
#[tokio::main]
11422
async fn main() {

0 commit comments

Comments
 (0)