commit
e46f781ec9
@ -1,25 +1,21 @@
|
|||||||
use rusqlite::Connection;
|
|
||||||
use std::env;
|
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
use crate::internal::exit_code::AppExitCode;
|
use crate::internal::exit_code::AppExitCode;
|
||||||
use crate::internal::rpc::Package;
|
use crate::internal::rpc::Package;
|
||||||
use crate::{crash, log, Options};
|
use crate::{crash, log, Options};
|
||||||
|
|
||||||
|
use super::get_database_connection;
|
||||||
|
|
||||||
pub fn add(pkg: Package, options: Options) {
|
pub fn add(pkg: Package, options: Options) {
|
||||||
let conn = Connection::open(Path::new(&format!(
|
let conn = get_database_connection();
|
||||||
"{}/.local/share/ame/db.sqlite",
|
|
||||||
env::var("HOME").unwrap()
|
|
||||||
)))
|
|
||||||
.expect("Couldn't connect to database");
|
|
||||||
|
|
||||||
if options.verbosity >= 1 {
|
if options.verbosity >= 1 {
|
||||||
log(format!("Adding package {} to database", pkg.name));
|
log!("Adding package {} to database", pkg.name);
|
||||||
}
|
}
|
||||||
|
let pkg_description = pkg
|
||||||
|
.description
|
||||||
|
.unwrap_or_else(|| "No description found.".parse().unwrap());
|
||||||
conn.execute("INSERT OR REPLACE INTO packages (name, version, description, depends, make_depends) VALUES (?1, ?2, ?3, ?4, ?5)",
|
conn.execute("INSERT OR REPLACE INTO packages (name, version, description, depends, make_depends) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||||
[&pkg.name, &pkg.version, &pkg.description.unwrap_or_else(|| "No description found.".parse().unwrap()), &pkg.depends.join(" "), &pkg.make_depends.join(" ")],
|
[&pkg.name, &pkg.version, &pkg_description, &pkg.depends.join(" "), &pkg.make_depends.join(" ")],
|
||||||
).unwrap_or_else(|e|
|
).unwrap_or_else(|e|
|
||||||
crash(format!("Failed adding package {} to the database: {}", pkg.name, e), AppExitCode::FailedAddingPkg)
|
crash!(AppExitCode::FailedAddingPkg, "Failed adding package {} to the database: {}", pkg.name, e)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
|
use std::{env, path::PathBuf};
|
||||||
|
|
||||||
|
pub mod add;
|
||||||
|
pub mod initialise;
|
||||||
|
pub mod query;
|
||||||
|
pub mod remove;
|
||||||
|
|
||||||
pub use add::*;
|
pub use add::*;
|
||||||
pub use initialise::*;
|
pub use initialise::*;
|
||||||
pub use query::*;
|
pub use query::*;
|
||||||
pub use remove::*;
|
pub use remove::*;
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
mod add;
|
fn get_database_connection() -> Connection {
|
||||||
mod initialise;
|
let db_path = format!("{}/.local/share/ame/db.sqlite", env::var("HOME").unwrap());
|
||||||
mod query;
|
Connection::open(PathBuf::from(db_path)).expect("Couldn't connect to database")
|
||||||
mod remove;
|
}
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
use std::io;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::process::exit;
|
|
||||||
use std::time::UNIX_EPOCH;
|
|
||||||
|
|
||||||
use crate::internal::exit_code::AppExitCode;
|
|
||||||
use crate::{internal, uwu};
|
|
||||||
|
|
||||||
pub fn info<S: ToString>(msg: S) {
|
|
||||||
let a = msg.to_string();
|
|
||||||
let a = if internal::uwu_enabled() { uwu!(&a) } else { a };
|
|
||||||
|
|
||||||
println!("\x1b[2;22;35m❖\x1b[0m \x1b[1;37m{}\x1b[0m", a)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn warn<S: ToString>(msg: S) {
|
|
||||||
let a = msg.to_string();
|
|
||||||
let a = if internal::uwu_enabled() { uwu!(&a) } else { a };
|
|
||||||
|
|
||||||
println!("\x1b[2;22;33m!\x1b[0m \x1b[1;37m{}\x1b[0m", a)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn crash<S: ToString>(msg: S, exit_code: AppExitCode) -> ! {
|
|
||||||
let a = msg.to_string();
|
|
||||||
let a = if internal::uwu_enabled() { uwu!(&a) } else { a };
|
|
||||||
|
|
||||||
println!("\x1b[2;22;31m❌:\x1b[0m \x1b[1;91m{}\x1b[0m", a);
|
|
||||||
exit(exit_code as i32);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn log<S: ToString>(msg: S) {
|
|
||||||
let a = msg.to_string();
|
|
||||||
let a = if internal::uwu_enabled() && internal::uwu_debug_enabled() {
|
|
||||||
uwu!(&a)
|
|
||||||
} else {
|
|
||||||
a
|
|
||||||
};
|
|
||||||
|
|
||||||
eprintln!(
|
|
||||||
"{} {}",
|
|
||||||
std::time::SystemTime::now()
|
|
||||||
.duration_since(UNIX_EPOCH)
|
|
||||||
.unwrap()
|
|
||||||
.as_secs(),
|
|
||||||
a
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn prompt<S: ToString>(a: S, b: bool) -> bool {
|
|
||||||
let a = a.to_string();
|
|
||||||
let default = ["[Y/n]", "[y/N]"];
|
|
||||||
let i = if b { 0 } else { 1 };
|
|
||||||
|
|
||||||
let a = if internal::uwu_enabled() { uwu!(&a) } else { a };
|
|
||||||
|
|
||||||
print!(
|
|
||||||
"\x1b[2;22;35m?\x1b[0m \x1b[1;37m{}\x1b[0m \x1b[2;22;37m{}\x1b[0m: ",
|
|
||||||
a, default[i]
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut yn: String = String::new();
|
|
||||||
|
|
||||||
io::stdout().flush().ok();
|
|
||||||
let _ = std::io::stdin().read_line(&mut yn);
|
|
||||||
|
|
||||||
if yn.trim().to_lowercase() == "n" || yn.trim().to_lowercase() == "no" {
|
|
||||||
false
|
|
||||||
} else if yn.trim().to_lowercase() == "y" || yn.trim().to_lowercase() == "yes" {
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
b
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,139 @@
|
|||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::process::exit;
|
||||||
|
use std::time::UNIX_EPOCH;
|
||||||
|
|
||||||
|
use crate::internal::exit_code::AppExitCode;
|
||||||
|
use crate::{internal, uwu};
|
||||||
|
use colored::*;
|
||||||
|
|
||||||
|
const OK_SYMBOL: &str = "❖";
|
||||||
|
const ERR_SYMBOL: &str = "❌";
|
||||||
|
const WARN_SYMBOL: &str = "!";
|
||||||
|
const PROMPT_SYMBOL: &str = "?";
|
||||||
|
|
||||||
|
const PROMPT_YN_DEFAULT_TRUE: &str = "[Y/n]";
|
||||||
|
const PROMPT_YN_DEFAULT_FALSE: &str = "[y/N]";
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! info {
|
||||||
|
($($arg:tt)+) => {
|
||||||
|
$crate::internal::utils::log_info(format!($($arg)+))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! warn {
|
||||||
|
($($arg:tt)+) => {
|
||||||
|
$crate::internal::utils::log_warn(format!($($arg)+))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! crash {
|
||||||
|
($exit_code:expr, $($arg:tt)+) => {
|
||||||
|
$crate::internal::utils::log_and_crash(format!($($arg)+), $exit_code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! log {
|
||||||
|
($($arg:tt)+) => {
|
||||||
|
$crate::internal::utils::log_debug(format!($($arg)+))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! prompt {
|
||||||
|
(default $default:expr, $($arg:tt)+) => {
|
||||||
|
$crate::internal::utils::prompt_yn(format!($($arg)+), $default)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_info<S: ToString>(msg: S) {
|
||||||
|
let msg = msg.to_string();
|
||||||
|
let msg = if internal::uwu_enabled() {
|
||||||
|
uwu!(&msg)
|
||||||
|
} else {
|
||||||
|
msg
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{} {}", OK_SYMBOL.purple(), msg.bold())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_warn<S: ToString>(msg: S) {
|
||||||
|
let msg = msg.to_string();
|
||||||
|
let msg = if internal::uwu_enabled() {
|
||||||
|
uwu!(&msg)
|
||||||
|
} else {
|
||||||
|
msg
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{} {}", WARN_SYMBOL.yellow(), msg.bold())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_and_crash<S: ToString>(msg: S, exit_code: AppExitCode) -> ! {
|
||||||
|
let msg = msg.to_string();
|
||||||
|
let msg = if internal::uwu_enabled() {
|
||||||
|
uwu!(&msg)
|
||||||
|
} else {
|
||||||
|
msg
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{}: {}", ERR_SYMBOL.red().bold(), msg.red().bold());
|
||||||
|
exit(exit_code as i32);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn log_debug<S: ToString>(msg: S) {
|
||||||
|
let msg = msg.to_string();
|
||||||
|
let msg = if internal::uwu_enabled() && internal::uwu_debug_enabled() {
|
||||||
|
uwu!(&msg)
|
||||||
|
} else {
|
||||||
|
msg
|
||||||
|
};
|
||||||
|
|
||||||
|
eprintln!(
|
||||||
|
"{} {}",
|
||||||
|
std::time::SystemTime::now()
|
||||||
|
.duration_since(UNIX_EPOCH)
|
||||||
|
.unwrap()
|
||||||
|
.as_secs(),
|
||||||
|
msg
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prompt_yn<S: ToString>(question: S, default_true: bool) -> bool {
|
||||||
|
let question = question.to_string();
|
||||||
|
|
||||||
|
let yn_prompt = if default_true {
|
||||||
|
PROMPT_YN_DEFAULT_TRUE
|
||||||
|
} else {
|
||||||
|
PROMPT_YN_DEFAULT_FALSE
|
||||||
|
};
|
||||||
|
|
||||||
|
let question = if internal::uwu_enabled() {
|
||||||
|
uwu!(&question)
|
||||||
|
} else {
|
||||||
|
question
|
||||||
|
};
|
||||||
|
|
||||||
|
print!(
|
||||||
|
"{} {} {}: ",
|
||||||
|
PROMPT_SYMBOL.purple(),
|
||||||
|
question.bold(),
|
||||||
|
yn_prompt
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut yn: String = String::new();
|
||||||
|
|
||||||
|
io::stdout().flush().ok();
|
||||||
|
let _ = std::io::stdin().read_line(&mut yn);
|
||||||
|
|
||||||
|
if yn.trim().to_lowercase() == "n" || yn.trim().to_lowercase() == "no" {
|
||||||
|
false
|
||||||
|
} else if yn.trim().to_lowercase() == "y" || yn.trim().to_lowercase() == "yes" {
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
default_true
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue