Skip to content

Commit

Permalink
feat(defaults): up defaults read - should read from stdin
Browse files Browse the repository at this point in the history
Nice easy way to convert plists to yaml.
  • Loading branch information
gibfahn committed Jul 3, 2023
1 parent ae664a1 commit 1e3ea9c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn get_env(
trace!("Provided env: {config_env:#?}");
let mut calculated_env = HashMap::new();
let home_dir = files::home_dir()?;
for (key, val) in config_env.iter() {
for (key, val) in config_env {
calculated_env.insert(
key.clone(),
shellexpand::full_with_context(
Expand Down
4 changes: 3 additions & 1 deletion src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,9 @@ pub struct DefaultsReadOptions {
/// Read from the global domain. If you set this, do not also pass a domain argument.
#[clap(short = 'g', long = "globalDomain")]
pub(crate) global_domain: bool,
/// Defaults domain to print.
/**
Defaults domain to print. Use `-` to read from stdin.
*/
pub(crate) domain: Option<String>,
/// Defaults key to print.
pub(crate) key: Option<String>,
Expand Down
16 changes: 15 additions & 1 deletion src/tasks/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use itertools::Itertools;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use std::collections::HashMap;
use std::io::Read;
use std::process::ExitStatus;
use thiserror::Error;
use tracing::debug;
Expand Down Expand Up @@ -382,7 +383,20 @@ pub(crate) fn read(current_host: bool, defaults_opts: DefaultsReadOptions) -> Re
let plist_path = plist_path(&domain, current_host)?;
debug!("Plist path: {plist_path}");

let plist: plist::Value = plist::from_file(&plist_path).map_err(|e| E::PlistRead {
let plist: plist::Value = if &plist_path == "-" {
// Read from stdin directly if specified.
let mut bytes = Vec::new();
_ = std::io::stdin()
.read_to_end(&mut bytes)
.map_err(|e| E::FileRead {
path: plist_path.clone(),
source: e,
})?;
plist::from_bytes(&bytes)
} else {
plist::from_file(&plist_path)
}
.map_err(|e| E::PlistRead {
path: plist_path,
source: e,
})?;
Expand Down
5 changes: 5 additions & 0 deletions src/tasks/defaults/plist_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ pub(super) fn plist_path(domain: &str, current_host: bool) -> Result<Utf8PathBuf
return Ok(Utf8PathBuf::from(domain));
}

// User wants to read from stdin, use that directly.
if domain == "-" {
return Ok(Utf8PathBuf::from(domain));
}

let home_dir = files::home_dir().map_err(|e| E::MissingHomeDir { source: e })?;

// Global Domain -> hardcoded value.
Expand Down

0 comments on commit 1e3ea9c

Please sign in to comment.