From 47c6094d3cb805fdb1e8f41072fb77a68841d9e5 Mon Sep 17 00:00:00 2001 From: ericthelemur Date: Sat, 1 Oct 2022 18:06:48 +0100 Subject: [PATCH 1/5] initdb creates files as well --- scripts/initdb.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/initdb.sh b/scripts/initdb.sh index c17c7b5..fcb2a69 100755 --- a/scripts/initdb.sh +++ b/scripts/initdb.sh @@ -1,6 +1,8 @@ #! /bin/sh -sqlite3 packagedb.sqlite \ +mkdir -p packages/packages +touch packages/packagedb.sqlite +sqlite3 packages/packagedb.sqlite \ "CREATE TABLE packages( id INTEGER PRIMARY KEY NOT NULL, name STRING NOT NULL, @@ -12,4 +14,4 @@ sqlite3 packagedb.sqlite \ crc INTEGER NOT NULL, has_installer INTEGER NOT NULL, add_to_path INTEGER NOT NULL, - UNIQUE (name, version))" \ No newline at end of file + UNIQUE (name, version))" From 96be337dcd9c1cf7666e1e8f25932ca267bd067b Mon Sep 17 00:00:00 2001 From: ericthelemur Date: Sun, 2 Oct 2022 11:53:58 +0100 Subject: [PATCH 2/5] Add JSON flag to list --- dcspkg/src/cli.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/dcspkg/src/cli.rs b/dcspkg/src/cli.rs index d712934..1f5f38f 100644 --- a/dcspkg/src/cli.rs +++ b/dcspkg/src/cli.rs @@ -21,7 +21,10 @@ pub struct Cli { #[derive(Subcommand)] pub enum Command { ///List all packages available for install - List, + List { + #[clap(long, short, action)] + json: bool + }, /// Install a package Install { package: String }, ///Show all installed packages and their versions @@ -34,8 +37,13 @@ impl Command { pub fn run(&self, config: DcspkgConfig) -> anyhow::Result<()> { use Command::*; match &self { - List => { - print_package_list(&dcspkg_client::list(config.server.url)?); + List { json } => { + let packages = dcspkg_client::list(config.server.url)?; + if *json { + println!("{}", serde_json::to_string(&packages)?) + } else { + print_package_list(&packages); + } Ok(()) } Install { package } => dcspkg_client::install( From b3679fcb860b8a22bbf825574c47432e780dd534 Mon Sep 17 00:00:00 2001 From: ericthelemur Date: Sun, 2 Oct 2022 11:54:24 +0100 Subject: [PATCH 3/5] Release DB setting (maybe use postgres in future) --- Rocket.toml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Rocket.toml b/Rocket.toml index 0b3ffb6..c99022a 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -4,3 +4,6 @@ port = 8000 [debug.databases.packagedb] url = "packages/packagedb.sqlite" + +[release.databases.packagedb] +url = "packages/packagedb.sqlite" From 5e2bbe77f2c43f8af1be0cf29ed3ba48eead7385 Mon Sep 17 00:00:00 2001 From: ericthelemur Date: Sun, 2 Oct 2022 18:08:13 +0100 Subject: [PATCH 4/5] Can fetch package data by name or ID --- dcspkg-server/src/db.rs | 13 +++++++++++++ dcspkg-server/src/handlers.rs | 11 +++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/dcspkg-server/src/db.rs b/dcspkg-server/src/db.rs index b4c282b..3e537eb 100644 --- a/dcspkg-server/src/db.rs +++ b/dcspkg-server/src/db.rs @@ -19,6 +19,19 @@ pub async fn get_package_by_name( Ok(all.pop()) } +pub async fn get_package_by_id( + conn: &mut PoolConnection, + id: i64, +) -> Result, sqlx::Error> { + let mut all: Vec = sqlx::query_as("SELECT * FROM packages WHERE id=?") + .bind(id) + .fetch_all(conn) + .await?; + //get latest version + all.sort_by_key(|p| semver::Version::parse(&p.version).unwrap()); + Ok(all.pop()) +} + pub async fn get_all_packages( conn: &mut PoolConnection, ) -> Result, sqlx::Error> { diff --git a/dcspkg-server/src/handlers.rs b/dcspkg-server/src/handlers.rs index 8e7a4e6..a5eb0b3 100644 --- a/dcspkg-server/src/handlers.rs +++ b/dcspkg-server/src/handlers.rs @@ -1,4 +1,4 @@ -use crate::db::{get_all_packages, get_package_by_name, PackageDB}; +use crate::db::{get_all_packages, get_package_by_name, get_package_by_id, PackageDB}; use dcspkg_common::Package; use rocket::get; use rocket::serde::json::Json; @@ -14,8 +14,11 @@ pub async fn list(mut db: Connection) -> Json> { #[get("/pkgdata/")] pub async fn pkgdata(mut db: Connection, name: &str) -> Option> { - match get_package_by_name(&mut *db, name).await { - Ok(x) => x.map(Json), - Err(e) => panic!("{e:?}"), //TODO, work out how to handle failure in reponder + let mut pkg = get_package_by_name(&mut *db, name).await.ok()?; + // If nothing, attempt fetching by ID + if pkg.is_none() { + let id: i64 = name.parse().ok()?; + pkg = get_package_by_id(&mut *db, id).await.ok()?; } + pkg.map(Json) } From f48b186ceb2a5a65bbcd2a0099800d40fbaa65b8 Mon Sep 17 00:00:00 2001 From: ericthelemur Date: Sun, 2 Oct 2022 18:22:15 +0100 Subject: [PATCH 5/5] Add json to installed --- dcspkg/src/cli.rs | 18 ++++++++---------- dcspkg/src/util.rs | 33 +++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/dcspkg/src/cli.rs b/dcspkg/src/cli.rs index 1f5f38f..40a0503 100644 --- a/dcspkg/src/cli.rs +++ b/dcspkg/src/cli.rs @@ -28,7 +28,10 @@ pub enum Command { /// Install a package Install { package: String }, ///Show all installed packages and their versions - Installed, + Installed { + #[clap(long, short, action)] + json: bool + }, ///Run the executable from the package specified Run { package: String }, } @@ -39,12 +42,7 @@ impl Command { match &self { List { json } => { let packages = dcspkg_client::list(config.server.url)?; - if *json { - println!("{}", serde_json::to_string(&packages)?) - } else { - print_package_list(&packages); - } - Ok(()) + print_package_list(&packages, *json).context("Cannot format package list") } Install { package } => dcspkg_client::install( package, @@ -53,9 +51,9 @@ impl Command { config.registry.bin_dir, config.registry.registry_file, ), - Installed => { - print_package_list(&get_registry(&config.registry.registry_file)?); - Ok(()) + Installed { json } => { + let packages = &get_registry(&config.registry.registry_file)?; + print_package_list(packages, *json).context("Cannot format package list") } Run { package } => { let package_data = get_registry(&config.registry.registry_file)? diff --git a/dcspkg/src/util.rs b/dcspkg/src/util.rs index 997992a..3be05ba 100644 --- a/dcspkg/src/util.rs +++ b/dcspkg/src/util.rs @@ -1,21 +1,26 @@ use dcspkg_client::Package; use tabular::{Row, Table}; -pub fn print_package_list(list: &[Package]) { - let mut table = Table::new("{:<} {:<} {:<}").with_row( - Row::new() - .with_cell("Package Name") - .with_cell("Version") - .with_cell("Description"), - ); - for pkg in list { - table.add_row( +pub fn print_package_list(list: &[Package], raw: bool) -> Option<()> { + if raw { + println!("{}", serde_json::to_string(list).unwrap()); + } else { + let mut table = Table::new("{:<} {:<} {:<}").with_row( Row::new() - .with_cell(&pkg.name) - .with_cell(&pkg.version) - .with_cell(pkg.description.as_deref().unwrap_or("-")), + .with_cell("Package Name") + .with_cell("Version") + .with_cell("Description"), ); - } + for pkg in list { + table.add_row( + Row::new() + .with_cell(&pkg.name) + .with_cell(&pkg.version) + .with_cell(pkg.description.as_deref().unwrap_or("-")), + ); + } - println!("{table}"); + println!("{table}"); + } + Some(()) }