Skip to content

Commit

Permalink
aider: feat: add envset get command to read a single environment va…
Browse files Browse the repository at this point in the history
…riable
  • Loading branch information
schpet committed Sep 8, 2024
1 parent b154edb commit dbd2f90
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
85 changes: 58 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ mod tests;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,

/// Do not overwrite existing variables
#[arg(short, long)]
no_overwrite: bool,
Expand All @@ -26,43 +29,71 @@ struct Cli {
vars: Vec<String>,
}

#[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<String, String>, updated: &HashMap<String, String>) {
Expand Down
11 changes: 11 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

0 comments on commit dbd2f90

Please sign in to comment.