Skip to content

Commit fe4e766

Browse files
committed
database and create fucked with
1 parent 7d328e7 commit fe4e766

File tree

8 files changed

+85
-546
lines changed

8 files changed

+85
-546
lines changed

Cargo.lock

Lines changed: 23 additions & 474 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dcspkg-create/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ smol = "1.2.5"
1515
tar = "0.4.38"
1616
anyhow = "1"
1717
flate2 = "1.0.24"
18-
clap = { version = "3.2.20", features = ["derive", "cargo"] }
18+
clap = { version = "4.0.29", features = ["derive", "cargo"] }
1919
dialoguer = "0.10.2"
2020
semver = "1.0.13"
2121
url = "2.3.0"
22-
sqlx = { version = "0.6.1", features = ["sqlite", "runtime-async-std-rustls"] }
22+
sqlx = { version = "0.6.2", features = ["sqlite", "runtime-async-std-rustls"] }
23+
dcspkg_server = { path = "../dcspkg-server" }

dcspkg-create/src/db.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
use anyhow::{anyhow, Context, Result};
2-
use dcspkg_common::Package;
2+
use dcspkg_server::Package;
33
use sqlx::{
44
sqlite::{self, SqliteConnection},
55
Connection,
66
};
77
use std::path::Path;
88

9-
pub fn validate_name_and_version(db_path: &Path, pkg_name: &str, version: &str) -> Result<()> {
10-
smol::block_on(async { async_validate_name_and_version(db_path, pkg_name, version).await })
9+
pub fn check_name_unique(db_path: &Path, pkg_name: &str) -> Result<()> {
10+
smol::block_on(async { async_check_name_unique(db_path, pkg_name).await })
1111
}
1212

13-
pub fn add_package_to_db(db_path: &Path, package: &mut Package) -> Result<()> {
13+
pub fn add_package_to_db(db_path: &Path, package: Package) -> Result<()> {
1414
smol::block_on(async { async_add_package_to_db(db_path, package).await })
1515
}
1616

17-
async fn async_validate_name_and_version(
18-
db_path: &Path,
19-
pkg_name: &str,
20-
version: &str,
21-
) -> Result<()> {
17+
async fn async_check_name_unique(db_path: &Path, pkg_name: &str) -> Result<()> {
2218
let mut connection = connect(db_path).await?;
2319
let result: Result<Option<(String, String)>, sqlx::Error> =
24-
sqlx::query_as("SELECT name, version FROM packages WHERE name=? AND version=?")
20+
sqlx::query_as("SELECT * FROM packages WHERE pkgname=?")
2521
.bind(pkg_name)
26-
.bind(version)
2722
.fetch_optional(&mut connection)
2823
.await;
2924

@@ -36,23 +31,20 @@ async fn async_validate_name_and_version(
3631
}
3732
}
3833

39-
async fn async_add_package_to_db(db_path: &Path, package: &mut Package) -> Result<()> {
34+
async fn async_add_package_to_db(db_path: &Path, package: Package) -> Result<()> {
4035
let mut connection = connect(db_path).await?;
41-
let query = sqlx::query(
42-
"INSERT INTO packages (name, description, version, image_url, archive_path, executable_path, crc, has_installer, add_to_path) VALUES (?,?,?,?,?,?,?,?,?)")
43-
.bind(&package.name)
36+
sqlx::query(
37+
"INSERT INTO packages (pkgname, fullname, description, image_url, executable_path, crc, has_installer, add_to_path) VALUES (?,?,?,?,?,?,?,?,?)")
38+
.bind(&package.pkgname)
39+
.bind(&package.fullname)
4440
.bind(&package.description)
45-
.bind(&package.version)
4641
.bind(&package.image_url)
47-
.bind(&package.archive_path)
4842
.bind(&package.executable_path)
4943
.bind(package.crc)
5044
.bind(package.has_installer)
5145
.bind(package.add_to_path)
5246
.execute(&mut connection)
53-
.await.context("Could not insert package into database")?;
54-
package.id = query.last_insert_rowid();
55-
Ok(())
47+
.await.context("Could not insert package into database").map(|_|()).map_err(Into::into)
5648
}
5749

5850
async fn connect(path: &Path) -> Result<SqliteConnection> {

dcspkg-create/src/main.rs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clap::Parser;
2-
use dcspkg_common::Package;
2+
use dcspkg_server::Package;
33
use std::io::Write;
4-
use std::path::{Path, PathBuf};
4+
use std::path::PathBuf;
55
mod archive;
66
mod db;
77
mod opts;
@@ -12,44 +12,41 @@ fn main() -> anyhow::Result<()> {
1212
println!("Creating new dcspkg from {directory:?}");
1313
println!("Please specify package options (skip to use defaults)");
1414

15-
let pkg_name = opts::get_pkg_name(directory.file_name().and_then(|s| s.to_str()))?;
16-
let version = opts::get_version()?;
15+
let pkgname = opts::get_pkg_name(directory.file_name().and_then(|s| s.to_str()))?;
1716

18-
db::validate_name_and_version(&args.db, &pkg_name, &version)?;
17+
db::check_name_unique(&args.db, &pkgname)?;
1918

19+
let fullname = opts::get_full_name(&pkgname)?;
2020
let description = opts::get_description()?;
2121
let image_url = opts::get_image_url()?;
2222
let executable_path = opts::get_exe_path(&directory)?;
2323
let add_to_path = opts::add_to_path()?;
2424
let has_installer = opts::has_installer(&directory)?;
25-
let archive_name = format!("{pkg_name}-{version}.dcspkg");
2625

2726
print!("Creating tarball...");
2827
std::io::stdout().flush()?; //print with no newline so force a flush
2928

30-
let archive_path = args.pkg_dir.join(&archive_name);
29+
let archive_path = args.pkg_dir.join(format!("{pkgname}.dcspkg"));
3130

3231
let crc = archive::make_archive(&archive_path, &directory)?;
3332

3433
println!("done!");
3534

36-
let mut package = Package {
37-
id: 0,
38-
name: pkg_name,
35+
let package = Package {
36+
pkgname,
3937
description,
40-
version,
4138
image_url,
42-
archive_path: archive_name,
4339
executable_path,
4440
crc,
4541
has_installer,
4642
add_to_path,
43+
fullname,
4744
};
4845

49-
db::add_package_to_db(&args.db, &mut package)?;
50-
5146
println!("{}", serde_json::to_string_pretty(&package)?);
5247

48+
db::add_package_to_db(&args.db, package)?;
49+
5350
println!("Added package to database");
5451
println!("Your package is now ready for download!");
5552
Ok(())
@@ -59,26 +56,30 @@ fn main() -> anyhow::Result<()> {
5956
#[clap(author, version, about, long_about = None)]
6057
struct Cli {
6158
/// The directory to package up
62-
#[clap(validator = dir_exists)]
59+
#[arg(value_parser = dir_exists)]
6360
directory: PathBuf,
64-
#[clap(short, long, value_parser, validator=file_exists)]
65-
#[clap(default_value = "packages/packagedb.sqlite")]
61+
#[arg(short, long, value_parser, value_parser=file_exists)]
62+
#[arg(default_value = "packages/packagedb.sqlite")]
6663
db: PathBuf,
67-
#[clap(short, long, value_parser, validator=dir_exists)]
68-
#[clap(default_value = "packages/packages")]
64+
#[arg(short, long, value_parser, value_parser=dir_exists)]
65+
#[arg(default_value = "packages/packages")]
6966
pkg_dir: PathBuf,
7067
}
7168

72-
fn dir_exists(f: &str) -> Result<(), &'static str> {
73-
Path::new(f)
74-
.is_dir()
75-
.then_some(())
76-
.ok_or("Directory does not exist")
69+
fn dir_exists(f: &str) -> Result<PathBuf, &'static str> {
70+
let path = PathBuf::from(f);
71+
if !path.is_dir() {
72+
Err("Directory does not exist")
73+
} else {
74+
Ok(path)
75+
}
7776
}
7877

79-
fn file_exists(f: &str) -> Result<(), &'static str> {
80-
Path::new(f)
81-
.is_file()
82-
.then_some(())
83-
.ok_or("File does not exist")
78+
fn file_exists(f: &str) -> Result<PathBuf, &'static str> {
79+
let path = PathBuf::from(f);
80+
if !path.is_file() {
81+
Err("File does not exist")
82+
} else {
83+
Ok(path)
84+
}
8485
}

dcspkg-create/src/opts.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,28 @@ use std::path::Path;
55
pub fn get_pkg_name(default: Option<&str>) -> Result<String> {
66
if let Some(default) = default {
77
Input::with_theme(&ColorfulTheme::default())
8-
.with_prompt("Enter package name")
8+
.with_prompt("Enter package short name")
99
.default(default.to_string())
1010
.show_default(true)
1111
.interact_text()
1212
} else {
1313
Input::with_theme(&ColorfulTheme::default())
14-
.with_prompt("Enter package name")
14+
.with_prompt("Enter package short name")
1515
.show_default(true)
1616
.interact_text()
1717
}
1818
.context("Could not get package name")
1919
}
2020

21+
pub fn get_full_name(default: &str) -> Result<String> {
22+
Input::with_theme(&ColorfulTheme::default())
23+
.with_prompt("Enter full application name or game title")
24+
.default(default.to_string())
25+
.show_default(true)
26+
.interact_text()
27+
.context("Could not get package fullname")
28+
}
29+
2130
pub fn get_description() -> Result<Option<String>> {
2231
Input::<String>::with_theme(&ColorfulTheme::default())
2332
.with_prompt("Enter package description")
@@ -27,15 +36,6 @@ pub fn get_description() -> Result<Option<String>> {
2736
.context("Could not get description")
2837
}
2938

30-
pub fn get_version() -> Result<String> {
31-
Input::with_theme(&ColorfulTheme::default())
32-
.with_prompt("Enter version")
33-
.default("0.1.0".to_owned())
34-
.validate_with(|input: &String| semver::Version::parse(input).map(|_| ()))
35-
.interact_text()
36-
.context("Could not get version")
37-
}
38-
3939
pub fn get_image_url() -> Result<Option<String>> {
4040
Input::<String>::with_theme(&ColorfulTheme::default())
4141
.with_prompt("Enter URL for image")

dcspkg-server/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub async fn get_package_by_name(
44
conn: &sqlx::SqlitePool,
55
name: &str,
66
) -> Result<Option<Package>, sqlx::Error> {
7-
sqlx::query_as("SELECT * FROM packages WHERE name=?")
7+
sqlx::query_as("SELECT * FROM packages WHERE pkgname=?")
88
.bind(name)
99
.fetch_optional(conn)
1010
.await

dcspkg-server/src/package.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ use serde::{Deserialize, Serialize};
88
pub struct Package {
99
/// The package's name, ie "gcc"
1010
/// This is the primary key
11-
pub name: String,
11+
pub pkgname: String,
1212
/// The game/app's full name/title, ie "The GNU Compiler Collection, Version 4.3"
1313
pub fullname: String,
1414
/// A short description of the package
1515
#[sqlx(default)]
1616
pub description: Option<String>,
1717
/// A URL pointing to an image for the package
1818
pub image_url: Option<String>,
19-
/// The path to the package tarball on disk
20-
pub archive_path: String,
2119
/// The relative path of the executable within the tarball
2220
pub executable_path: Option<String>,
2321
/// The package's CRC checksum

scripts/initdb.sh

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ mkdir -p packages/packages
44
touch packages/packagedb.sqlite
55
sqlite3 packages/packagedb.sqlite \
66
"CREATE TABLE packages(
7-
id INTEGER PRIMARY KEY NOT NULL,
8-
name STRING NOT NULL,
7+
pkgname STRING PRIMARY KEY NOT NULL,
8+
fullname STRING NOT NULL,
99
description STRING,
10-
version STRING NOT NULL,
1110
image_url STRING,
12-
archive_path STRING NOT NULL,
1311
executable_path STRING,
1412
crc INTEGER NOT NULL,
1513
has_installer INTEGER NOT NULL,
16-
add_to_path INTEGER NOT NULL,
17-
UNIQUE (name, version))"
14+
add_to_path INTEGER NOT NULL)
15+
"

0 commit comments

Comments
 (0)