diff --git a/Cargo.lock b/Cargo.lock index 83d539a..4443ce0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -383,7 +383,7 @@ checksum = "545b22097d44f8a9581187cdf93de7a71e4722bf51200cfaba810865b49a495d" [[package]] name = "envhub" -version = "0.2.10" +version = "0.2.11" dependencies = [ "anyhow", "clap 3.2.25", @@ -411,7 +411,7 @@ dependencies = [ [[package]] name = "envhub-hm" -version = "0.1.5" +version = "0.2.0" dependencies = [ "anyhow", "envhub-types", diff --git a/README.md b/README.md index 79c6559..e93c01e 100644 --- a/README.md +++ b/README.md @@ -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.10' + version: 'v0.2.11' - run: envhub --help ``` diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index fdf2749..335f40a 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT" name = "envhub" readme = "../../README.md" repository = "https://github.com/tsirysndr/envhub" -version = "0.2.10" +version = "0.2.11" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -16,7 +16,7 @@ version = "0.2.10" anyhow = "1.0.71" clap = "3.2.20" envhub-ext = {path = "../ext", version = "0.1.0"} -envhub-hm = {path = "../hm", version = "0.1.5"} +envhub-hm = {path = "../hm", version = "0.2.0"} envhub-pkgs = {path = "../pkgs", version = "0.1.2"} envhub-providers = {path = "../providers", version = "0.2.0"} envhub-stow = {path = "../stow", version = "0.1.0"} diff --git a/crates/cli/src/cmd/package.rs b/crates/cli/src/cmd/package.rs index ea4c43f..599a324 100644 --- a/crates/cli/src/cmd/package.rs +++ b/crates/cli/src/cmd/package.rs @@ -18,7 +18,7 @@ pub fn add(package: &str, apply: bool) -> Result<(), Error> { write_envhub_file(".", &config)?; println!("Package {} added to envhub file", package.cyan()); if apply { - use_environment(".")?; + use_environment(".", false)?; } Ok(()) } @@ -59,7 +59,7 @@ pub fn remove(package: &str, apply: bool) -> Result<(), Error> { package.cyan() ); if apply { - use_environment(".")?; + use_environment(".", false)?; } Ok(()) } diff --git a/crates/cli/src/cmd/use.rs b/crates/cli/src/cmd/use.rs index 3ad9693..9f7edb9 100644 --- a/crates/cli/src/cmd/use.rs +++ b/crates/cli/src/cmd/use.rs @@ -11,7 +11,7 @@ use crate::helpers::{ copy_home_nix, get_home_manager_dir, install_packages, read_envhub_file, save_secrets, }; -pub fn use_environment(name: &str) -> Result<(), Error> { +pub fn use_environment(name: &str, backup: bool) -> Result<(), Error> { let scheme = match name.split(":").collect::>().len() > 1 { false => "local", true => name.split(":").collect::>()[0], @@ -49,7 +49,7 @@ pub fn use_environment(name: &str) -> Result<(), Error> { rtx.load(&config)?; } - switch_env(Some(&home_manager_dir), &config)?; + switch_env(Some(&home_manager_dir), &config, backup)?; let state = toml::to_string(&config)?; diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 470bb12..c0a4c32 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -112,6 +112,7 @@ fn cli() -> Command<'static> { .subcommand( Command::new("use") .about("Enable an environment, can be a remote repository or a local directory") + .arg(arg!(-b --backup "Backup if files already exist, e.g. .bashrc")) .arg(arg!([environment]).required(false).index(1)), ) .subcommand( @@ -187,9 +188,10 @@ async fn main() -> Result<(), Error> { Some(("list", _)) => cmd::file::list()?, _ => cli().print_help().unwrap(), }, - Some(("use", args)) => { - cmd::r#use::use_environment(args.value_of("environment").unwrap_or("."))? - } + Some(("use", args)) => cmd::r#use::use_environment( + args.value_of("environment").unwrap_or("."), + args.is_present("backup"), + )?, Some(("unuse", _)) => cmd::unuse::unuse_environment()?, Some(("status", _)) => cmd::status::status()?, _ => cli().print_help().unwrap(), diff --git a/crates/hm/Cargo.toml b/crates/hm/Cargo.toml index 8a742cd..40e04d1 100644 --- a/crates/hm/Cargo.toml +++ b/crates/hm/Cargo.toml @@ -7,7 +7,7 @@ keywords = ["nix", "shell", "environment", "dotfiles"] license = "MIT" name = "envhub-hm" repository = "https://github.com/tsirysndr/envhub" -version = "0.1.5" +version = "0.2.0" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/crates/hm/src/switch.rs b/crates/hm/src/switch.rs index bfd12ed..6e971da 100644 --- a/crates/hm/src/switch.rs +++ b/crates/hm/src/switch.rs @@ -9,7 +9,7 @@ use indexmap::IndexMap; use crate::{nix, HOME_MANAGER}; -pub fn switch_env(dir: Option<&str>, config: &Configuration) -> Result<(), Error> { +pub fn switch_env(dir: Option<&str>, config: &Configuration, backup: bool) -> Result<(), Error> { nix::install()?; let home_nix = fs::read_to_string(format!("{}/home.nix", dir.unwrap_or(HOME_MANAGER)))?; let mut updated_home_nix = home_nix.clone(); @@ -49,12 +49,20 @@ pub fn switch_env(dir: Option<&str>, config: &Configuration) -> Result<(), Error let home_nix_file = format!("{}/home.nix", dir.unwrap_or(HOME_MANAGER)); fs::write(&home_nix_file, &updated_home_nix)?; - let mut child = Command::new("sh") - .arg("-c") - .arg(format!( + let cmd = match backup { + true => format!( + "nix run home-manager/master -- switch --flake {} -b backup", + dir.unwrap_or(HOME_MANAGER) + ), + false => format!( "nix run home-manager/master -- switch --flake {}", dir.unwrap_or(HOME_MANAGER) - )) + ), + }; + + let mut child = Command::new("sh") + .arg("-c") + .arg(cmd) .stdin(Stdio::inherit()) .stdout(Stdio::inherit()) .stderr(Stdio::inherit())