cli.rs
| 1 | use std::net::IpAddr; |
| 2 | use std::path::PathBuf; |
| 3 | |
| 4 | use clap::Parser; |
| 5 | use webauthn_rs::prelude::Url; |
| 6 | |
| 7 | #[derive(Parser, Debug)] |
| 8 | #[command( |
| 9 | name = "filebrowser-ng", |
| 10 | about = "A simple, noob-friendly web file browser", |
| 11 | version |
| 12 | )] |
| 13 | pub struct Cli { |
| 14 | /// Root folder the server has access to (required). |
| 15 | /// All user folders and shares are relative to this. |
| 16 | #[arg(long)] |
| 17 | pub root: PathBuf, |
| 18 | |
| 19 | /// Display name of the root folder in the UI. Defaults to the folder's |
| 20 | /// own name, which in a container is the mount point (e.g. "data"). |
| 21 | #[arg(long, env = "FILEBROWSER_ROOT_NAME")] |
| 22 | pub root_name: Option<String>, |
| 23 | |
| 24 | /// Path to the SQLite database file (required). |
| 25 | #[arg(long)] |
| 26 | pub db: PathBuf, |
| 27 | |
| 28 | /// Port to listen on. |
| 29 | #[arg(long, default_value_t = 8080)] |
| 30 | pub port: u16, |
| 31 | |
| 32 | /// Address to bind. Use 0.0.0.0 to expose beyond localhost. |
| 33 | #[arg(long, default_value = "127.0.0.1")] |
| 34 | pub bind: IpAddr, |
| 35 | |
| 36 | /// Folder for the thumbnail cache. Without it thumbnails are off and |
| 37 | /// the grid shows icons only. Entries not used for a day are swept. |
| 38 | #[arg(long, env = "FILEBROWSER_CACHE")] |
| 39 | pub cache: Option<PathBuf>, |
| 40 | |
| 41 | /// Public base URL of this server, e.g. `https://files.example.com`. |
| 42 | /// Share and WebDAV links in the UI are built from it, and an `https` |
| 43 | /// scheme is what tells the server it sits behind a TLS-terminating |
| 44 | /// proxy. Without it links use whatever address the browser is connected |
| 45 | /// to, and the server assumes plain HTTP. |
| 46 | #[arg(long, env = "FILEBROWSER_PUBLIC_URL")] |
| 47 | pub public_url: Option<Url>, |
| 48 | } |
| 49 | |
| 50 | #[cfg(test)] |
| 51 | mod tests { |
| 52 | use super::*; |
| 53 | use clap::Parser; |
| 54 | use std::path::PathBuf; |
| 55 | |
| 56 | #[test] |
| 57 | fn defaults_applied() { |
| 58 | let c = Cli::try_parse_from(["filebrowser-ng", "--root", "/r", "--db", "/d"]).unwrap(); |
| 59 | assert_eq!(c.root, PathBuf::from("/r")); |
| 60 | assert_eq!(c.db, PathBuf::from("/d")); |
| 61 | assert_eq!(c.port, 8080); |
| 62 | assert_eq!(c.bind.to_string(), "127.0.0.1"); |
| 63 | assert_eq!(c.cache, None); |
| 64 | } |
| 65 | |
| 66 | #[test] |
| 67 | fn flags_override_defaults() { |
| 68 | let c = Cli::try_parse_from([ |
| 69 | "filebrowser-ng", |
| 70 | "--root", |
| 71 | "/r", |
| 72 | "--db", |
| 73 | "/d", |
| 74 | "--port", |
| 75 | "9000", |
| 76 | "--bind", |
| 77 | "0.0.0.0", |
| 78 | ]) |
| 79 | .unwrap(); |
| 80 | assert_eq!(c.port, 9000); |
| 81 | assert_eq!(c.bind.to_string(), "0.0.0.0"); |
| 82 | } |
| 83 | |
| 84 | #[test] |
| 85 | fn required_args_enforced() { |
| 86 | assert!(Cli::try_parse_from(["filebrowser-ng", "--root", "/r"]).is_err()); |
| 87 | assert!(Cli::try_parse_from(["filebrowser-ng"]).is_err()); |
| 88 | } |
| 89 | } |
| 90 |