everything pretty much
parent
79340f2e6b
commit
bba02caf7d
@ -1,7 +1,23 @@
|
||||
use crate::internal::rpc::Package;
|
||||
use crate::Options;
|
||||
use rusqlite::Connection;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[allow(unused_variables)]
|
||||
pub fn add(a: String, options: Options) {
|
||||
// TODO: the actual database code lmao
|
||||
pub fn add(pkg: Package, options: Options) {
|
||||
let conn = Connection::open(Path::new(&format!(
|
||||
"{}/.local/share/ame/db.sqlite",
|
||||
env::var("HOME").unwrap()
|
||||
)))
|
||||
.expect("Couldn't connect to database.");
|
||||
|
||||
if options.verbosity >= 1 {
|
||||
eprintln!("Adding package {} to database", pkg.name);
|
||||
}
|
||||
|
||||
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(" ")]
|
||||
).unwrap_or_else(|e| {
|
||||
panic!("Failed adding package {} to the database: {}", pkg.name, e);
|
||||
});
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
use crate::Options;
|
||||
use rusqlite::Connection;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn init(options: Options) {
|
||||
let path = format!("{}/.local/share/ame/db.sqlite", env::var("HOME").unwrap());
|
||||
let dbpath = Path::new(&path);
|
||||
let verbosity = options.verbosity;
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Creating database at {}", &path);
|
||||
}
|
||||
|
||||
let conn =
|
||||
Connection::open(dbpath).expect("Couldn't create database at ~/.local/share/ame/db.sqlite");
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Populating database with table")
|
||||
}
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE packages (
|
||||
name TEXT PRIMARY KEY NOT NULL,
|
||||
version TEXT NOT NULL,
|
||||
description TEXT,
|
||||
depends BLOB,
|
||||
make_depends BLOB
|
||||
)",
|
||||
[],
|
||||
)
|
||||
.unwrap_or_else(|e| {
|
||||
panic!("Couldn't initialise database: {}", e);
|
||||
});
|
||||
}
|
@ -1,8 +1,23 @@
|
||||
use crate::internal::rpc::Package;
|
||||
use crate::Options;
|
||||
|
||||
mod add;
|
||||
mod initialise;
|
||||
mod query;
|
||||
mod remove;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn add(a: String, options: Options) {
|
||||
pub fn add(a: Package, options: Options) {
|
||||
add::add(a, options);
|
||||
}
|
||||
|
||||
pub fn remove(a: &str, options: Options) {
|
||||
remove::remove(a, options);
|
||||
}
|
||||
|
||||
pub fn init(options: Options) {
|
||||
initialise::init(options);
|
||||
}
|
||||
|
||||
pub fn query(a: &str, options: Options) -> Vec<Package> {
|
||||
query::query(a, options)
|
||||
}
|
||||
|
@ -0,0 +1,64 @@
|
||||
use crate::internal::rpc::Package;
|
||||
use crate::Options;
|
||||
use rusqlite::Connection;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn query(a: &str, options: Options) -> Vec<Package> {
|
||||
let verbosity = options.verbosity;
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Connecting to database");
|
||||
}
|
||||
|
||||
let conn = Connection::open(Path::new(&format!(
|
||||
"{}/.local/share/ame/db.sqlite",
|
||||
env::var("HOME").unwrap()
|
||||
)))
|
||||
.expect("Couldn't connect to database.");
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Querying database for input")
|
||||
}
|
||||
|
||||
let mut rs = conn
|
||||
.prepare("SELECT name, version, description, depends, make_depends FROM packages WHERE name LIKE \"?\"")
|
||||
.unwrap();
|
||||
let packages_iter = rs
|
||||
.query_map([a], |row| {
|
||||
Ok(Package {
|
||||
name: row.get(0).unwrap(),
|
||||
version: row.get(1).unwrap(),
|
||||
description: row.get(2).unwrap(),
|
||||
depends: row
|
||||
.get::<usize, String>(3)
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.map(|s| s.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
make_depends: row
|
||||
.get::<usize, String>(4)
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.map(|s| s.to_string())
|
||||
.collect::<Vec<String>>(),
|
||||
})
|
||||
})
|
||||
.expect("Couldn't query database for packages.");
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Retrieved results");
|
||||
}
|
||||
|
||||
let mut results: Vec<Package> = vec![];
|
||||
|
||||
for package in packages_iter {
|
||||
results.push(package.unwrap());
|
||||
}
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Collected results")
|
||||
}
|
||||
|
||||
results
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
use crate::Options;
|
||||
use rusqlite::Connection;
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn remove(pkg: &str, options: Options) {
|
||||
let conn = Connection::open(Path::new(&format!(
|
||||
"{}/.local/share/ame/db.sqlite",
|
||||
env::var("HOME").unwrap()
|
||||
)))
|
||||
.expect("Couldn't connect to database.");
|
||||
|
||||
let verbosity = options.verbosity;
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Removing package {} from database", pkg);
|
||||
}
|
||||
|
||||
conn.execute(
|
||||
"DELETE FROM packages
|
||||
WHERE EXISTS
|
||||
(SELECT *
|
||||
FROM packages
|
||||
WHERE name = ?);",
|
||||
[pkg],
|
||||
)
|
||||
.expect("Couldn't delete package from database.");
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
use crate::internal::rpc::rpcinfo;
|
||||
use crate::operations::aur_install::aur_install;
|
||||
use crate::Options;
|
||||
use runas::Command;
|
||||
|
||||
pub fn upgrade(options: Options) {
|
||||
let verbosity = options.verbosity;
|
||||
let noconfirm = options.noconfirm;
|
||||
|
||||
let mut pacman_args = vec!["-Syu"];
|
||||
if noconfirm {
|
||||
pacman_args.push("--noconfirm");
|
||||
}
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Upgrading repo packages")
|
||||
}
|
||||
|
||||
Command::new("pacman")
|
||||
.args(&pacman_args)
|
||||
.status()
|
||||
.expect("Something has gone wrong.");
|
||||
|
||||
if verbosity >= 1 {
|
||||
eprintln!("Upgrading AUR packages")
|
||||
}
|
||||
|
||||
let res = crate::database::query("\"%\"", options);
|
||||
|
||||
let mut aur_upgrades = vec![];
|
||||
for r in res {
|
||||
let re = r.clone();
|
||||
let ver = rpcinfo(r.name);
|
||||
if ver.package.unwrap().version != r.version {
|
||||
aur_upgrades.push(re.name);
|
||||
}
|
||||
}
|
||||
|
||||
aur_install(aur_upgrades, options);
|
||||
}
|
Loading…
Reference in New Issue