feat: add cli

Added cli and options --config and --database to pass path to the configuration and database file.
This commit is contained in:
MedzikUser 2022-09-21 18:49:06 +02:00
parent f5d8341234
commit e50ee66b0e
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
6 changed files with 154 additions and 19 deletions

111
Cargo.lock generated
View File

@ -107,6 +107,17 @@ dependencies = [
"num-traits",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -292,6 +303,45 @@ dependencies = [
"winapi",
]
[[package]]
name = "clap"
version = "3.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"strsim",
"termcolor",
"textwrap",
]
[[package]]
name = "clap_derive"
version = "3.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea0c8bce528c4be4da13ea6fead8965e95b6073585a2f05204bd8f4119f82a65"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
@ -717,6 +767,7 @@ dependencies = [
"axum-server",
"backtrace",
"byte-unit",
"clap",
"crypto-utils",
"futures-util",
"governor",
@ -1098,6 +1149,12 @@ version = "1.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0"
[[package]]
name = "os_str_bytes"
version = "6.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ff7415e9ae3fff1225851df9e0d9e4e5479f947619774677a63572e55e80eff"
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -1211,6 +1268,30 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.43"
@ -1601,6 +1682,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "subtle"
version = "2.4.1"
@ -1624,6 +1711,21 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20518fe4a4c9acf048008599e464deb21beeae3d3578418951a189c235a7a9a8"
[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "949517c0cf1bf4ee812e2e07e08ab448e3ae0d23472aee8a06c985f0c8815b16"
[[package]]
name = "thiserror"
version = "1.0.35"
@ -2114,6 +2216,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -1,5 +1,6 @@
[package]
name = "homedisk"
description = "Extremely fast and lightweight local cloud for your data written in Rust."
authors = ["MedzikUser <medzik@duck.com>"]
homepage = "https://github.com/HomeDisk/cloud"
repository = "https://github.com/HomeDisk/cloud"
@ -40,8 +41,11 @@ axum = { version = "0.6.0-rc.2", features = ["http2", "multipart"] }
axum-server = { version = "0.4", features = ["tls-rustls"] }
tower-http = { version = "0.3", features = ["full"] }
hyper = { version = "0.14", features = ["full"] }
byte-unit = "4.0.14"
byte-unit = "4.0"
# HTTP rate limiting
once_cell = "1.14.0"
governor = "0.5.0"
once_cell = "1.14"
governor = "0.5"
# CLI
clap = { version = "3.2", features = ["derive"] }

View File

@ -8,57 +8,71 @@
use std::{fs::File, io::Write, path::Path};
use clap::Parser;
use config::Config;
use tracing::{info, warn};
use crate::database::Database;
#[path = "./types/config.rs"]
mod config;
mod database;
mod logger;
mod server;
mod variables;
/// Default SQLite file path for the database.
pub const DATABASE_FILE: &str = "homedisk.db";
/// Default configuration file.
pub const CONFIG_FILE: &str = "config.toml";
/// Default configuration file content.
pub static DEFAULT_CONFIG_CONTENT: &'static [u8] = include_bytes!("../config.toml");
pub use variables::*;
#[derive(Debug, Parser)]
#[clap(
name = env!("CARGO_PKG_NAME"),
about = env!("CARGO_PKG_DESCRIPTION"),
version = env!("CARGO_PKG_VERSION"),
)]
struct Cli {
#[clap(short = 'c', long = "config", help = "Configuration file path", default_value = DEFAULT_CONFIG_FILE, display_order = 1)]
config: String,
#[clap(short = 'd', long = "database", help = "SQLite database path", default_value = DEFAULT_DATABASE_FILE, display_order = 2)]
database: String,
}
#[tokio::main]
async fn main() {
logger::init();
let args = Cli::parse();
let config_path = args.config;
let database_path = &args.database;
// if configuration file doesn't exist, create it
if !Path::new(CONFIG_FILE).exists() {
if !Path::new(&config_path).exists() {
warn!("Configuration file doesn't exists.");
let mut file =
File::create(CONFIG_FILE).expect("notrace - Failed to create configuration file");
File::create(config_path).expect("notrace - Failed to create configuration file");
file.write_all(DEFAULT_CONFIG_CONTENT)
.expect("notrace - Failed to write default configuration to config file");
info!("Created default configuration file.");
info!("Created configuration file. Exiting...");
std::process::exit(0);
}
let config = Config::parse(CONFIG_FILE).expect("notrace - Failed to parse configuration file");
let config = Config::parse(&config_path).expect("notrace - Failed to parse configuration file");
// open database connection
let db =
// if database file doesn't exists create it
if !Path::new(DATABASE_FILE).exists() {
if !Path::new(&database_path).exists() {
warn!("Database file doesn't exists.");
info!("Creating database file...");
// create an empty database file
File::create(DATABASE_FILE).expect("notrace - Failed to create a database file");
File::create(database_path).expect("notrace - Failed to create a database file");
// open database file
let db = Database::open(DATABASE_FILE)
let db = Database::open(database_path)
.await
.expect("notrace - Failed to open database file");
@ -71,7 +85,7 @@ async fn main() {
}
// if database file exists
else {
Database::open(DATABASE_FILE)
Database::open(database_path)
.await
.expect("notrace - Failed to open database file")
};

View File

@ -89,7 +89,7 @@ async fn redirect_http_to_https(config: Config) {
}
};
debug!("🚀 Http redirect listening on http://{host}");
debug!("🚀 HTTPS redirect listening on http://{host}");
axum::Server::bind(&host.parse().unwrap())
.serve(redirect.into_make_service())

6
src/variables.rs Normal file
View File

@ -0,0 +1,6 @@
/// Default SQLite file path for the database.
pub const DEFAULT_DATABASE_FILE: &str = "homedisk.db";
/// Default configuration file.
pub const DEFAULT_CONFIG_FILE: &str = "config.toml";
/// Default configuration file content.
pub static DEFAULT_CONFIG_CONTENT: &[u8] = include_bytes!("../config.toml");