aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cli.rs8
-rw-r--r--src/main.rs7
2 files changed, 14 insertions, 1 deletions
diff --git a/src/cli.rs b/src/cli.rs
index 79753d4..33310ca 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -6,6 +6,7 @@ use std::path::PathBuf;
6 6
7pub struct Config { 7pub struct Config {
8 pub help: bool, 8 pub help: bool,
9 pub version: bool,
9 pub port: u16, 10 pub port: u16,
10 pub db_path: PathBuf, 11 pub db_path: PathBuf,
11} 12}
@@ -14,6 +15,7 @@ impl Default for Config {
14 fn default() -> Self { 15 fn default() -> Self {
15 Config { 16 Config {
16 help: false, 17 help: false,
18 version: false,
17 port: 3000, 19 port: 3000,
18 db_path: "./urls.db_3".into(), 20 db_path: "./urls.db_3".into(),
19 } 21 }
@@ -36,12 +38,14 @@ Options
36 -h, --help Prints help information 38 -h, --help Prints help information
37 --port Port to start the server on (default: 3000) 39 --port Port to start the server on (default: 3000)
38 --database Path to database (default: urls.db_3) 40 --database Path to database (default: urls.db_3)
41 -v, --version Print crate version
39"; 42";
40 43
41fn parse_args() -> Result<Config> { 44fn parse_args() -> Result<Config> {
42 let mut _a = pico_args::Arguments::from_env(); 45 let mut _a = pico_args::Arguments::from_env();
43 return Ok(Config { 46 return Ok(Config {
44 help: _a.contains(["-h", "--help"]), 47 help: _a.contains(["-h", "--help"]),
48 version: _a.contains(["-v", "--version"]),
45 port: _a 49 port: _a
46 .opt_value_from_fn("--port", str::parse::<u16>)? 50 .opt_value_from_fn("--port", str::parse::<u16>)?
47 .unwrap_or(7878), 51 .unwrap_or(7878),
@@ -50,3 +54,7 @@ fn parse_args() -> Result<Config> {
50 .unwrap_or(PathBuf::from("./urls.db_3")), 54 .unwrap_or(PathBuf::from("./urls.db_3")),
51 }); 55 });
52} 56}
57
58pub fn version() {
59 println!("isostatic v{}", env!("CARGO_PKG_VERSION"));
60}
diff --git a/src/main.rs b/src/main.rs
index c1e3268..3bd25d5 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,15 +12,20 @@ mod service;
12use service::shortner_service; 12use service::shortner_service;
13 13
14mod cli; 14mod cli;
15use cli::{CONFIG, HELP_TEXT}; 15use cli::{version, CONFIG, HELP_TEXT};
16 16
17fn main() -> Result<()> { 17fn main() -> Result<()> {
18 pretty_env_logger::init(); 18 pretty_env_logger::init();
19 19
20 if CONFIG.help { 20 if CONFIG.help {
21 version();
21 println!("{}", HELP_TEXT); 22 println!("{}", HELP_TEXT);
22 return Ok(()); 23 return Ok(());
23 } 24 }
25 if CONFIG.version {
26 version();
27 return Ok(());
28 }
24 29
25 init_db(&CONFIG.db_path)?; 30 init_db(&CONFIG.db_path)?;
26 smol::run(async { 31 smol::run(async {