Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #74

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,43 @@ fn create_package_contents(name: String, lib: bool) -> std::io::Result<()> {
Ok(())
}

pub fn init(name: Option<String>, lib: bool) -> std::io::Result<()> {
let project_name;
fn init_current_dir() -> std::io::Result<String> {
let path = env::current_dir()?;
let path_name = path.components().next_back().unwrap();

if name.is_none() {
let path = env::current_dir()?;
let path_name = path.components().next_back().unwrap();
debug!(
"Name not specified, working in current directory {:?}",
path_name.as_os_str()
);

let project_name = format!("{:?}", path_name.as_os_str());
Ok(project_name)
}

debug!(
"Name not specified, working in current directory {:?}",
path_name.as_os_str()
fn init_new_dir(name: String) -> std::io::Result<String> {
if Path::new(&name).exists() {
eprintln!(
"Package {} already created",
bold_color_text!(name, color::Blue),
);
std::process::exit(0);
}

project_name = format!("{:?}", path_name.as_os_str());
} else {
if Path::new(&name.as_ref().unwrap()).exists() {
eprintln!(
"Package {} already created",
bold_color_text!(name.unwrap(), color::Blue),
);
std::process::exit(0);
}
debug!("Creating directory called {}", name.clone());
fs::create_dir_all(format!("./{}", name.clone()))?;
env::set_current_dir(name.clone())?;
let project_name = name;

debug!("Creating directory called {}", name.clone().unwrap());
fs::create_dir_all(format!("./{}", name.clone().unwrap()))?;
env::set_current_dir(name.clone().unwrap())?;
project_name = name.unwrap();
Ok(project_name)
}

pub fn init(name: Option<String>, lib: bool) -> std::io::Result<()> {
let project_name: String;

if name.is_none() {
project_name = init_current_dir()?;
} else {
project_name = init_new_dir(name.unwrap().to_string())?;
}

match create_package_contents(project_name.clone(), lib) {
Expand Down
34 changes: 24 additions & 10 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,44 @@ pub struct Package {
pub package: InnerPackage,
}

fn pull_remote_package(uri: String) -> reqwest::Response {
debug!("Pulling manifest from {}", uri);
let req: reqwest::Response = match reqwest::get(&uri) {
Ok(a) => a,
Err(e) => panic!("{}", e),
};

return req;
}

fn get_request_text(mut req: reqwest::Response) -> String {
let text = match req.text() {
Ok(a) => a,
Err(e) => panic!("{}", e),
};

debug!("{}", text);
return text;
}

pub fn fetch_remote_package(package: &str) -> Result<Package, error::Error> {
let uri = format!(
"https://raw.githubusercontent.com/{}/main/package.yml",
package
);

debug!("Pulling manifest from {}", uri);
let mut req = reqwest::get(&uri).unwrap();

let req = pull_remote_package(uri);
if !req.status().is_success() {
eprintln!("Requested turned back a ");
return Err(error::Error::RequestFailed);
eprintln!("Requested turned back an error.");
}

let text = req.text().unwrap();

debug!("{}", text);
let text = get_request_text(req);

let package_yaml: Package = match serde_yaml::from_str(&text) {
Ok(package) => package,
Err(_) => return Err(error::Error::ParseError),
};

debug!("{:?}", package_yaml);

Ok(package_yaml)
Expand All @@ -50,9 +66,7 @@ pub fn fetch_remote_package(package: &str) -> Result<Package, error::Error> {
pub fn fetch_local_package(package: &str) -> Result<Package, serde_yaml::Error> {
let local = get_local_dir();
let package_name = Path::new(&local).join(package).join("package.yml");

let contents = fs::read_to_string(package_name).expect("Error reading file");

let loaded: Package = serde_yaml::from_str(&contents)?;
Ok(loaded)
}
Expand Down
28 changes: 12 additions & 16 deletions src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,23 @@ use super::package::get_local_dir;
use super::spinner::spinner;
use std::fs;
use std::path::Path;
use nix::unistd::Uid;

fn check_if_package_exists(package_path: &str, package: &str) {
if !Path::new(&package_path).exists() {
eprintln!("Package {} does not exist", package);
std::process::exit(0);
}
}

pub fn uninstall(package: &str) -> std::io::Result<()> {
let spin = spinner("Uninstalling...".to_string());

let local = get_local_dir();
let package_name = format!("{}/{}", local, package);
let package_path = format!("{}/{}", get_local_dir(), package);
check_if_package_exists(&package_path, package);

if !Path::new(&package_name).exists() {
eprintln!("Package {} does not exist", package,);
std::process::exit(0);
}

// Check for root before running remove_dir_all
if !Uid::effective().is_root() {
fs::remove_dir_all(package_name)?;
debug!("Successfully removed package");
spin.finish_with_message("Done ✔");
} else {
spin.finish_with_message("Please do not run uninstall with root.")
}
fs::remove_dir_all(package_path)?;
debug!("Successfully removed package");
spin.finish_with_message("Done ✔");

Ok(())
}
21 changes: 14 additions & 7 deletions src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ use crate::package::get_local_dir;
use std::env;
use std::process::Command;

pub fn upgrade(name: &str) -> std::io::Result<()> {
let spin = spinner("Upgrading...".to_string());
fn git_pull() {
Command::new("git")
.arg("pull")
.output()
.expect("Failed to pull");
}

fn go_to_package_dir(name: &str) -> std::io::Result<()> {
let local = get_local_dir();
let package_path = format!("{}/{}", local, name);
env::set_current_dir(package_path)?;
Ok(())
}

Command::new("git")
.arg("pull")
.output()
.expect("Failed to pull");
pub fn upgrade(name: &str) -> std::io::Result<()> {
let spin = spinner("Upgrading...".to_string());

spin.finish_with_message("Done ✔");
go_to_package_dir(name)?;
git_pull();

spin.finish_with_message("Done ✔");
Ok(())
}