From dbd2f9094aeb98505b73f572a1f6869610b23129 Mon Sep 17 00:00:00 2001 From: "Peter Schilling (aider)" Date: Sun, 8 Sep 2024 06:35:04 -0700 Subject: [PATCH] aider: feat: add `envset get` command to read a single environment variable --- src/main.rs | 85 +++++++++++++++++++++++++++++++++++----------------- src/tests.rs | 11 +++++++ 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index 95c8884..75d60ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,9 @@ mod tests; #[derive(Parser)] #[command(author, version, about, long_about = None)] struct Cli { + #[command(subcommand)] + command: Option, + /// Do not overwrite existing variables #[arg(short, long)] no_overwrite: bool, @@ -26,43 +29,71 @@ struct Cli { vars: Vec, } +#[derive(clap::Subcommand)] +enum Commands { + /// Get the value of a single environment variable + Get { key: String }, +} + fn main() { let cli = Cli::parse(); - let no_overwrite = cli.no_overwrite; - let env_file = &cli.file; - let (mut env_vars, original_lines) = match read_env_file(env_file) { - Ok(result) => result, - Err(e) => { - if e.kind() == std::io::ErrorKind::NotFound { - (HashMap::new(), Vec::new()) - } else { - eprintln!("Error reading .env file: {}", e); - process::exit(1); + match &cli.command { + Some(Commands::Get { key }) => { + let env_file = &cli.file; + let (env_vars, _) = match read_env_file(env_file) { + Ok(result) => result, + Err(e) => { + eprintln!("Error reading .env file: {}", e); + process::exit(1); + } + }; + + match env_vars.get(key) { + Some(value) => println!("{}", value), + None => { + eprintln!("Environment variable '{}' not found", key); + process::exit(1); + } } } - }; + None => { + let no_overwrite = cli.no_overwrite; + let env_file = &cli.file; + let (mut env_vars, original_lines) = match read_env_file(env_file) { + Ok(result) => result, + Err(e) => { + if e.kind() == std::io::ErrorKind::NotFound { + (HashMap::new(), Vec::new()) + } else { + eprintln!("Error reading .env file: {}", e); + process::exit(1); + } + } + }; - let original_env = env_vars.clone(); + let original_env = env_vars.clone(); - let new_vars = if atty::is(Stream::Stdin) { - parse_args(&cli.vars) - } else { - parse_stdin() - }; + let new_vars = if atty::is(Stream::Stdin) { + parse_args(&cli.vars) + } else { + parse_stdin() + }; - for (key, value) in &new_vars { - if !env_vars.contains_key(key as &str) || !no_overwrite { - env_vars.insert(key.clone(), value.clone()); - } - } + for (key, value) in &new_vars { + if !env_vars.contains_key(key as &str) || !no_overwrite { + env_vars.insert(key.clone(), value.clone()); + } + } - if let Err(e) = write_env_file(env_file, &env_vars, &original_lines) { - eprintln!("Error writing .env file: {}", e); - process::exit(1); - } + if let Err(e) = write_env_file(env_file, &env_vars, &original_lines) { + eprintln!("Error writing .env file: {}", e); + process::exit(1); + } - print_diff(&original_env, &env_vars); + print_diff(&original_env, &env_vars); + } + } } fn print_diff(original: &HashMap, updated: &HashMap) { diff --git a/src/tests.rs b/src/tests.rs index 4147472..5deae0b 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -179,3 +179,14 @@ fn test_keep_last_occurrence_of_duplicate_keys() { let final_content = fs::read_to_string(&file_path).unwrap(); assert_eq!(final_content, "A=a\nFOO=3\nB=b\n"); } +#[test] +fn test_get_single_env_var() { + let dir = tempdir().unwrap(); + let file_path = dir.path().join(".env"); + let mut file = File::create(&file_path).unwrap(); + writeln!(file, "FOO=bar\nBAZ=qux").unwrap(); + + let (env_vars, _) = read_env_file(file_path.to_str().unwrap()).unwrap(); + assert_eq!(env_vars.get("FOO"), Some(&"bar".to_string())); + assert_eq!(env_vars.get("BAZ"), Some(&"qux".to_string())); +}