Skip to content

Commit

Permalink
fix: unwrap -> ok_or and ?
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Sep 3, 2024
1 parent 9f578d3 commit f796c69
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 23 deletions.
88 changes: 66 additions & 22 deletions src/commands/config/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,96 @@ pub struct Args {
pub async fn command(args: Args) -> Result<()> {
println!("Migrating config file... Please do not interrupt this process.");

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

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

// mkdir at ~/.config/envx
let mut new_config_path = home_dir().unwrap();
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().unwrap().exists() {
std::fs::create_dir_all(new_config_path.clone()).unwrap();
if !new_config_path
.parent()
.ok_or(anyhow!("Failed to get parent directory"))?
.exists()
{
std::fs::create_dir_all(new_config_path.clone())?;
}

if args.verbose {
println!(
"Copying {} to {}",
old_config_path.to_str().unwrap(),
new_config_path.to_str().unwrap()
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.clone(), new_config_path)?;

let mut old_key_dir = home_dir().unwrap();
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().unwrap();
let mut new_key_dir =
home_dir().ok_or(anyhow!("Failed to get home directory"))?;
new_key_dir.push(".config/envx/keys");

if !old_key_dir.exists() {
println!(
"Key directory not found at {}. Skipping migration.",
old_key_dir.to_str().unwrap()
old_key_dir.to_str().ok_or(anyhow!("Failed to get path"))?
);
return Ok(());
}

// mkdir at ~/.config/envx/keys
if !new_key_dir.parent().unwrap().exists() {
if !new_key_dir
.parent()
.ok_or(anyhow!("Failed to get parent directory"))?
.exists()
{
if args.verbose {
println!(
"Creating key directory at {}",
new_key_dir.to_str().unwrap()
new_key_dir.to_str().ok_or(anyhow!("Failed to get path"))?
);
}
std::fs::create_dir_all(new_key_dir.clone()).unwrap();
std::fs::create_dir_all(new_key_dir.clone())?;
}

// copy all files from old key dir to new key dir
for entry in std::fs::read_dir(old_key_dir)? {
let entry = entry?;
if entry.file_type()?.is_dir() {
let old_path = entry.path();
let new_dir_path = new_key_dir.join(old_path.file_name().unwrap());
let new_dir_path = new_key_dir.join(
old_path
.file_name()
.ok_or(anyhow!("Failed to get file name"))?,
);

if !new_dir_path.exists() {
if args.verbose {
println!(
"Creating key directory at {}",
new_dir_path.to_str().unwrap()
new_dir_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
);
}
std::fs::create_dir_all(new_dir_path.clone()).unwrap();
std::fs::create_dir_all(new_dir_path.clone())?;
}

for file in std::fs::read_dir(entry.path())? {
Expand All @@ -87,20 +111,40 @@ pub async fn command(args: Args) -> Result<()> {
continue;
}
let old_path = file.path();
let new_path = new_dir_path.join(old_path.file_name().unwrap());
let new_path = new_dir_path.join(
old_path
.file_name()
.ok_or(anyhow!("Failed to get file name"))?,
);
if args.verbose {
println!(
"Copying {} to {}",
old_path.to_str().unwrap(),
new_path.to_str().unwrap()
old_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?,
new_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
);
}

if !old_path.to_str().unwrap().contains("private.key")
&& !old_path.to_str().unwrap().contains("public.key")
if !old_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
.contains("private.key")
&& !old_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
.contains("public.key")
{
println!("{}", "Extraneous file found, copying anyway. Please make sure you have the correct files in your key directory.".red());
println!("File: {}", old_path.to_str().unwrap().red());
println!(
"File: {}",
old_path
.to_str()
.ok_or(anyhow!("Failed to get path"))?
.red()
);
}

let result = std::fs::copy(old_path, new_path);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{sdk::SDK, utils::config::get_config};
/// Unset (delete) an environment variable
#[derive(Parser)]
pub struct Args {
/// Variable to unset
/// Variable to unset
#[clap(short, long)]
variable: Option<String>,

Expand Down

0 comments on commit f796c69

Please sign in to comment.