Skip to content

Commit

Permalink
Merge pull request #37 from tsirysndr/feat/backup-option
Browse files Browse the repository at this point in the history
feat: add --backup option
  • Loading branch information
tsirysndr authored Feb 20, 2024
2 parents 65e3e1c + 9dc72b9 commit 20a4d24
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 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.10'
version: 'v0.2.11'
- 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.10"
version = "0.2.11"

# 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.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"}
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/cmd/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn remove(package: &str, apply: bool) -> Result<(), Error> {
package.cyan()
);
if apply {
use_environment(".")?;
use_environment(".", false)?;
}
Ok(())
}
4 changes: 2 additions & 2 deletions crates/cli/src/cmd/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<&str>>().len() > 1 {
false => "local",
true => name.split(":").collect::<Vec<&str>>()[0],
Expand Down Expand Up @@ -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)?;

Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
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.5"
version = "0.2.0"

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

Expand Down
18 changes: 13 additions & 5 deletions crates/hm/src/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 20a4d24

Please sign in to comment.