Skip to content

Commit

Permalink
Merge pull request #1 from its-Lyn/dev
Browse files Browse the repository at this point in the history
Merge 0.6.2 in main
  • Loading branch information
EveMeows authored Jan 17, 2024
2 parents 10806a7 + 1ccd93e commit d4f31d6
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lymerge"
version = "0.1.0"
version = "0.6.2"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
24 changes: 22 additions & 2 deletions src/cli/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ pub fn create_arguments() -> Command {
Command::new("lymerge")
.about("A small and easy wrapper around emerge.")
.arg_required_else_help(true)
.version("0.6.2")
.disable_version_flag(true)
.arg(
Arg::new("version")
.long("version")
.short('v')
.help("Print program version.")
.action(ArgAction::SetTrue)
)
.subcommand(
Command::new("install")
.about("Download and install a package.")
.about("Download and install packages.")
.arg(
arg!(<PACKAGE> "The package to install.").num_args(0..)
arg!(<PACKAGES> "The package to install.").num_args(0..)
)
.arg(
Arg::new("nobin")
Expand All @@ -21,6 +30,13 @@ pub fn create_arguments() -> Command {
.action(ArgAction::SetTrue)
)
)
.subcommand(
Command::new("uninstall")
.about("Remove packages from your system.")
.arg(
arg!(<PACKAGES> "The packages to uninstall.").num_args(0..)
)
)
.subcommand(
Command::new("upgrade")
.about("Upgrade your system.")
Expand All @@ -46,4 +62,8 @@ pub fn create_arguments() -> Command {
arg!(<SEARCH> "The packages to search for.").num_args(0..)
)
)
.subcommand(
Command::new("info")
.about("Show portage info.")
)
}
80 changes: 72 additions & 8 deletions src/cli/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
use crate::utils::system_commands::{run_command, ensure_root};
use crate::logger::logger::log_string;
use crate::logger::logger::log_stdout;
use crate::logger::log_level::LogLevel;

use super::arguments::create_arguments;

pub fn parse() {
let command_matches = create_arguments().get_matches();

if command_matches.get_flag("version") {
println!("lymerge 0.6.2");
run_command("emerge", vec!["--version"]);

println!(r"
♡ ∩ ∩
(„•֊•„)♡ ♡
| ̄U U ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄|
| Have a good day.. |
| |
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄♡
");

std::process::exit(0);
}

match command_matches.subcommand() {
Some(("install", sub_matches)) => {
ensure_root();
Expand All @@ -18,9 +34,18 @@ pub fn parse() {
"--getbinpkg"
];

let packages: Vec<_> = sub_matches.get_many::<String>("PACKAGE").expect("weh :c").collect();
for package in packages.iter() {
emerge_arguments.push(package);
let packages = sub_matches.get_many::<String>("PACKAGES");
match packages {
Some(packages) => {
for package in packages {
emerge_arguments.push(package);
}
},

None => {
log_stdout(LogLevel::Error, "Please make sure to provide the package(s).", true);
std::process::exit(1);
}
}


Expand Down Expand Up @@ -53,14 +78,22 @@ pub fn parse() {
},

Some(("search", ser_matches)) => {
let packages: Vec<_> = ser_matches.get_many::<String>("SEARCH").expect(log_string(LogLevel::Error, "Please make sure to provide the package.").as_str()).collect();

let mut search_arguments = vec![
"--search"
];

for package in packages.iter() {
search_arguments.push(package);
let packages = ser_matches.get_many::<String>("SEARCH");
match packages {
Some(packages) => {
for package in packages {
search_arguments.push(package);
}
},

None => {
log_stdout(LogLevel::Error, "Please make sure to provide the package(s).", true);
std::process::exit(1);
}
}

run_command(
Expand All @@ -69,6 +102,37 @@ pub fn parse() {
)
},

Some(("uninstall", uns_matches)) => {
ensure_root();

let mut uninstall_arguments = vec![
"--ask",
"--deselect"
];

let packages = uns_matches.get_many::<String>("PACKAGES");
match packages {
Some(packages) => {
for package in packages {
uninstall_arguments.push(package);
}
},

None => {
log_stdout(LogLevel::Error, "Please make sure to provide the package(s).", true);
std::process::exit(1);
}
}

// Deselect, then depclean, makes it safer.
run_command("emerge", uninstall_arguments);
run_command("emerge", vec!["--ask", "--depclean"]);
},

Some(("info", _)) => {
run_command("emerge", vec!["--info"])
},

_ => unreachable!(),
}
}
7 changes: 0 additions & 7 deletions src/logger/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ use super::log_level::LogLevel;
use std::io;
use std::io::Write;

pub fn log_string(
log_level: LogLevel,
message: &str
) -> String {
return format!("{}: {}", log_level, message);
}

pub fn log_stdout(
log_level: LogLevel,
message: &str,
Expand Down
31 changes: 16 additions & 15 deletions src/utils/system_commands.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::process;
use std::path::Path;
use std::process::{Command, Stdio};
use std::process::Command;
use sudo::RunningAs;

use std::io::Write;

use crate::logger::log_level::LogLevel;
use crate::logger::logger::{log_stdout, log_string};
use crate::logger::logger::log_stdout;

pub fn check_os() {
if !cfg!(unix) || !Path::new("/etc/gentoo-release").exists() {
Expand All @@ -17,19 +15,22 @@ pub fn check_os() {

pub fn ensure_root() {
if sudo::check() == RunningAs::User {
log_stdout(LogLevel::Warning, "Lymerge needs to be run as root, elevating.", true);
sudo::escalate_if_needed().expect(log_string(LogLevel::Error, "Failed to escalate to root.").as_str());
log_stdout(LogLevel::Warning, "Lymerge needs to be ran as root, elevating.", true);

if let Err(_) = sudo::escalate_if_needed() {
log_stdout(LogLevel::Error, "Failed to escalate, aborting.", true);
std::process::exit(1);
}
}
}

pub fn run_command(name: &str, args: Vec<&str>) {
std::io::stdout().flush().expect("Failed to flush stdout.");

Command::new(name)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
pub fn run_command(name: &str, args: Vec<&str>) {
let cmd = Command::new(name)
.args(args)
.status()
.expect(log_string(LogLevel::Error, "Failed to run command.").as_str());
.status();

if let Err(e) = cmd {
log_stdout(LogLevel::Error, format!("Command failed to execute: {}, exitting.", e).as_str(), true);
std::process::exit(1);
}
}

0 comments on commit d4f31d6

Please sign in to comment.