Skip to content

Commit

Permalink
Modify initialization of config directory and shellscript (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
humblepenguinn committed Apr 8, 2024
1 parent 3ac5f1e commit 7c58b61
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 128 deletions.
134 changes: 6 additions & 128 deletions src/bin/envio/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ mod commands;
mod utils;
mod version;

#[cfg(target_family = "unix")]
use std::io::Write;
#[cfg(target_family = "unix")]
use std::path::{Path, PathBuf};

use clap::Parser;
use colored::Colorize;
#[cfg(target_family = "unix")]
use inquire::Text;
use semver::Version;

use clap_app::ClapApp;
use utils::initalize_config;
use version::get_latest_version;

fn main() {
Expand All @@ -27,7 +21,7 @@ fn main() {
val
} else {
println!("{}: Failed to parse current version", "Error".red());
return;
"0.0.0".parse().unwrap()
};

if latest_version > current_version {
Expand All @@ -39,130 +33,14 @@ fn main() {
);
}

#[cfg(target_family = "unix")]
{
let configdir = match utils::get_configdir() {
Ok(val) => val,
Err(e) => {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
};

let homedir = match utils::get_homedir() {
Ok(val) => val,
Err(e) => {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
};

if !Path::new(&configdir).exists() {
println!("{}", "Creating config directory".bold());
if let Err(e) = std::fs::create_dir(&configdir) {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}

if let Err(e) = std::fs::create_dir(configdir.join("profiles")) {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
}

if !Path::new(&configdir.join("setenv.sh")).exists() {
println!("{}", "Creating shellscript".bold());
if let Err(e) = std::fs::write(configdir.join("setenv.sh"), "") {
println!("{}: {}", "Error".red(), e);
if let Err(e) = std::fs::remove_dir_all(&configdir) {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}

std::process::exit(1);
}

let shellconfig = match utils::get_shell_config() {
Ok(val) => val,
Err(e) => {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
};

let mut file_path = PathBuf::from(
&(homedir.to_str().unwrap().to_owned() + &format!("/{}", shellconfig)),
);
if !file_path.exists() {
let input = Text::new(
"Shell config file not found, please enter the path to your shell config file:",
)
.prompt();

file_path = if let Ok(val) = input {
PathBuf::from(val)
} else {
println!("{}: {}", "Error".red(), input.err().unwrap());
if let Err(e) = std::fs::remove_dir_all(&configdir) {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
std::process::exit(1);
};

if !file_path.exists() {
println!(
"{}: Specified shell config file does not exist either!?",
"Error".red()
);

if let Err(e) = std::fs::remove_dir_all(&configdir) {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}

std::process::exit(1);
}
}

let mut file = std::fs::OpenOptions::new()
.append(true)
.open(file_path)
.unwrap();

let shellscript_path = &configdir.join("setenv.sh");

let buffer = if shellconfig.contains("fish") {
println!(
"To use the shellscript properly you need to install the {}(https://github.com/edc/bass) plugin for fish",
"bass".bold()
);
format!(
"
# envio DO NOT MODIFY
bass source {}
",
shellscript_path.to_str().unwrap()
)
} else {
format!(
"
#envio DO NOT MODIFY
source {}
",
shellscript_path.to_str().unwrap()
)
};
let args = ClapApp::parse();

if let Err(e) = writeln!(file, "{}", buffer) {
println!("{}: {}", "Error".red(), e);
}
}
if let Err(e) = initalize_config() {
println!("{}: {}", "Error".red(), e);
}

let args = ClapApp::parse();

if let Err(e) = args.command.run() {
println!("{}: {}", "Error".red(), e);
std::process::exit(1);
}
}
75 changes: 75 additions & 0 deletions src/bin/envio/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,81 @@ use envio::{Env, EnvVec};
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::Client;

#[cfg(target_family = "unix")]
pub fn initalize_config() -> Result<()> {
use std::path::Path;
use colored::Colorize;
use inquire::Text;

let configdir = get_configdir()?;
let homedir = get_homedir()?;

if !Path::new(&configdir).exists() {
println!("{}", "Creating config directory".bold());

std::fs::create_dir(&configdir)?;
std::fs::create_dir(configdir.join("profiles"))?;
}

if !Path::new(&configdir.join("setenv.sh")).exists() {
println!("{}", "Creating shellscript".bold());
std::fs::write(configdir.join("setenv.sh"), "")?;

let shellconfig = get_shell_config()?;

let mut file_path =
PathBuf::from(&(homedir.to_str().unwrap().to_owned() + &format!("/{}", shellconfig)));
if !file_path.exists() {
let input = Text::new(
"Shell config file not found, please enter the path to your shell config file:",
)
.prompt();

file_path = if let Ok(val) = input {
PathBuf::from(val)
} else {
return Err(Error::Msg("Failed to get shell config file path".to_string()));
};

if !file_path.exists() {
return Err(Error::Msg("Specified shell config file does not exist".to_string()));
}
}

let mut file = std::fs::OpenOptions::new()
.append(true)
.open(file_path)
.unwrap();

let shellscript_path = &configdir.join("setenv.sh");

let buffer = if shellconfig.contains("fish") {
println!(
"To use the shellscript properly you need to install the {}(https://github.com/edc/bass) plugin for fish",
"bass".bold()
);
format!(
"
# envio DO NOT MODIFY
bass source {}
",
shellscript_path.to_str().unwrap()
)
} else {
format!(
"
#envio DO NOT MODIFY
source {}
",
shellscript_path.to_str().unwrap()
)
};

writeln!(file, "{}", buffer)?
}

Ok(())
}
/// Get the home directory
///
/// # Returns
Expand Down

0 comments on commit 7c58b61

Please sign in to comment.