Skip to content

Commit

Permalink
Merge branch 'owen-updates' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ericthelemur committed Oct 3, 2022
2 parents b4744b0 + f48b186 commit 4f8269d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 28 deletions.
3 changes: 3 additions & 0 deletions Rocket.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ port = 8000

[debug.databases.packagedb]
url = "packages/packagedb.sqlite"

[release.databases.packagedb]
url = "packages/packagedb.sqlite"
13 changes: 13 additions & 0 deletions dcspkg-server/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ pub async fn get_package_by_name(
Ok(all.pop())
}

pub async fn get_package_by_id(
conn: &mut PoolConnection<Sqlite>,
id: i64,
) -> Result<Option<Package>, sqlx::Error> {
let mut all: Vec<Package> = 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<Sqlite>,
) -> Result<Vec<Package>, sqlx::Error> {
Expand Down
11 changes: 7 additions & 4 deletions dcspkg-server/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,8 +14,11 @@ pub async fn list(mut db: Connection<PackageDB>) -> Json<Vec<Package>> {

#[get("/pkgdata/<name>")]
pub async fn pkgdata(mut db: Connection<PackageDB>, name: &str) -> Option<Json<Package>> {
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)
}
22 changes: 14 additions & 8 deletions dcspkg/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ 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
Installed,
Installed {
#[clap(long, short, action)]
json: bool
},
///Run the executable from the package specified
Run { package: String },
}
Expand All @@ -34,9 +40,9 @@ impl Command {
pub fn run(&self, config: DcspkgConfig) -> anyhow::Result<()> {
use Command::*;
match &self {
List => {
print_package_list(&dcspkg_client::list(config.server.url)?);
Ok(())
List { json } => {
let packages = dcspkg_client::list(config.server.url)?;
print_package_list(&packages, *json).context("Cannot format package list")
}
Install { package } => dcspkg_client::install(
package,
Expand All @@ -45,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)?
Expand Down
33 changes: 19 additions & 14 deletions dcspkg/src/util.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
6 changes: 4 additions & 2 deletions scripts/initdb.sh
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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))"
UNIQUE (name, version))"

0 comments on commit 4f8269d

Please sign in to comment.