Skip to content

Commit

Permalink
refactor: remove syntax.rs and merge syntax into commands file
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-TW committed Sep 19, 2024
1 parent b8678ce commit 2cc8038
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 242 deletions.
40 changes: 37 additions & 3 deletions src/command/info.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn info(manager: String, package: String, silent: bool) {
if package.is_empty() {
Expand All @@ -10,12 +10,46 @@ pub fn info(manager: String, package: String, silent: bool) {
println!("yu: Getting package information");
}

let mut info_cmd = syntax::gen_info_syntax(manager.clone())
let mut info_cmd = gen_info_syntax(manager.clone())
.arg(package)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()
.expect("Failed to execute info command");

info_cmd.wait().expect("Info command wasn't running");
}
}

pub fn gen_info_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
match manager.as_str() {
"apt" => {
command.arg("show");
}
"dnf" => {
command.arg("info");
}
"yum" => {
command.arg("info");
}
"pacman" => {
command.arg("-Qi");
}
"zypper" => {
command.arg("info");
}
"apk" => {
command.arg("info");
}
"portage" => {
command.arg("info");
}
"brew" => {
command.arg("info");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}
command
}
28 changes: 26 additions & 2 deletions src/command/install.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn install(manager: String, package: String, silent: bool, verbose: bool) {
if package.is_empty() {
Expand All @@ -10,7 +10,7 @@ pub fn install(manager: String, package: String, silent: bool, verbose: bool) {
println!("yu: Installing package: {}", package);
}

let mut install_cmd = syntax::gen_install_syntax(manager.clone())
let mut install_cmd = gen_install_syntax(manager.clone())
.arg(package)
.stdout(if verbose { std::process::Stdio::inherit() } else { std::process::Stdio::null() })
.stderr(std::process::Stdio::inherit())
Expand All @@ -19,3 +19,27 @@ pub fn install(manager: String, package: String, silent: bool, verbose: bool) {

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

pub fn gen_install_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
// add arguments
match manager.as_str() {
"apt" | "dnf" | "yum" | "pacman" | "zypper" => {
command.arg("install");
command.arg("-y");
}
"apk" => {
command.arg("add");
}
"portage" => {
command.arg("emerge");
}
"brew" => {
command.arg("install");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}
command
}
44 changes: 42 additions & 2 deletions src/command/list.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn list(manager: String) {
let mut list_cmd = syntax::gen_list_syntax(manager.clone())
let mut list_cmd = gen_list_syntax(manager.clone())
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()
.expect("yu: Failed to execute command");

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

pub fn gen_list_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
match manager.as_str() {
"apt" => {
command.arg("list");
command.arg("--installed");
}
"dnf" => {
command.arg("list");
command.arg("installed");
}
"yum" => {
command.arg("list");
command.arg("installed");
}
"pacman" => {
command.arg("-Q");
}
"zypper" => {
command.arg("search");
command.arg("--installed-only");
}
"apk" => {
command.arg("info");
command.arg("--installed");
}
"portage" => {
command.arg("--list");
command.arg("world");
}
"brew" => {
command.arg("list");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}
command
}
31 changes: 28 additions & 3 deletions src/command/reinstall.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn reinstall(manager: String, package: String, silent: bool, verbose: bool) {
if package.is_empty() {
Expand All @@ -10,12 +10,37 @@ pub fn reinstall(manager: String, package: String, silent: bool, verbose: bool)
println!("yu: Reinstalling package: {}", package);
}

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

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

pub fn gen_reinstall_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
match manager.as_str() {
"apt" | "dnf" | "yum" | "pacman" | "zypper" => {
command.arg("reinstall");
command.arg("-y");
}
"apk" => {
command.arg("add");
command.arg("-f");
}
"portage" => {
command.arg("emerge");
command.arg("--oneshot");
}
"brew" => {
command.arg("reinstall");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}
command
}
30 changes: 28 additions & 2 deletions src/command/uninstall.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn uninstall(manager: String, package: String, silent: bool, verbose: bool) {
if package.is_empty() {
Expand All @@ -10,7 +10,7 @@ pub fn uninstall(manager: String, package: String, silent: bool, verbose: bool)
println!("yu: Uninstalling package: {}", package);
}

let mut uninstall_cmd = syntax::gen_uninstall_syntax(manager.clone())
let mut uninstall_cmd = gen_uninstall_syntax(manager.clone())
.arg(package)
.stdout(if verbose { std::process::Stdio::inherit() } else { std::process::Stdio::null() })
.stderr(std::process::Stdio::inherit())
Expand All @@ -19,3 +19,29 @@ pub fn uninstall(manager: String, package: String, silent: bool, verbose: bool)

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

pub fn gen_uninstall_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
// add arguments
match manager.as_str() {
"apt" | "dnf" | "yum" | "pacman" | "zypper" => {
command.arg("remove");
command.arg("-y");
}
"apk" => {
command.arg("del");
}
"portage" => {
command.arg("emerge");
command.arg("--unmerge");
}
"brew" => {
command.arg("uninstall");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}

command
}
35 changes: 33 additions & 2 deletions src/command/update.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
use crate::syntax;
use crate::root::get_sudo;

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

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

pub fn gen_update_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
match manager.as_str() {
"apt" => {
command.arg("update");
}
"dnf" | "yum" => {
command.arg("check-update");
}
"pacman" => {
command.arg("-Sy");
}
"zypper" => {
command.arg("refresh");
}
"apk" => {
command.arg("update");
}
"portage" => {
command.arg("sync");
}
"brew" => {
command.arg("update");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}
command
}
37 changes: 35 additions & 2 deletions src/command/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::syntax;
use crate::root::get_sudo;

pub fn upgrade(manager: String, silent: bool, verbose: bool) {
if !silent {
println!("yu: Upgrading system");
}
let mut upgrade_cmd = syntax::gen_upgrade_syntax(manager.clone())
let mut upgrade_cmd = gen_upgrade_syntax(manager.clone())
.stdout(if verbose { std::process::Stdio::inherit() } else { std::process::Stdio::null() })
.stderr(std::process::Stdio::inherit())
.spawn()
Expand All @@ -16,3 +16,36 @@ pub fn upgrade(manager: String, silent: bool, verbose: bool) {
println!("yu: System upgraded");
}
}

pub fn gen_upgrade_syntax(manager: String) -> std::process::Command {
let mut command: std::process::Command = get_sudo(manager.clone());
match manager.as_str() {
"apt" | "dnf" | "yum" => {
command.arg("upgrade");
command.arg("-y");
}
"pacman" => {
command.arg("-Syu");
}
"zypper" => {
command.arg("update");
}
"apk" => {
command.arg("upgrade");
}
"portage" => {
command.arg("world");
command.arg("--update");
command.arg("--deep");
command.arg("--newuse");
}
"brew" => {
command.arg("upgrade");
}
_ => {
println!("Unknown package manager: {}", manager);
}
}

command
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{Arg, Command};

mod env;
mod syntax;
mod root;

mod command {
pub mod info;
Expand Down
12 changes: 12 additions & 0 deletions src/root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn get_sudo(manager: String) -> std::process::Command {
let command = match manager.as_str() {
"apt" | "dnf" | "yum" | "pacman" | "zypper" | "apk" | "portage" => {
let mut cmd = std::process::Command::new("sudo");
cmd.arg(manager.clone());
cmd
}
_ => std::process::Command::new(manager.clone()),
};

command
}
Loading

0 comments on commit 2cc8038

Please sign in to comment.