refactor: Environment variable interpolation in config file works globally, not based on type
This commit is contained in:
@@ -89,6 +89,7 @@ gman aws sts get-caller-identity
|
|||||||
- [Features](#features)
|
- [Features](#features)
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
|
- [Environment Variable Interpolation](#environment-variable-interpolation)
|
||||||
- [Providers](#providers)
|
- [Providers](#providers)
|
||||||
- [Local](#provider-local)
|
- [Local](#provider-local)
|
||||||
- [AWS Secrets Manager](#provider-aws_secrets_manager)
|
- [AWS Secrets Manager](#provider-aws_secrets_manager)
|
||||||
@@ -264,9 +265,6 @@ providers:
|
|||||||
aws_region: us-east-1
|
aws_region: us-east-1
|
||||||
```
|
```
|
||||||
|
|
||||||
**Important Note:** Environment variable interpolation is only supported in string or numeric fields. It is not
|
|
||||||
supported in lists or maps.
|
|
||||||
|
|
||||||
## Providers
|
## Providers
|
||||||
`gman` supports multiple providers for secret storage. The default provider is `local`, which stores secrets in an
|
`gman` supports multiple providers for secret storage. The default provider is `local`, which stores secrets in an
|
||||||
encrypted file on your filesystem. The CLI and config format are designed to be extensible so new providers can be
|
encrypted file on your filesystem. The CLI and config format are designed to be extensible so new providers can be
|
||||||
|
|||||||
+5
-5
@@ -1,8 +1,8 @@
|
|||||||
use crate::command::preview_command;
|
use crate::command::preview_command;
|
||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use clap_complete::CompletionCandidate;
|
use clap_complete::CompletionCandidate;
|
||||||
use futures::future::join_all;
|
use futures::future::join_all;
|
||||||
use gman::config::{Config, RunConfig, load_config};
|
use gman::config::{load_config, Config, RunConfig};
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -257,7 +257,7 @@ pub fn parse_args(
|
|||||||
|
|
||||||
pub fn run_config_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
pub fn run_config_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||||
let cur = current.to_string_lossy();
|
let cur = current.to_string_lossy();
|
||||||
match load_config() {
|
match load_config(true) {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
if let Some(run_configs) = config.run_configs {
|
if let Some(run_configs) = config.run_configs {
|
||||||
run_configs
|
run_configs
|
||||||
@@ -282,7 +282,7 @@ pub fn run_config_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
|||||||
|
|
||||||
pub fn provider_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
pub fn provider_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||||
let cur = current.to_string_lossy();
|
let cur = current.to_string_lossy();
|
||||||
match load_config() {
|
match load_config(true) {
|
||||||
Ok(config) => config
|
Ok(config) => config
|
||||||
.providers
|
.providers
|
||||||
.iter()
|
.iter()
|
||||||
@@ -300,7 +300,7 @@ pub fn provider_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
|||||||
|
|
||||||
pub fn secrets_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
pub fn secrets_completer(current: &OsStr) -> Vec<CompletionCandidate> {
|
||||||
let cur = current.to_string_lossy();
|
let cur = current.to_string_lossy();
|
||||||
match load_config() {
|
match load_config(true) {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
let mut provider_config = match config.extract_provider_config(None) {
|
let mut provider_config = match config.extract_provider_config(None) {
|
||||||
Ok(pc) => pc,
|
Ok(pc) => pc,
|
||||||
|
|||||||
+6
-17
@@ -4,12 +4,12 @@ use crate::cli::secrets_completer;
|
|||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use clap::Subcommand;
|
use clap::Subcommand;
|
||||||
use clap::{
|
use clap::{
|
||||||
CommandFactory, Parser, ValueEnum, crate_authors, crate_description, crate_name, crate_version,
|
crate_authors, crate_description, crate_name, crate_version, CommandFactory, Parser, ValueEnum,
|
||||||
};
|
};
|
||||||
use clap_complete::{ArgValueCompleter, CompleteEnv};
|
use clap_complete::{ArgValueCompleter, CompleteEnv};
|
||||||
use crossterm::execute;
|
use crossterm::execute;
|
||||||
use crossterm::terminal::{LeaveAlternateScreen, disable_raw_mode};
|
use crossterm::terminal::{disable_raw_mode, LeaveAlternateScreen};
|
||||||
use gman::config::{Config, get_config_file_path, load_config};
|
use gman::config::{get_config_file_path, load_config, Config};
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::io::{self, IsTerminal, Read, Write};
|
use std::io::{self, IsTerminal, Read, Write};
|
||||||
use std::panic::PanicHookInfo;
|
use std::panic::PanicHookInfo;
|
||||||
@@ -123,13 +123,6 @@ enum Commands {
|
|||||||
/// configured in a corresponding run profile
|
/// configured in a corresponding run profile
|
||||||
#[command(external_subcommand)]
|
#[command(external_subcommand)]
|
||||||
External(Vec<OsString>),
|
External(Vec<OsString>),
|
||||||
|
|
||||||
/// Generate shell completion scripts
|
|
||||||
Completions {
|
|
||||||
/// The shell to generate the script for
|
|
||||||
#[arg(value_enum)]
|
|
||||||
shell: clap_complete::Shell,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -157,7 +150,7 @@ async fn main() -> Result<()> {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let config = load_config()?;
|
let config = load_config(true)?;
|
||||||
let mut provider_config = config.extract_provider_config(cli.provider.clone())?;
|
let mut provider_config = config.extract_provider_config(cli.provider.clone())?;
|
||||||
let secrets_provider = provider_config.extract_provider();
|
let secrets_provider = provider_config.extract_provider();
|
||||||
|
|
||||||
@@ -238,7 +231,8 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Commands::Config {} => {
|
Commands::Config {} => {
|
||||||
let config_yaml = serde_yaml::to_string(&config)
|
let uninterpolated_config = load_config(false)?;
|
||||||
|
let config_yaml = serde_yaml::to_string(&uninterpolated_config)
|
||||||
.with_context(|| "failed to serialize existing configuration")?;
|
.with_context(|| "failed to serialize existing configuration")?;
|
||||||
let new_config = Editor::new()
|
let new_config = Editor::new()
|
||||||
.edit(&config_yaml)
|
.edit(&config_yaml)
|
||||||
@@ -267,11 +261,6 @@ async fn main() -> Result<()> {
|
|||||||
Commands::External(tokens) => {
|
Commands::External(tokens) => {
|
||||||
wrap_and_run_command(cli.provider, &config, tokens, cli.profile, cli.dry_run).await?;
|
wrap_and_run_command(cli.provider, &config, tokens, cli.profile, cli.dry_run).await?;
|
||||||
}
|
}
|
||||||
Commands::Completions { shell } => {
|
|
||||||
let mut cmd = Cli::command();
|
|
||||||
let bin_name = cmd.get_name().to_string();
|
|
||||||
clap_complete::generate(shell, &mut cmd, bin_name, &mut io::stdout());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+27
-313
@@ -46,19 +46,14 @@ use validator::{Validate, ValidationError};
|
|||||||
#[validate(schema(function = "flags_or_files"))]
|
#[validate(schema(function = "flags_or_files"))]
|
||||||
pub struct RunConfig {
|
pub struct RunConfig {
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub name: Option<String>,
|
pub name: Option<String>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub provider: Option<String>,
|
pub provider: Option<String>,
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
pub secrets: Option<Vec<String>>,
|
pub secrets: Option<Vec<String>>,
|
||||||
pub files: Option<Vec<PathBuf>>,
|
pub files: Option<Vec<PathBuf>>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub flag: Option<String>,
|
pub flag: Option<String>,
|
||||||
#[validate(range(min = 1))]
|
#[validate(range(min = 1))]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_usize_env_var")]
|
|
||||||
pub flag_position: Option<usize>,
|
pub flag_position: Option<usize>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub arg_format: Option<String>,
|
pub arg_format: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +193,6 @@ impl ProviderConfig {
|
|||||||
#[validate(schema(function = "default_provider_exists"))]
|
#[validate(schema(function = "default_provider_exists"))]
|
||||||
#[validate(schema(function = "providers_names_are_unique"))]
|
#[validate(schema(function = "providers_names_are_unique"))]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
#[serde(deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub default_provider: Option<String>,
|
pub default_provider: Option<String>,
|
||||||
#[validate(length(min = 1))]
|
#[validate(length(min = 1))]
|
||||||
#[validate(nested)]
|
#[validate(nested)]
|
||||||
@@ -293,10 +287,11 @@ impl Config {
|
|||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use gman::config::load_config;
|
/// # use gman::config::load_config;
|
||||||
/// let config = load_config().unwrap();
|
/// // Load config with environment variable interpolation enabled
|
||||||
|
/// let config = load_config(true).unwrap();
|
||||||
/// println!("loaded config: {:?}", config);
|
/// println!("loaded config: {:?}", config);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn load_config() -> Result<Config> {
|
pub fn load_config(interpolate: bool) -> Result<Config> {
|
||||||
let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from);
|
let xdg_path = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from);
|
||||||
|
|
||||||
let mut config: Config = if let Some(base) = xdg_path.as_ref() {
|
let mut config: Config = if let Some(base) = xdg_path.as_ref() {
|
||||||
@@ -305,17 +300,22 @@ pub fn load_config() -> Result<Config> {
|
|||||||
let yaml = app_dir.join("config.yaml");
|
let yaml = app_dir.join("config.yaml");
|
||||||
if yml.exists() || yaml.exists() {
|
if yml.exists() || yaml.exists() {
|
||||||
let load_path = if yml.exists() { &yml } else { &yaml };
|
let load_path = if yml.exists() { &yml } else { &yaml };
|
||||||
let content = fs::read_to_string(load_path)
|
let mut content =
|
||||||
.with_context(|| format!("failed to read config file '{}'", load_path.display()))?;
|
fs::read_to_string(load_path).with_context(|| {
|
||||||
|
format!("failed to read config file '{}'", load_path.display())
|
||||||
|
})?;
|
||||||
|
if interpolate {
|
||||||
|
content = interpolate_env_vars(&content);
|
||||||
|
}
|
||||||
let cfg: Config = serde_yaml::from_str(&content).with_context(|| {
|
let cfg: Config = serde_yaml::from_str(&content).with_context(|| {
|
||||||
format!("failed to parse YAML config at '{}'", load_path.display())
|
format!("failed to parse YAML config at '{}'", load_path.display())
|
||||||
})?;
|
})?;
|
||||||
cfg
|
cfg
|
||||||
} else {
|
} else {
|
||||||
confy::load("gman", "config")?
|
load_confy_config(interpolate)?
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
confy::load("gman", "config")?
|
load_confy_config(interpolate)?
|
||||||
};
|
};
|
||||||
|
|
||||||
config.validate()?;
|
config.validate()?;
|
||||||
@@ -338,6 +338,20 @@ pub fn load_config() -> Result<Config> {
|
|||||||
Ok(config)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn load_confy_config(interpolate: bool) -> Result<Config> {
|
||||||
|
let load_path = confy::get_configuration_file_path("gman", "config")?;
|
||||||
|
let mut content =
|
||||||
|
fs::read_to_string(&load_path)
|
||||||
|
.with_context(|| format!("failed to read config file '{}'", load_path.display()))?;
|
||||||
|
if interpolate {
|
||||||
|
content = interpolate_env_vars(&content);
|
||||||
|
}
|
||||||
|
let cfg: Config = serde_yaml::from_str(&content)
|
||||||
|
.with_context(|| format!("failed to parse YAML config at '{}'", load_path.display()))?;
|
||||||
|
|
||||||
|
Ok(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the configuration file path that `confy` will use
|
/// Returns the configuration file path that `confy` will use
|
||||||
pub fn get_config_file_path() -> Result<PathBuf> {
|
pub fn get_config_file_path() -> Result<PathBuf> {
|
||||||
if let Some(base) = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from) {
|
if let Some(base) = env::var_os("XDG_CONFIG_HOME").map(PathBuf::from) {
|
||||||
@@ -352,53 +366,6 @@ pub fn get_config_file_path() -> Result<PathBuf> {
|
|||||||
Ok(confy::get_configuration_file_path("gman", "config")?)
|
Ok(confy::get_configuration_file_path("gman", "config")?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deserialize_optional_env_var<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
|
|
||||||
where
|
|
||||||
D: serde::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let s: Option<String> = Option::deserialize(deserializer)?;
|
|
||||||
match s {
|
|
||||||
Some(value) => {
|
|
||||||
let interpolated = interpolate_env_vars(&value);
|
|
||||||
Ok(Some(interpolated))
|
|
||||||
}
|
|
||||||
None => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deserialize_optional_pathbuf_env_var<'de, D>(
|
|
||||||
deserializer: D,
|
|
||||||
) -> Result<Option<PathBuf>, D::Error>
|
|
||||||
where
|
|
||||||
D: serde::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let s: Option<String> = Option::deserialize(deserializer)?;
|
|
||||||
match s {
|
|
||||||
Some(value) => {
|
|
||||||
let interpolated = interpolate_env_vars(&value);
|
|
||||||
Ok(Some(interpolated.parse().unwrap()))
|
|
||||||
}
|
|
||||||
None => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_optional_usize_env_var<'de, D>(deserializer: D) -> Result<Option<usize>, D::Error>
|
|
||||||
where
|
|
||||||
D: serde::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
let s: Option<String> = Option::deserialize(deserializer)?;
|
|
||||||
match s {
|
|
||||||
Some(value) => {
|
|
||||||
let interpolated = interpolate_env_vars(&value);
|
|
||||||
interpolated
|
|
||||||
.parse::<usize>()
|
|
||||||
.map(Some)
|
|
||||||
.map_err(serde::de::Error::custom)
|
|
||||||
}
|
|
||||||
None => Ok(None),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn interpolate_env_vars(s: &str) -> String {
|
pub fn interpolate_env_vars(s: &str) -> String {
|
||||||
let result = s.to_string();
|
let result = s.to_string();
|
||||||
let scrubbing_regex = Regex::new(r#"[\s{}^()\[\]\\|`'"]+"#).unwrap();
|
let scrubbing_regex = Regex::new(r#"[\s{}^()\[\]\\|`'"]+"#).unwrap();
|
||||||
@@ -430,261 +397,8 @@ pub fn interpolate_env_vars(s: &str) -> String {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use indoc::indoc;
|
use pretty_assertions::assert_str_eq;
|
||||||
use pretty_assertions::{assert_eq, assert_str_eq};
|
|
||||||
use serde::Deserialize;
|
|
||||||
use serial_test::serial;
|
use serial_test::serial;
|
||||||
use std::path::PathBuf;
|
|
||||||
|
|
||||||
#[derive(Default, Deserialize, PartialEq, Eq, Debug)]
|
|
||||||
struct TestConfig {
|
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
string_var: Option<String>,
|
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_pathbuf_env_var")]
|
|
||||||
path_var: Option<PathBuf>,
|
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_usize_env_var")]
|
|
||||||
usize_var: Option<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_env_var_is_present() {
|
|
||||||
unsafe { env::set_var("TEST_VAR_DESERIALIZE_OPTION", "localhost") };
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: ${TEST_VAR_DESERIALIZE_OPTION}
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.string_var, Some("localhost".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_env_var_empty_env_var_uses_default_value_if_provided() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: ${TEST_VAR_DESERIALIZE_OPTION_UNDEFINED:-localhost}
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.string_var, Some("localhost".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_env_var_does_not_overwrite_non_env_value() {
|
|
||||||
unsafe { env::set_var("TEST_VAR_DESERIALIZE_OPTION_NO_OVERWRITE", "localhost") };
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: www.example.com
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.string_var, Some("www.example.com".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION_NO_OVERWRITE") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_env_var_empty() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.string_var, None);
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_pathbuf_env_var_is_present() {
|
|
||||||
unsafe { env::set_var("TEST_VAR_DESERIALIZE_OPTION_PATHBUF", "/some/path") };
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: ${TEST_VAR_DESERIALIZE_OPTION_PATHBUF}
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION_PATHBUF") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_pathbuf_env_var_empty_env_var_uses_default_value_if_provided() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: ${TEST_VAR_DESERIALIZE_OPTION_PATHBUF_UNDEFINED:-/some/path}
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_pathbuf_env_var_does_not_overwrite_non_env_value() {
|
|
||||||
unsafe {
|
|
||||||
env::set_var(
|
|
||||||
"TEST_VAR_DESERIALIZE_OPTION_PATHBUF_NO_OVERWRITE",
|
|
||||||
"/something/else",
|
|
||||||
)
|
|
||||||
};
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION_PATHBUF_NO_OVERWRITE") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_pathbuf_env_var_empty() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.path_var, None);
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_usize_env_var_is_present() {
|
|
||||||
unsafe { env::set_var("TEST_VAR_DESERIALIZE_OPTION_USIZE", "123") };
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: ${TEST_VAR_DESERIALIZE_OPTION_USIZE}
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION_USIZE") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_usize_env_var_uses_default_value_if_provided() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: ${TEST_VAR_DESERIALIZE_OPTION_USIZE_UNDEFINED:-123}
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
#[serial]
|
|
||||||
fn test_deserialize_optional_usize_env_var_does_not_overwrite_non_env_value() {
|
|
||||||
unsafe { env::set_var("TEST_VAR_DESERIALIZE_OPTION_NO_OVERWRITE_USIZE", "456") };
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: 123
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.usize_var, Some(123));
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
unsafe { env::remove_var("TEST_VAR_DESERIALIZE_OPTION_NO_OVERWRITE_USIZE") };
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_usize_env_var_invalid_number() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
usize_var: "holo"
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
let result: Result<TestConfig, _> = serde_yaml::from_str(yaml_data);
|
|
||||||
|
|
||||||
assert!(result.is_err());
|
|
||||||
let err = result.unwrap_err().to_string();
|
|
||||||
assert!(err.contains("invalid digit found in string"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_deserialize_optional_usize_env_var_empty() {
|
|
||||||
let yaml_data = indoc!(
|
|
||||||
r#"
|
|
||||||
string_var: hithere
|
|
||||||
path_var: /some/path
|
|
||||||
"#
|
|
||||||
);
|
|
||||||
|
|
||||||
let config: TestConfig = serde_yaml::from_str(yaml_data).unwrap();
|
|
||||||
|
|
||||||
assert_eq!(config.usize_var, None);
|
|
||||||
assert_eq!(config.string_var, Some("hithere".to_string()));
|
|
||||||
assert_eq!(config.path_var, Some(PathBuf::from("/some/path")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_interpolate_env_vars_defaults_to_original_string_if_not_in_yaml_interpolation_format() {
|
fn test_interpolate_env_vars_defaults_to_original_string_if_not_in_yaml_interpolation_format() {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::config::deserialize_optional_env_var;
|
|
||||||
use crate::providers::SecretProvider;
|
use crate::providers::SecretProvider;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
@@ -33,10 +32,8 @@ use validator::Validate;
|
|||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AwsSecretsManagerProvider {
|
pub struct AwsSecretsManagerProvider {
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub aws_profile: Option<String>,
|
pub aws_profile: Option<String>,
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub aws_region: Option<String>,
|
pub aws_region: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::config::deserialize_optional_env_var;
|
|
||||||
use crate::providers::SecretProvider;
|
use crate::providers::SecretProvider;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use azure_identity::DefaultAzureCredential;
|
use azure_identity::DefaultAzureCredential;
|
||||||
@@ -31,7 +30,6 @@ use validator::Validate;
|
|||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AzureKeyVaultProvider {
|
pub struct AzureKeyVaultProvider {
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub vault_name: Option<String>,
|
pub vault_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::config::deserialize_optional_env_var;
|
|
||||||
use crate::providers::SecretProvider;
|
use crate::providers::SecretProvider;
|
||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{Context, Result, anyhow};
|
||||||
use gcloud_sdk::google::cloud::secretmanager::v1;
|
use gcloud_sdk::google::cloud::secretmanager::v1;
|
||||||
@@ -40,7 +39,6 @@ type SecretsManagerClient = GoogleApi<SecretManagerServiceClient<GoogleAuthMiddl
|
|||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct GcpSecretManagerProvider {
|
pub struct GcpSecretManagerProvider {
|
||||||
#[validate(required)]
|
#[validate(required)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub gcp_project_id: Option<String>,
|
pub gcp_project_id: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::config::deserialize_optional_env_var;
|
|
||||||
use crate::providers::{ENV_PATH, SecretProvider};
|
use crate::providers::{ENV_PATH, SecretProvider};
|
||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{Context, Result, anyhow};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@@ -29,7 +28,6 @@ use validator::Validate;
|
|||||||
#[derive(Debug, Default, Clone, Validate, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Default, Clone, Validate, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct GopassProvider {
|
pub struct GopassProvider {
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub store: Option<String>,
|
pub store: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-17
@@ -1,16 +1,14 @@
|
|||||||
use crate::config::deserialize_optional_env_var;
|
use anyhow::{anyhow, bail, Context};
|
||||||
use crate::config::deserialize_optional_pathbuf_env_var;
|
|
||||||
use anyhow::{Context, anyhow, bail};
|
|
||||||
use secrecy::{ExposeSecret, SecretString};
|
use secrecy::{ExposeSecret, SecretString};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
use zeroize::Zeroize;
|
use zeroize::Zeroize;
|
||||||
|
|
||||||
use crate::config::{Config, get_config_file_path, load_config};
|
use crate::config::{get_config_file_path, load_config, Config};
|
||||||
use crate::providers::git_sync::{
|
use crate::providers::git_sync::{
|
||||||
SyncOpts, default_git_email, default_git_username, ensure_git_available, repo_name_from_url,
|
default_git_email, default_git_username, ensure_git_available, repo_name_from_url, resolve_git,
|
||||||
resolve_git, sync_and_push,
|
sync_and_push, SyncOpts,
|
||||||
};
|
};
|
||||||
use crate::providers::{SecretProvider, SupportedProvider};
|
use crate::providers::{SecretProvider, SupportedProvider};
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -18,13 +16,13 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use argon2::{Algorithm, Argon2, Params, Version};
|
use argon2::{Algorithm, Argon2, Params, Version};
|
||||||
use base64::{Engine as _, engine::general_purpose::STANDARD as B64};
|
use base64::{engine::general_purpose::STANDARD as B64, Engine as _};
|
||||||
use chacha20poly1305::aead::rand_core::RngCore;
|
use chacha20poly1305::aead::rand_core::RngCore;
|
||||||
use chacha20poly1305::{
|
use chacha20poly1305::{
|
||||||
Key, XChaCha20Poly1305, XNonce,
|
|
||||||
aead::{Aead, KeyInit, OsRng},
|
aead::{Aead, KeyInit, OsRng},
|
||||||
|
Key, XChaCha20Poly1305, XNonce,
|
||||||
};
|
};
|
||||||
use dialoguer::{Input, theme};
|
use dialoguer::{theme, Input};
|
||||||
use log::{debug, error};
|
use log::{debug, error};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_with::skip_serializing_none;
|
use serde_with::skip_serializing_none;
|
||||||
@@ -52,21 +50,14 @@ use validator::Validate;
|
|||||||
#[derive(Debug, Clone, Validate, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Validate, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct LocalProvider {
|
pub struct LocalProvider {
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_pathbuf_env_var")]
|
|
||||||
pub password_file: Option<PathBuf>,
|
pub password_file: Option<PathBuf>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub git_branch: Option<String>,
|
pub git_branch: Option<String>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub git_remote_url: Option<String>,
|
pub git_remote_url: Option<String>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub git_user_name: Option<String>,
|
pub git_user_name: Option<String>,
|
||||||
#[validate(email)]
|
#[validate(email)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub git_user_email: Option<String>,
|
pub git_user_email: Option<String>,
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_pathbuf_env_var")]
|
|
||||||
pub git_executable: Option<PathBuf>,
|
pub git_executable: Option<PathBuf>,
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
#[serde(default, deserialize_with = "deserialize_optional_env_var")]
|
|
||||||
pub runtime_provider_name: Option<String>,
|
pub runtime_provider_name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +247,7 @@ impl LocalProvider {
|
|||||||
fn persist_git_settings_to_config(&self) -> Result<()> {
|
fn persist_git_settings_to_config(&self) -> Result<()> {
|
||||||
debug!("Saving updated config (only current local provider)");
|
debug!("Saving updated config (only current local provider)");
|
||||||
|
|
||||||
let mut cfg = load_config().with_context(|| "failed to load existing config")?;
|
let mut cfg = load_config(true).with_context(|| "failed to load existing config")?;
|
||||||
|
|
||||||
let target_name = self.runtime_provider_name.clone();
|
let target_name = self.runtime_provider_name.clone();
|
||||||
let mut updated = false;
|
let mut updated = false;
|
||||||
|
|||||||
@@ -130,18 +130,6 @@ fn cli_shows_help() {
|
|||||||
.stdout(predicate::str::contains("Usage").or(predicate::str::contains("Add")));
|
.stdout(predicate::str::contains("Usage").or(predicate::str::contains("Add")));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn cli_completions_bash() {
|
|
||||||
let (_td, cfg, cache) = setup_env();
|
|
||||||
let mut cmd = Command::cargo_bin("gman").unwrap();
|
|
||||||
cmd.env("XDG_CACHE_HOME", &cache)
|
|
||||||
.env("XDG_CONFIG_HOME", &cfg)
|
|
||||||
.args(["completions", "bash"]);
|
|
||||||
cmd.assert()
|
|
||||||
.success()
|
|
||||||
.stdout(predicate::str::contains("_gman").or(predicate::str::contains("complete -F")));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn cli_add_get_list_update_delete_roundtrip() {
|
fn cli_add_get_list_update_delete_roundtrip() {
|
||||||
let (td, xdg_cfg, xdg_cache) = setup_env();
|
let (td, xdg_cfg, xdg_cache) = setup_env();
|
||||||
|
|||||||
Reference in New Issue
Block a user