Skip to content

Commit

Permalink
feat: Allow setting extra repository options via env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Feb 25, 2024
1 parent a39a14d commit c9ff80b
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ simplelog = "0.12"
bytesize = "1"
clap = { version = "4", features = ["derive", "env", "wrap_help"] }
clap_complete = "4"
convert_case = "0.6.0"
dialoguer = "0.11.0"
directories = "5"
gethostname = "0.4"
Expand Down
12 changes: 9 additions & 3 deletions config/full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ warm-up = false
warm-up-command = "warmup.sh %id" # Default: not set
warm-up-wait = "10min" # Default: not set

# Additional repository options - depending on backend. These can be only set in the config file.
# Additional repository options - depending on backend. These can be only set in the config file or using env variables.
# For env variables use upper snake case and prefix with "RUSTIC_REPO_OPT_", e.g. `use-passwort = "true"` becomes
# `RUSTIC_REPO_OPT_USE_PASSWORT=true`
[repository.options]
post-create-command = "par2create -qq -n1 -r5 %file" # Only local backend; Default: not set
post-delete-command = "sh -c \"rm -f %file*.par2\"" # Only local backend; Default: not set
Expand All @@ -48,11 +50,15 @@ rest-url = "http://localhost:8000" # Only rclone; Default: determine REST URL fr
# Note that opendal backends use several service-dependent options which may be specified here, see
# https://opendal.apache.org/docs/rust/opendal/services/index.html

# Additional repository options for the hot part - depending on backend. These can be only set in the config file.
# Additional repository options for the hot part - depending on backend. These can be only set in the config file or
# using env variables.
# For env variables use upper snake case and prefix with "RUSTIC_REPO_OPTHOT_"
[repository.options-hot]
# see [repository.options]

# Additional repository options for the cold part - depending on backend. These can be only set in the config file.
# Additional repository options for the cold part - depending on backend. These can be only set in the config file or
# using env variables.
# For env variables use upper snake case and prefix with "RUSTIC_REPO_OPTCOLD_"
[repository.options-cold]
# see [repository.options]

Expand Down
4 changes: 2 additions & 2 deletions config/services/b2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ password = "<rustic_passwd>"
[repository.options]
# Here, we give the required b2 options, see https://opendal.apache.org/docs/rust/opendal/services/struct.B2.html
application_key_id = "my_id" # B2 application key ID
application_key = "my_key" # B2 application key secret
application_key = "my_key" # B2 application key secret. Can be also set using OPENDAL_APPLICATION_KEY
bucket = "bucket_name" # B2 bucket name
bucket_id = "bucket_id" # B2 bucket ID
# root = "/" # Set a repository root directory if not using the root directory of the bucket
# root = "/" # Set a repository root directory if not using the root directory of the bucket
18 changes: 18 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use clap::builder::{
styling::{AnsiColor, Effects},
Styles,
};
use convert_case::{Case, Casing};
use dialoguer::Password;
use human_panic::setup_panic;
use log::{log, Level};
Expand Down Expand Up @@ -185,6 +186,23 @@ impl Configurable<RusticConfig> for EntryPoint {
// That's why it says `_config`, because it's not read at all and therefore not needed.
let mut config = self.config.clone();

// collect "RUSTIC_REPO_OPT*" and "OPENDAL_*" env variables
for (var, value) in std::env::vars() {
if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPT_") {
let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
_ = config.repository.be.options.insert(var, value);
} else if let Some(var) = var.strip_prefix("OPENDAL_") {
let var = var.from_case(Case::UpperSnake).to_case(Case::Snake);
_ = config.repository.be.options.insert(var, value);
} else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTHOT_") {
let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
_ = config.repository.be.options_hot.insert(var, value);
} else if let Some(var) = var.strip_prefix("RUSTIC_REPO_OPTCOLD_") {
let var = var.from_case(Case::UpperSnake).to_case(Case::Kebab);
_ = config.repository.be.options_cold.insert(var, value);
}
}

// collect logs during merging as we start the logger *after* merging
let mut merge_logs = Vec::new();

Expand Down

0 comments on commit c9ff80b

Please sign in to comment.