From fea9bf988a4d9c2ba96fa209f1448d193df2a41b Mon Sep 17 00:00:00 2001
From: MuXiu1997 <MuXiu1997@Gmail.com>
Date: Sun, 19 Feb 2023 11:40:14 +0800
Subject: [PATCH] feat: use env var for args (#170)

closed #160
---
 Cargo.toml  |  2 +-
 README.md   | 44 ++++++++++++++++++++++----------------------
 src/args.rs | 22 ++++++++++++++++++++++
 3 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 4b2348b..cbe2700 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,7 +11,7 @@ categories = ["command-line-utilities", "web-programming::http-server"]
 keywords = ["static", "file", "server", "webdav", "cli"]
 
 [dependencies]
-clap = { version = "4", features = ["wrap_help"] }
+clap = { version = "4", features = ["wrap_help", "env"] }
 clap_complete = "4"
 chrono = "0.4"
 tokio = { version = "1", features = ["rt-multi-thread", "macros", "fs", "io-util", "signal"]}
diff --git a/README.md b/README.md
index 0bab07a..5278491 100644
--- a/README.md
+++ b/README.md
@@ -51,30 +51,30 @@ Dufs is a distinctive utility file server - https://github.com/sigoden/dufs
 Usage: dufs [OPTIONS] [root]
 
 Arguments:
-  [root]  Specific path to serve [default: .]
+  [root]  Specific path to serve [env: DUFS_ROOT=] [default: .]
 
 Options:
-  -b, --bind <addrs>         Specify bind address or unix socket
-  -p, --port <port>          Specify port to listen on [default: 5000]
-      --path-prefix <path>   Specify a path prefix
-      --hidden <value>       Hide paths from directory listings, separated by `,`
-  -a, --auth <rules>         Add auth for path
-      --auth-method <value>  Select auth method [default: digest] [possible values: basic, digest]
-  -A, --allow-all            Allow all operations
-      --allow-upload         Allow upload files/folders
-      --allow-delete         Allow delete files/folders
-      --allow-search         Allow search files/folders
-      --allow-symlink        Allow symlink to files/folders outside root directory
-      --allow-archive        Allow zip archive generation
-      --enable-cors          Enable CORS, sets `Access-Control-Allow-Origin: *`
-      --render-index         Serve index.html when requesting a directory, returns 404 if not found index.html
-      --render-try-index     Serve index.html when requesting a directory, returns directory listing if not found index.html
-      --render-spa           Serve SPA(Single Page Application)
-      --assets <path>        Use custom assets to override builtin assets
-      --tls-cert <path>      Path to an SSL/TLS certificate to serve with HTTPS
-      --tls-key <path>       Path to the SSL/TLS certificate's private key
-      --log-format <format>  Customize http log format
-      --completions <shell>  Print shell completion script for <shell> [possible values: bash, elvish, fish, powershell, zsh]
+  -b, --bind <addrs>         Specify bind address or unix socket [env: DUFS_BIND=]
+  -p, --port <port>          Specify port to listen on [env: DUFS_PORT=] [default: 5000]
+      --path-prefix <path>   Specify a path prefix [env: DUFS_PATH_PREFIX=]
+      --hidden <value>       Hide paths from directory listings, separated by `,` [env: DUFS_HIDDEN=]
+  -a, --auth <rules>         Add auth for path [env: DUFS_AUTH=]
+      --auth-method <value>  Select auth method [env: DUFS_AUTH_METHOD=] [default: digest] [possible values: basic, digest]
+  -A, --allow-all            Allow all operations [env: DUFS_ALLOW_ALL=]
+      --allow-upload         Allow upload files/folders [env: DUFS_ALLOW_UPLOAD=]
+      --allow-delete         Allow delete files/folders [env: DUFS_ALLOW_DELETE=]
+      --allow-search         Allow search files/folders [env: DUFS_ALLOW_SEARCH=]
+      --allow-symlink        Allow symlink to files/folders outside root directory [env: DUFS_ALLOW_SYMLINK=]
+      --allow-archive        Allow zip archive generation [env: DUFS_ALLOW_ARCHIVE=]
+      --enable-cors          Enable CORS, sets `Access-Control-Allow-Origin: *` [env: DUFS_ENABLE_CORS=]
+      --render-index         Serve index.html when requesting a directory, returns 404 if not found index.html [env: DUFS_RENDER_INDEX=]
+      --render-try-index     Serve index.html when requesting a directory, returns directory listing if not found index.html [env: DUFS_RENDER_TRY_INDEX=]
+      --render-spa           Serve SPA(Single Page Application) [env: DUFS_RENDER_SPA=]
+      --assets <path>        Use custom assets to override builtin assets [env: DUFS_ASSETS=]
+      --tls-cert <path>      Path to an SSL/TLS certificate to serve with HTTPS [env: DUFS_TLS_CERT=]
+      --tls-key <path>       Path to the SSL/TLS certificate's private key [env: DUFS_TLS_KEY=]
+      --log-format <format>  Customize http log format [env: DUFS_LOG_FORMAT=]
+      --completions <shell>  Print shell completion script for <shell> [env: DUFS_COMPLETIONS=] [possible values: bash, elvish, fish, powershell, zsh]
   -h, --help                 Print help information
   -V, --version              Print version information
 ```
diff --git a/src/args.rs b/src/args.rs
index 8e6699c..31dbe67 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -26,12 +26,14 @@ pub fn build_cli() -> Command {
         ))
         .arg(
             Arg::new("root")
+                .env("DUFS_ROOT")
                 .default_value(".")
                 .value_parser(value_parser!(PathBuf))
                 .help("Specific path to serve"),
         )
         .arg(
             Arg::new("bind")
+                .env("DUFS_BIND")
                 .short('b')
                 .long("bind")
                 .help("Specify bind address or unix socket")
@@ -41,6 +43,7 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("port")
+                .env("DUFS_PORT")
                 .short('p')
                 .long("port")
                 .default_value("5000")
@@ -50,18 +53,21 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("path-prefix")
+                .env("DUFS_PATH_PREFIX")
                 .long("path-prefix")
                 .value_name("path")
                 .help("Specify a path prefix"),
         )
         .arg(
             Arg::new("hidden")
+                .env("DUFS_HIDDEN")
                 .long("hidden")
                 .help("Hide paths from directory listings, separated by `,`")
                 .value_name("value"),
         )
         .arg(
             Arg::new("auth")
+                .env("DUFS_AUTH")
                 .short('a')
                 .long("auth")
                 .help("Add auth for path")
@@ -71,6 +77,7 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("auth-method")
+                .env("DUFS_AUTH_METHOD")
                 .long("auth-method")
                 .help("Select auth method")
                 .value_parser(PossibleValuesParser::new(["basic", "digest"]))
@@ -79,6 +86,7 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("allow-all")
+                .env("DUFS_ALLOW_ALL")
                 .short('A')
                 .long("allow-all")
                 .action(ArgAction::SetTrue)
@@ -86,60 +94,70 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("allow-upload")
+                .env("DUFS_ALLOW_UPLOAD")
                 .long("allow-upload")
                 .action(ArgAction::SetTrue)
                 .help("Allow upload files/folders"),
         )
         .arg(
             Arg::new("allow-delete")
+                .env("DUFS_ALLOW_DELETE")
                 .long("allow-delete")
                 .action(ArgAction::SetTrue)
                 .help("Allow delete files/folders"),
         )
         .arg(
             Arg::new("allow-search")
+                .env("DUFS_ALLOW_SEARCH")
                 .long("allow-search")
                 .action(ArgAction::SetTrue)
                 .help("Allow search files/folders"),
         )
         .arg(
             Arg::new("allow-symlink")
+                .env("DUFS_ALLOW_SYMLINK")
                 .long("allow-symlink")
                 .action(ArgAction::SetTrue)
                 .help("Allow symlink to files/folders outside root directory"),
         )
         .arg(
             Arg::new("allow-archive")
+                .env("DUFS_ALLOW_ARCHIVE")
                 .long("allow-archive")
                 .action(ArgAction::SetTrue)
                 .help("Allow zip archive generation"),
         )
         .arg(
             Arg::new("enable-cors")
+                .env("DUFS_ENABLE_CORS")
                 .long("enable-cors")
                 .action(ArgAction::SetTrue)
                 .help("Enable CORS, sets `Access-Control-Allow-Origin: *`"),
         )
         .arg(
             Arg::new("render-index")
+                .env("DUFS_RENDER_INDEX")
                 .long("render-index")
                 .action(ArgAction::SetTrue)
                 .help("Serve index.html when requesting a directory, returns 404 if not found index.html"),
         )
         .arg(
             Arg::new("render-try-index")
+                .env("DUFS_RENDER_TRY_INDEX")
                 .long("render-try-index")
                 .action(ArgAction::SetTrue)
                 .help("Serve index.html when requesting a directory, returns directory listing if not found index.html"),
         )
         .arg(
             Arg::new("render-spa")
+                .env("DUFS_RENDER_SPA")
                 .long("render-spa")
                 .action(ArgAction::SetTrue)
                 .help("Serve SPA(Single Page Application)"),
         )
         .arg(
             Arg::new("assets")
+                .env("DUFS_ASSETS")
                 .long("assets")
                 .help("Use custom assets to override builtin assets")
                 .value_parser(value_parser!(PathBuf))
@@ -150,6 +168,7 @@ pub fn build_cli() -> Command {
     let app = app
         .arg(
             Arg::new("tls-cert")
+                .env("DUFS_TLS_CERT")
                 .long("tls-cert")
                 .value_name("path")
                 .value_parser(value_parser!(PathBuf))
@@ -157,6 +176,7 @@ pub fn build_cli() -> Command {
         )
         .arg(
             Arg::new("tls-key")
+                .env("DUFS_TLS_KEY")
                 .long("tls-key")
                 .value_name("path")
                 .value_parser(value_parser!(PathBuf))
@@ -165,12 +185,14 @@ pub fn build_cli() -> Command {
 
     app.arg(
         Arg::new("log-format")
+            .env("DUFS_LOG_FORMAT")
             .long("log-format")
             .value_name("format")
             .help("Customize http log format"),
     )
     .arg(
         Arg::new("completions")
+            .env("DUFS_COMPLETIONS")
             .long("completions")
             .value_name("shell")
             .value_parser(value_parser!(Shell))