Skip to content

Commit

Permalink
Merge pull request #35 from tsirysndr/feat/load-secrets-from-env
Browse files Browse the repository at this point in the history
feat: load secrets from env
  • Loading branch information
tsirysndr committed Feb 20, 2024
2 parents d5bf9b6 + 48b8b9c commit 65e3e1c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ You can use EnvHub as a [GitHub Action](https://github.com/tsirysndr/setup-envhu
```yaml
- uses: tsirysndr/setup-envhub@v1
with:
version: 'v0.2.9'
version: 'v0.2.10'
- run: envhub --help
```
4 changes: 2 additions & 2 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ license = "MIT"
name = "envhub"
readme = "../../README.md"
repository = "https://github.com/tsirysndr/envhub"
version = "0.2.9"
version = "0.2.10"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.71"
clap = "3.2.20"
envhub-ext = {path = "../ext", version = "0.1.0"}
envhub-hm = {path = "../hm", version = "0.1.4"}
envhub-hm = {path = "../hm", version = "0.1.5"}
envhub-pkgs = {path = "../pkgs", version = "0.1.2"}
envhub-providers = {path = "../providers", version = "0.2.0"}
envhub-stow = {path = "../stow", version = "0.1.0"}
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ pub fn save_secrets(config: &Configuration) -> Result<(), Error> {
let mut secrets_map = IndexMap::new();

for secret in secrets {
if let Ok(value) = env::var(&secret) {
secrets_map.insert(secret, value);
continue;
}
let value = Password::new(&secret)
.with_display_toggle_enabled()
.with_display_mode(PasswordDisplayMode::Masked)
Expand Down
2 changes: 1 addition & 1 deletion crates/hm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ keywords = ["nix", "shell", "environment", "dotfiles"]
license = "MIT"
name = "envhub-hm"
repository = "https://github.com/tsirysndr/envhub"
version = "0.1.4"
version = "0.1.5"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
24 changes: 24 additions & 0 deletions crates/hm/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ pub fn switch_env(dir: Option<&str>, config: &Configuration) -> Result<(), Error
if sm == "home-manager" {
updated_home_nix =
add_files(&updated_home_nix, config.files.clone().unwrap_or_default())?;
if config.envs.is_some() {
updated_home_nix =
add_envs(&updated_home_nix, config.envs.clone().unwrap_or_default())?;
}
}
}
None => {
updated_home_nix =
add_files(&updated_home_nix, config.files.clone().unwrap_or_default())?;
if config.envs.is_some() {
updated_home_nix =
add_envs(&updated_home_nix, config.envs.clone().unwrap_or_default())?;
}
}
}

Expand Down Expand Up @@ -114,3 +122,19 @@ pub fn add_files(
let result = nix_editor::write::write(content, "home.file", &entry)?;
Ok(result)
}

pub fn add_envs(content: &str, envs: IndexMap<String, String>) -> Result<String, Error> {
let mut entry = String::new();

if envs.is_empty() {
return Ok(content.into());
}

entry.push_str("{\n");
for (env, value) in envs {
entry.push_str(&format!(" \"{}\" = \"{}\";\n", env, value));
}
entry.push_str("}\n");
let result = nix_editor::write::write(content, "home.sessionVariables", &entry)?;
Ok(result)
}
4 changes: 4 additions & 0 deletions demo/envhub.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ packages = [
"hello"
]

envs {
EDITOR = "vim"
}

file ".screenrc" {
source = "dotfiles/.screenrc"
}
Expand Down

0 comments on commit 65e3e1c

Please sign in to comment.