cli.rs
| 1 | use std::path::PathBuf; |
| 2 | |
| 3 | use clap::Parser; |
| 4 | |
| 5 | #[derive(Parser, Debug)] |
| 6 | #[command( |
| 7 | name = "filebrowser-ng", |
| 8 | about = "A simple, noob-friendly web file browser", |
| 9 | version |
| 10 | )] |
| 11 | pub struct Cli { |
| 12 | /// Root folder the server has access to (required). |
| 13 | /// All user folders and shares are relative to this. |
| 14 | #[arg(long)] |
| 15 | pub root: PathBuf, |
| 16 | |
| 17 | /// Path to the SQLite database file (required). |
| 18 | #[arg(long)] |
| 19 | pub db: PathBuf, |
| 20 | |
| 21 | /// Port to listen on. |
| 22 | #[arg(long, default_value_t = 8080)] |
| 23 | pub port: u16, |
| 24 | |
| 25 | /// Address to bind. Use 0.0.0.0 to expose beyond localhost. |
| 26 | #[arg(long, default_value = "127.0.0.1")] |
| 27 | pub bind: String, |
| 28 | |
| 29 | /// Assume the server runs behind a TLS-terminating reverse proxy |
| 30 | /// (sets the `Secure` attribute on the session cookie). |
| 31 | #[arg(long)] |
| 32 | pub https: bool, |
| 33 | } |
| 34 | |
| 35 | #[cfg(test)] |
| 36 | mod tests { |
| 37 | use super::*; |
| 38 | use clap::Parser; |
| 39 | use std::path::PathBuf; |
| 40 | |
| 41 | #[test] |
| 42 | fn defaults_applied() { |
| 43 | let c = Cli::try_parse_from(["filebrowser-ng", "--root", "/r", "--db", "/d"]).unwrap(); |
| 44 | assert_eq!(c.root, PathBuf::from("/r")); |
| 45 | assert_eq!(c.db, PathBuf::from("/d")); |
| 46 | assert_eq!(c.port, 8080); |
| 47 | assert_eq!(c.bind, "127.0.0.1"); |
| 48 | assert!(!c.https); |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn flags_override_defaults() { |
| 53 | let c = Cli::try_parse_from([ |
| 54 | "filebrowser-ng", |
| 55 | "--root", |
| 56 | "/r", |
| 57 | "--db", |
| 58 | "/d", |
| 59 | "--port", |
| 60 | "9000", |
| 61 | "--bind", |
| 62 | "0.0.0.0", |
| 63 | "--https", |
| 64 | ]) |
| 65 | .unwrap(); |
| 66 | assert_eq!(c.port, 9000); |
| 67 | assert_eq!(c.bind, "0.0.0.0"); |
| 68 | assert!(c.https); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn required_args_enforced() { |
| 73 | assert!(Cli::try_parse_from(["filebrowser-ng", "--root", "/r"]).is_err()); |
| 74 | assert!(Cli::try_parse_from(["filebrowser-ng"]).is_err()); |
| 75 | } |
| 76 | } |
| 77 |