Skip to content

Commit

Permalink
Merge pull request #22 from psibi/ancestor-dirs
Browse files Browse the repository at this point in the history
Search amber.yaml in parent directory in some cases
  • Loading branch information
snoyberg authored Dec 22, 2021
2 parents ac59cb6 + 7b34de1 commit d1f7616
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
## 0.1.2 (UNRELEASED)

* Allow `encrypt` subcommand to take secret value from `stdin` [#15](https://github.com/fpco/amber/issues/15)
* Amber searches the parent directory for the amber.yaml file if
amber.yaml isn't present in the current working directory. This
check is only done when no explicit amber-yaml is specificed (unless
the specified amber yaml itself is amber.yaml which is the default
value)

## 0.1.1 (2021-08-31)

Expand Down
30 changes: 27 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::*;
use clap::Clap;
use once_cell::sync::Lazy;

Expand Down Expand Up @@ -88,15 +89,17 @@ static VERSION_SHA: Lazy<String> = Lazy::new(|| {
}
});

const DEFAULT_AMBER_YAML: &str = "amber.yaml";

/// Utility to store encrypted secrets in version trackable plain text files.
#[derive(Clap, Debug)]
pub struct Opt {
/// Turn on verbose output
#[clap(short, long, global = true)]
pub verbose: bool,
/// amber.yaml file location
#[clap(long, default_value = "amber.yaml", global = true, env = "AMBER_YAML")]
pub amber_yaml: PathBuf,
#[clap(long, global = true, env = "AMBER_YAML")]
pub amber_yaml: Option<PathBuf>,
/// Disable masking of secret values during exec
#[clap(long, global = true)]
pub unmasked: bool,
Expand All @@ -112,4 +115,25 @@ impl Opt {
builder.filter_module(env!("CARGO_CRATE_NAME"), level);
builder.target(Target::Stderr).init();
}

pub fn find_amber_yaml(&mut self) -> Result<&Path> {
if self.amber_yaml.is_none() {
for dir in std::env::current_dir()?.ancestors() {
let amber_yaml: PathBuf = dir.join(DEFAULT_AMBER_YAML);
log::debug!("Checking if file {:?} exists", &amber_yaml);
if amber_yaml.exists() {
self.amber_yaml = Some(amber_yaml);
break;
}
}
}
self.amber_yaml
.as_deref()
.with_context(|| format!("No file named {} found", DEFAULT_AMBER_YAML))
}

pub fn find_amber_yaml_or_default(&mut self) -> &Path {
self.amber_yaml
.get_or_insert_with(|| Path::new(DEFAULT_AMBER_YAML).to_owned())
}
}
26 changes: 14 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ fn main() -> Result<()> {
}
}

fn init(opt: cli::Opt) -> Result<()> {
fn init(mut opt: cli::Opt) -> Result<()> {
let (secret_key, config) = config::Config::new();
let secret_key = sodiumoxide::hex::encode(secret_key);

config.save(&opt.amber_yaml)?;
config.save(opt.find_amber_yaml_or_default())?;

eprintln!("Your secret key is: {}", secret_key);
eprintln!(
Expand All @@ -72,9 +72,10 @@ fn validate_key(key: &str) -> Result<()> {
}
}

fn encrypt(opt: cli::Opt, key: String, value: Option<String>) -> Result<()> {
fn encrypt(mut opt: cli::Opt, key: String, value: Option<String>) -> Result<()> {
validate_key(&key)?;
let mut config = config::Config::load(&opt.amber_yaml)?;
let amber_yaml = opt.find_amber_yaml()?;
let mut config = config::Config::load(amber_yaml)?;
let value = value.map_or_else(
|| {
log::debug!("No value provided on command line, taking from stdin");
Expand All @@ -88,7 +89,7 @@ fn encrypt(opt: cli::Opt, key: String, value: Option<String>) -> Result<()> {
Ok,
)?;
config.encrypt(key, &value);
config.save(&opt.amber_yaml)
config.save(amber_yaml)
}

fn generate(opt: cli::Opt, key: String) -> Result<()> {
Expand All @@ -100,15 +101,16 @@ fn generate(opt: cli::Opt, key: String) -> Result<()> {
Ok(res)
}

fn remove(opt: cli::Opt, key: String) -> Result<()> {
fn remove(mut opt: cli::Opt, key: String) -> Result<()> {
validate_key(&key)?;
let mut config = config::Config::load(&opt.amber_yaml)?;
let amber_yaml = opt.find_amber_yaml()?;
let mut config = config::Config::load(amber_yaml)?;
config.remove(&key);
config.save(&opt.amber_yaml)
config.save(amber_yaml)
}

fn print(opt: cli::Opt, style: cli::PrintStyle) -> Result<()> {
let config = config::Config::load(&opt.amber_yaml)?;
fn print(mut opt: cli::Opt, style: cli::PrintStyle) -> Result<()> {
let config = config::Config::load(opt.find_amber_yaml()?)?;
let secret = config.load_secret_key()?;
let pairs: Result<Vec<_>> = config.iter_secrets(&secret).collect();
let mut pairs = pairs?;
Expand Down Expand Up @@ -139,8 +141,8 @@ fn print(opt: cli::Opt, style: cli::PrintStyle) -> Result<()> {
Ok(())
}

fn exec(opt: cli::Opt, cmd: String, args: Vec<String>) -> Result<()> {
let config = config::Config::load(&opt.amber_yaml)?;
fn exec(mut opt: cli::Opt, cmd: String, args: Vec<String>) -> Result<()> {
let config = config::Config::load(opt.find_amber_yaml()?)?;
let secret_key = config.load_secret_key()?;

let mut cmd = std::process::Command::new(cmd);
Expand Down

0 comments on commit d1f7616

Please sign in to comment.