Skip to content

Commit

Permalink
fix: migration command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Sep 3, 2024
1 parent 9079f8b commit a0709a0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 38 deletions.
91 changes: 53 additions & 38 deletions src/commands/config/migrate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use home::home_dir;

use super::*;
Expand All @@ -8,54 +10,69 @@ pub struct Args {
verbose: bool,
}

fn envx_dir() -> Result<PathBuf> {
let mut home_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
home_dir.push(".config/envx");
Ok(home_dir)
}

fn envcli_dir() -> Result<PathBuf> {
let mut home_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
home_dir.push(".config/envcli");
Ok(home_dir)
}

fn config_dir() -> Result<PathBuf> {
let mut home_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
home_dir.push(".config");
Ok(home_dir)
}

pub async fn command(args: Args) -> Result<()> {
println!("Migrating config file... Please do not interrupt this process.");

let mut old_config_path =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
old_config_path.push(".config/envcli/config.json");
let old_config_dir = envcli_dir()?;

if !old_config_path.exists() {
if !old_config_dir.exists() {
println!(
"Config file not found at {}. Skipping migration.",
old_config_path
old_config_dir
.to_str()
.ok_or(anyhow!("Failed to get path"))?
);
return Ok(());
}

// mkdir at ~/.config/envx
let mut new_config_path =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
new_config_path.push(".config/envx/config.json");
if !new_config_path
.parent()
.ok_or(anyhow!("Failed to get parent directory"))?
.exists()
{
std::fs::create_dir_all(new_config_path.clone())?;
let new_config_dir = envx_dir()?;
if !new_config_dir.exists() {
std::fs::create_dir_all(&new_config_dir)?;
}

if args.verbose {
println!(
"Copying {} to {}",
old_config_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?,
new_config_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
);
{
let old_config_path = old_config_dir.join("config.json");
let new_config_path = new_config_dir.join("config.json");
if args.verbose {
println!(
"Copying {} to {}",
old_config_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?,
new_config_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
);
}

std::fs::copy(old_config_path, new_config_path)?;
}
std::fs::copy(old_config_path.clone(), new_config_path)?;

let mut old_key_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
old_key_dir.push(".config/envcli/keys");
let mut new_key_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
new_key_dir.push(".config/envx/keys");
let mut old_key_dir = envcli_dir()?;
old_key_dir.push("keys");
let mut new_key_dir = envx_dir()?;
new_key_dir.push("keys");

if !old_key_dir.exists() {
println!(
Expand All @@ -66,11 +83,7 @@ pub async fn command(args: Args) -> Result<()> {
}

// mkdir at ~/.config/envx/keys
if !new_key_dir
.parent()
.ok_or(anyhow!("Failed to get parent directory"))?
.exists()
{
if !new_key_dir.exists() {
if args.verbose {
println!(
"Creating key directory at {}",
Expand Down Expand Up @@ -157,7 +170,9 @@ pub async fn command(args: Args) -> Result<()> {
}
}

std::fs::remove_dir_all(old_config_path)?;
std::fs::remove_dir_all(old_config_dir)?;

println!("{}", "Migration complete. Try running `envx variables` to see if everything worked.".green());

Ok(())
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ async fn main() -> Result<()> {
&& args.iter().any(|a| a == "migrate"))
{
eprintln!("The version 1 config file has been detected. Please run `{}` to migrate your config file to the new format.", "envx config migrate".green());
eprintln!("If you have already migrated, please delete the old config file at {}", config_path.to_str().unwrap_or("INVALID PATH"));
return Ok(());
}
}
Expand Down

0 comments on commit a0709a0

Please sign in to comment.