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 /// Assume the server runs behind a TLS-terminating reverse proxy
35 /// (sets the `Secure` attribute on the session cookie).
36 #[arg(long)]
37 pub https: bool,
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use clap::Parser;
44 use std::path::PathBuf;
45
46 #[test]
47 fn defaults_applied() {
48 let c = Cli::try_parse_from(["filebrowser-ng", "--root", "/r", "--db", "/d"]).unwrap();
49 assert_eq!(c.root, PathBuf::from("/r"));
50 assert_eq!(c.db, PathBuf::from("/d"));
51 assert_eq!(c.port, 8080);
52 assert_eq!(c.bind, "127.0.0.1");
53 assert!(!c.https);
54 }
55
56 #[test]
57 fn flags_override_defaults() {
58 let c = Cli::try_parse_from([
59 "filebrowser-ng",
60 "--root",
61 "/r",
62 "--db",
63 "/d",
64 "--port",
65 "9000",
66 "--bind",
67 "0.0.0.0",
68 "--https",
69 ])
70 .unwrap();
71 assert_eq!(c.port, 9000);
72 assert_eq!(c.bind, "0.0.0.0");
73 assert!(c.https);
74 }
75
76 #[test]
77 fn required_args_enforced() {
78 assert!(Cli::try_parse_from(["filebrowser-ng", "--root", "/r"]).is_err());
79 assert!(Cli::try_parse_from(["filebrowser-ng"]).is_err());
80 }
81}
82