Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: let custom binaries add extra default configs #2115

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2587,6 +2587,7 @@ pub fn handle_command_result(
pub struct CliRunner {
tracing_subscription: TracingSubscription,
app: Command,
extra_configs: Option<config::Config>,
store_factories: Option<StoreFactories>,
dispatch_fn: CliDispatchFn,
process_global_args_fns: Vec<ProcessGlobalArgsFn>,
Expand All @@ -2605,6 +2606,7 @@ impl CliRunner {
CliRunner {
tracing_subscription,
app: crate::commands::default_app(),
extra_configs: None,
store_factories: None,
dispatch_fn: Box::new(crate::commands::run_command),
process_global_args_fns: vec![],
Expand All @@ -2617,6 +2619,12 @@ impl CliRunner {
self
}

/// Adds default configs in addition to the normal defaults.
pub fn set_extra_config(mut self, extra_configs: config::Config) -> Self {
self.extra_configs = Some(extra_configs);
self
}

/// Replaces `StoreFactories` to be used.
pub fn set_store_factories(mut self, store_factories: StoreFactories) -> Self {
self.store_factories = Some(store_factories);
Expand Down Expand Up @@ -2709,8 +2717,16 @@ impl CliRunner {

#[must_use]
#[instrument(skip(self))]
pub fn run(self) -> ExitCode {
let layered_configs = LayeredConfigs::from_environment();
pub fn run(mut self) -> ExitCode {
let mut default_config = crate::config::default_config();
if let Some(extra_configs) = self.extra_configs.take() {
default_config = config::Config::builder()
.add_source(default_config)
.add_source(extra_configs)
.build()
.unwrap();
}
let layered_configs = LayeredConfigs::from_environment(default_config);
let mut ui = Ui::with_config(&layered_configs.merge())
.expect("default config should be valid, env vars are stringly typed");
let result = self.run_internal(&mut ui, layered_configs);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ pub struct LayeredConfigs {

impl LayeredConfigs {
/// Initializes configs with infallible sources.
pub fn from_environment() -> Self {
pub fn from_environment(default: config::Config) -> Self {
LayeredConfigs {
default: default_config(),
default,
env_base: env_base(),
user: None,
repo: None,
Expand Down