Skip to content

Commit

Permalink
Add the write-file command
Browse files Browse the repository at this point in the history
  • Loading branch information
snoyberg committed Mar 13, 2022
1 parent 8f8c1a1 commit a4842de
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log for Amber

## 0.1.3 (2022-03-13)

* Add the `write-file` command

## 0.1.2 (2022-01-18)

* Allow `encrypt` subcommand to take secret value from `stdin` [#15](https://github.com/fpco/amber/issues/15)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "amber"
version = "0.1.2"
version = "0.1.3"
authors = ["Michael Snoyman <[email protected]>"]
edition = "2018"
description = "Manage secret values in-repo via public key cryptography"
Expand Down
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ pub enum SubCommand {
/// Command line arguments to pass to the command
args: Vec<String>,
},
/// Write the contents of a secret to the given file.
WriteFile {
/// The key for the secret
#[clap(long)]
key: String,
/// File path to write to
#[clap(long)]
dest: PathBuf,
},
}

#[derive(Parser, Debug)]
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ impl Config {
.map(|plain| (key, plain))
})
}

/// Look up a specific secret value
pub(crate) fn get_secret(&self, key: &str, secret_key: &SecretKey) -> Result<String> {
self.secrets
.get(key)
.with_context(|| format!("Key does not exist: {}", key))
.and_then(|secret| secret.decrypt(&self.public_key, secret_key, key))
}
}

impl Secret {
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod config;
mod exec;
mod mask;

use std::io::Read;
use std::{io::Read, path::Path};

use anyhow::*;
use exec::CommandExecExt;
Expand Down Expand Up @@ -38,6 +38,7 @@ fn main() -> Result<()> {
cli::SubCommand::Remove { key } => remove(cmd.opt, key),
cli::SubCommand::Print { style } => print(cmd.opt, style),
cli::SubCommand::Exec { cmd: cmd_, args } => exec(cmd.opt, cmd_, args),
cli::SubCommand::WriteFile { key, dest } => write_file(cmd.opt, &key, &dest),
}
}

Expand Down Expand Up @@ -169,3 +170,11 @@ fn exec(mut opt: cli::Opt, cmd: String, args: Vec<String>) -> Result<()> {

Ok(())
}

fn write_file(mut opt: cli::Opt, key: &str, dest: &Path) -> Result<()> {
let config = config::Config::load(opt.find_amber_yaml()?)?;
let secret_key = config.load_secret_key()?;
let value = config.get_secret(key, &secret_key)?;
std::fs::write(dest, value)
.with_context(|| format!("Unable to write to file {}", dest.display()))
}

0 comments on commit a4842de

Please sign in to comment.