diff options
author | Akshay <[email protected]> | 2020-08-25 05:19:09 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-08-25 05:19:09 +0100 |
commit | 586cc4161b62ddb214e6dc2bb5156390d7eac715 (patch) | |
tree | 4de881a51c320d4427769003abb478e937731c72 | |
parent | 09e41a59e81f0e4858b33f01d94b0340c4e10fec (diff) |
add version flag
-rw-r--r-- | src/cli.rs | 8 | ||||
-rw-r--r-- | src/main.rs | 7 |
2 files changed, 14 insertions, 1 deletions
@@ -6,6 +6,7 @@ use std::path::PathBuf; | |||
6 | 6 | ||
7 | pub struct Config { | 7 | pub 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 | ||
41 | fn parse_args() -> Result<Config> { | 44 | fn 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 | |||
58 | pub 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; | |||
12 | use service::shortner_service; | 12 | use service::shortner_service; |
13 | 13 | ||
14 | mod cli; | 14 | mod cli; |
15 | use cli::{CONFIG, HELP_TEXT}; | 15 | use cli::{version, CONFIG, HELP_TEXT}; |
16 | 16 | ||
17 | fn main() -> Result<()> { | 17 | fn 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 { |