-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use clap to manage args.
- Loading branch information
Showing
3 changed files
with
259 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |