Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Commit

Permalink
Merge pull request #215 from tendermint/config-env-var
Browse files Browse the repository at this point in the history
config: Allow setting config path via TMKMS_CONFIG_FILE
  • Loading branch information
liamsi authored Mar 11, 2019
2 parents d9f03d7 + c980d47 commit 1b1b211
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
15 changes: 11 additions & 4 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub use self::yubihsm::YubihsmCommand;
pub use self::{
help::HelpCommand, keygen::KeygenCommand, start::StartCommand, version::VersionCommand,
};
use crate::config::{KmsConfig, CONFIG_FILE_NAME};
use crate::config::{KmsConfig, CONFIG_ENV_VAR, CONFIG_FILE_NAME};
use abscissa::{Callable, LoadConfig};
use std::path::PathBuf;
use std::{env, path::PathBuf};

/// Subcommands of the KMS command-line application
#[derive(Debug, Options)]
Expand Down Expand Up @@ -56,13 +56,20 @@ impl LoadConfig<KmsConfig> for KmsCommand {
/// or the default
fn config_path(&self) -> Option<PathBuf> {
let config = match self {
KmsCommand::Start(run) => run.config.as_ref().map(|s| s.as_ref()),
KmsCommand::Start(run) => run.config.as_ref(),
#[cfg(feature = "yubihsm")]
KmsCommand::Yubihsm(yubihsm) => yubihsm.config_path(),
_ => return None,
};

Some(PathBuf::from(config.unwrap_or(CONFIG_FILE_NAME)))
let path = PathBuf::from(
config
.cloned()
.or_else(|| env::var(CONFIG_ENV_VAR).ok())
.unwrap_or_else(|| CONFIG_FILE_NAME.to_owned()),
);

Some(path)
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/commands/yubihsm/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ pub enum KeysCommand {

impl KeysCommand {
/// Optional path to the configuration file
pub(super) fn config_path(&self) -> Option<&str> {
pub(super) fn config_path(&self) -> Option<&String> {
match self {
KeysCommand::Export(export) => export.config.as_ref().map(|s| s.as_ref()),
KeysCommand::Generate(generate) => generate.config.as_ref().map(|s| s.as_ref()),
KeysCommand::List(list) => list.config.as_ref().map(|s| s.as_ref()),
KeysCommand::Import(import) => import.config.as_ref().map(|s| s.as_ref()),
KeysCommand::Export(export) => export.config.as_ref(),
KeysCommand::Generate(generate) => generate.config.as_ref(),
KeysCommand::List(list) => list.config.as_ref(),
KeysCommand::Import(import) => import.config.as_ref(),
_ => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/yubihsm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ impl Callable for YubihsmCommand {
}

impl YubihsmCommand {
pub(super) fn config_path(&self) -> Option<&str> {
pub(super) fn config_path(&self) -> Option<&String> {
match self {
YubihsmCommand::Keys(keys) => keys.config_path(),
YubihsmCommand::Setup(setup) => setup.config.as_ref().map(|s| s.as_ref()),
YubihsmCommand::Test(test) => test.config.as_ref().map(|s| s.as_ref()),
YubihsmCommand::Setup(setup) => setup.config.as_ref(),
YubihsmCommand::Test(test) => test.config.as_ref(),
_ => None,
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ pub mod validator;
pub use self::validator::*;
use self::{chain::ChainConfig, provider::ProviderConfig};

/// Environment variable containing path to config file
pub const CONFIG_ENV_VAR: &str = "TMKMS_CONFIG_FILE";

/// Name of the KMS configuration file
pub const CONFIG_FILE_NAME: &str = "tmkms.toml";

Expand Down

0 comments on commit 1b1b211

Please sign in to comment.