Skip to content

Commit

Permalink
feat: Add --verbose and --silent
Browse files Browse the repository at this point in the history
Use clap to manage args.
  • Loading branch information
Young-TW committed Aug 21, 2024
1 parent e3e75ff commit 124c114
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 22 deletions.
193 changes: 193 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = { version = "4.0", features = ["derive"] }
87 changes: 65 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,78 +1,121 @@
use clap::{Arg, Command};
use std::process::Stdio;

mod env;
mod syntax;

fn main() {
let args: Vec<String> = std::env::args().collect();
let matches = Command::new("yu")
.version("0.1.0")
.about("A simple package manager wrapper")
.arg(
Arg::new("command")
.help("The command to execute")
.required(false)
.index(1),
)
.arg(
Arg::new("package")
.help("The package to install or uninstall")
.required(false)
.index(2),
)
.arg(
Arg::new("silent")
.long("silent")
.help("Run the command silently")
.action(clap::ArgAction::SetTrue),
)
.arg(
Arg::new("verbose")
.long("verbose")
.help("Run the command with verbose output")
.action(clap::ArgAction::SetTrue),
)
.get_matches();

let package_manager = env::detect_package_manager();
if args.len() < 2 {
upgrade(package_manager.clone());
return;
}

match args[1].as_str() {
"install" => install(package_manager.clone(), args[2].clone()),
"uninstall" => uninstall(package_manager.clone(), args[2].clone()),
"upgrade" => upgrade(package_manager.clone()),
// 檢查是否提供命令,若無則默認為 "upgrade"
let command = matches.get_one::<String>("command").map(|s| s.as_str()).unwrap_or("upgrade");
let package = matches.get_one::<String>("package").unwrap_or(&"".to_string()).to_string();
let silent = *matches.get_one::<bool>("silent").unwrap_or(&false);
let verbose = *matches.get_one::<bool>("verbose").unwrap_or(&false);

match command {
"install" => install(package_manager.clone(), package, silent, verbose),
"uninstall" => uninstall(package_manager.clone(), package, silent, verbose),
"upgrade" => upgrade(package_manager.clone(), silent, verbose),
_ => {
println!("Unknown command: {}", args[1]);
println!("Unknown command: {}", command);
}
}
}

fn install(manager: String, package: String) {
fn install(manager: String, package: String, silent: bool, verbose: bool) {
if package.is_empty() {
println!("Usage: install <package>");
return;
}

println!("yu: Installing package: {}", package);
if !silent {
println!("yu: Installing package: {}", package);
}

let mut cmd = syntax::gen_install_syntax(manager.clone())
.arg(package)
.stdout(Stdio::inherit())
.stdout(if verbose { Stdio::inherit() } else { Stdio::null() })
.stderr(Stdio::inherit())
.spawn()
.expect("yu: Failed to execute command");

cmd.wait().expect("Command wasn't running");
}

fn uninstall(manager: String, package: String) {
fn uninstall(manager: String, package: String, silent: bool, verbose: bool) {
if package.is_empty() {
println!("Usage: uninstall <package>");
return;
}

println!("yu: Uninstalling package: {}", package);
if !silent {
println!("yu: Uninstalling package: {}", package);
}

let mut cmd = syntax::gen_uninstall_syntax(manager.clone())
.arg(package)
.stdout(Stdio::inherit())
.stdout(if verbose { Stdio::inherit() } else { Stdio::null() })
.stderr(Stdio::inherit())
.spawn()
.expect("yu: Failed to execute command");

cmd.wait().expect("Command wasn't running");
}

fn upgrade(manager: String) {
println!("yu: Updating system");
fn upgrade(manager: String, silent: bool, verbose: bool) {
if !silent {
println!("yu: Updating system");
}
let mut update_cmd = syntax::gen_update_syntax(manager.clone())
.stdout(Stdio::inherit())
.stdout(if verbose { Stdio::inherit() } else { Stdio::null() })
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute update command");

update_cmd.wait().expect("Update command wasn't running");

println!("yu: Upgrading system");
if !silent {
println!("yu: Upgrading system");
}
let mut upgrade_cmd = syntax::gen_upgrade_syntax(manager.clone())
.stdout(Stdio::inherit())
.stdout(if verbose { Stdio::inherit() } else { Stdio::null() })
.stderr(Stdio::inherit())
.spawn()
.expect("Failed to execute upgrade command");

upgrade_cmd.wait().expect("Upgrade command wasn't running");

println!("yu: System upgraded");
if !silent {
println!("yu: System upgraded");
}
}

0 comments on commit 124c114

Please sign in to comment.