From 586cc4161b62ddb214e6dc2bb5156390d7eac715 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 25 Aug 2020 09:49:09 +0530 Subject: add version flag --- src/cli.rs | 8 ++++++++ src/main.rs | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) 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; pub struct Config { pub help: bool, + pub version: bool, pub port: u16, pub db_path: PathBuf, } @@ -14,6 +15,7 @@ impl Default for Config { fn default() -> Self { Config { help: false, + version: false, port: 3000, db_path: "./urls.db_3".into(), } @@ -36,12 +38,14 @@ Options -h, --help Prints help information --port Port to start the server on (default: 3000) --database Path to database (default: urls.db_3) + -v, --version Print crate version "; fn parse_args() -> Result { let mut _a = pico_args::Arguments::from_env(); return Ok(Config { help: _a.contains(["-h", "--help"]), + version: _a.contains(["-v", "--version"]), port: _a .opt_value_from_fn("--port", str::parse::)? .unwrap_or(7878), @@ -50,3 +54,7 @@ fn parse_args() -> Result { .unwrap_or(PathBuf::from("./urls.db_3")), }); } + +pub fn version() { + println!("isostatic v{}", env!("CARGO_PKG_VERSION")); +} 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; use service::shortner_service; mod cli; -use cli::{CONFIG, HELP_TEXT}; +use cli::{version, CONFIG, HELP_TEXT}; fn main() -> Result<()> { pretty_env_logger::init(); if CONFIG.help { + version(); println!("{}", HELP_TEXT); return Ok(()); } + if CONFIG.version { + version(); + return Ok(()); + } init_db(&CONFIG.db_path)?; smol::run(async { -- cgit v1.2.3