1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#![allow(clippy::struct_excessive_bools)]
use clap::Parser;
use std::{
    fs,
    io::{self, BufRead},
};

#[derive(Parser, Debug, Clone)]
pub struct Cmd {
    /// name of env var to update
    #[arg(long)]
    pub name: String,
    /// value of env var to update, if not provided stdin is used
    #[arg(long)]
    pub value: Option<String>,
    /// Path to .env file
    #[arg(long, default_value = ".env")]
    pub env_file: std::path::PathBuf,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error(transparent)]
    Io(#[from] io::Error),
}

impl Cmd {
    pub fn run(&self) -> Result<(), Error> {
        let file = &self.env_file;
        let env_file = if file.exists() {
            fs::read_to_string(file)?
        } else {
            String::new()
        };

        let value = self.value.clone().unwrap_or_else(|| {
            // read from stdin
            std::io::stdin()
                .lock()
                .lines()
                .next()
                .expect("stdin closed")
                .expect("stdin error")
        });
        let name = &self.name;
        let new_env_file =
            replace_lines_starting_with(&env_file, &format!("{name}="), &format!("{name}={value}"));
        fs::write(&self.env_file, new_env_file)?;
        Ok(())
    }
}

fn replace_lines_starting_with(input: &str, starts_with: &str, replacement: &str) -> String {
    let mut found = false;
    let mut v = input
        .lines()
        .map(|line| {
            if line.starts_with(starts_with) {
                found = true;
                replacement
            } else {
                line
            }
        })
        .collect::<Vec<&str>>();
    if !found {
        v.push(replacement);
    }
    v.join("\n")
}