Better error handling

This commit is contained in:
Skye Bleed 2019-09-04 18:04:18 -05:00
parent 39d7554686
commit adfd5c5300
4 changed files with 103 additions and 13 deletions

64
log.txt Normal file
View File

@ -0,0 +1,64 @@
Compiling pack v0.1.0 (/home/skye/rust/pack)
error[E0308]: mismatched types
--> src/main.rs:26:21
|
26 | err_and_exit("Please enter a repo to download from!", 0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| expected struct `std::string::String`, found reference
| help: try using a conversion method: `"Please enter a repo to download from!".to_string()`
|
= note: expected type `std::string::String`
found type `&'static str`
error[E0308]: match arms have incompatible types
--> src/main.rs:39:14
|
37 | let repo = match vec[1] {
| ________________-
38 | | "arch_core" => "http://mirrors.advancedhosters.com/archlinux/core/os/x86_64/",
| | -------------------------------------------------------------- this is found to be of type `&'static str`
39 | | _ => err_and_exit(format!("Repo {} is not supported...", vec[1]), 0),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected &str, found ()
40 | | };
| |_____- `match` arms have incompatible types
|
= note: expected type `&str`
found type `()`
error[E0308]: match arms have incompatible types
--> src/main.rs:46:19
|
44 | let mut resp = match client.get(url).send() {
| ____________________-
45 | | Ok(a) => a,
| | - this is found to be of type `reqwest::response::Response`
46 | | Err(e) => err_and_exit(format!("ERROR!: Couldn't get package from url. Are you connected to the internet?\n\n{}", e), 0),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `reqwest::response::Response`, found ()
47 | | };
| |_____- `match` arms have incompatible types
|
= note: expected type `reqwest::response::Response`
found type `()`
error[E0308]: match arms have incompatible types
--> src/main.rs:57:23
|
55 | let mut file = match File::create(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname)) {
| ________________________-
56 | | Ok(res) => res,
| | --- this is found to be of type `std::fs::File`
57 | | Err(e) => err_and_exit(format!("Failed to create file...did you setup you environment? Do you have permission to create files?\n\n{}", e), 0),
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::fs::File`, found ()
58 | | };
| |_________- `match` arms have incompatible types
|
= note: expected type `std::fs::File`
found type `()`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.
error: Could not compile `pack`.
To learn more, run the command again with --verbose.

BIN
pack Executable file

Binary file not shown.

View File

@ -5,10 +5,25 @@ os.execute("cargo build --release")
os.execute("cp target/release/pack .")
print("Done!\n\n")
print("Setup environment? (requires root) [Y/n]:")
stdo = io.open("/dev/stdout", "w")
stdo:write("Setup environment? (requires root) [Y/n]: ")
io.close(stdo)
input = io.read()
if input == "y" then
if input ~= "n" then
os.execute("scripts/setup.sh")
else
print("Goodbye!\n")
end
print()
print("Add /usr/pack/bin/ to path? [Y/n]:")
input = io.read()
if input ~= "n" then
print("Enter the name of the file to append to (full path):")
file = io.read()
f = io.open(file, "a")
f:write("export PATH=$PATH:/usr/pack/bin\n")
else
print("Goodbye!\n")
end

View File

@ -6,6 +6,11 @@ use std::process::Command;
use reqwest::Client;
use reqwest::header::ETAG;
fn err_and_exit(msg: String, code: i32) {
println!("ERROR!: {}", msg);
std::process::exit(code);
}
fn main() -> Result<(), std::io::Error> {
let client = Client::new();
@ -17,12 +22,14 @@ fn main() -> Result<(), std::io::Error> {
let split = args[1].as_str().split("::");
let vec: Vec<&str> = split.collect();
if vec.len() < 2 {
err_and_exit("Please enter a repo to download from!".to_string(), 0);
}
println!("[PACK]: Downloading {}", vec[0]);
if !Path::new(format!("/usr/pack/repo/{}", vec[1]).as_str()).exists() {
println!("[PACK]: Path /usr/pack/repo/{} does not exist. Please setup your environment.", vec[1]);
std::process::exit(1);
err_and_exit(format!("Path /usr/pack/repo/{} does not exist. Please setup your environment.", vec[1]), 1);
}
let full_path: Vec<&str> = vec[0].split("/").collect();
@ -30,15 +37,20 @@ fn main() -> Result<(), std::io::Error> {
let repo = match vec[1] {
"arch_core" => "http://mirrors.advancedhosters.com/archlinux/core/os/x86_64/",
_ => {
println!("Repo {} is not supported...", vec[1]);
println!("ERROR!: Repo {} is not supported...", vec[1]);
std::process::exit(0);
},
};
print!("[PACK]: Making request...");
let url = reqwest::Url::parse(&(repo.to_string() + pkgname)).expect("Failed to parse URL");
let mut resp = client.get(url)
.send().expect("Failed to make request");
let mut resp = match client.get(url).send() {
Ok(a) => a,
Err(e) => {
println!("ERROR!: Couldn't get package from url. Are you connected to the internet?\n\n{}", e);
std::process::exit(0);
},
};
if resp.status().is_success() {
println!("OK");
@ -49,10 +61,9 @@ fn main() -> Result<(), std::io::Error> {
let mut file = match File::create(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname)) {
Ok(res) => res,
Err(e) => {
println!("Failed to create file...did you setup you environment? Do you have permission to create files?");
println!("\nError: {}", e);
std::process::exit(1);
}
println!("Failed to create file...did you setup you environment? Do you have permission to create files?\n\n{}", e);
std::process::exit(0);
},
};
resp.copy_to(&mut file).expect("failed to write file");
@ -69,7 +80,7 @@ fn main() -> Result<(), std::io::Error> {
println!("OK");
print!("[PACK]: Executing tar...");
let cmd = Command::new("tar")
Command::new("tar")
.arg(format!("-C/usr/pack/repo/{}/{}", vec[1], dirname))
.arg("-xf")
.arg(format!("/usr/pack/repo/{}/tarballs/{}", vec[1], pkgname))
@ -86,7 +97,7 @@ fn main() -> Result<(), std::io::Error> {
let a: Vec<&str> = strpth.split("/").collect();
let name = a[a.len() - 1];
std::os::unix::fs::symlink(pth, format!("/usr/pack/bin/{}", name));
std::os::unix::fs::symlink(pth, format!("/usr/pack/bin/{}", name)).expect("Failed to symlink...");
}
Ok(())