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