diff --git a/src/main.rs b/src/main.rs index b9b8b4c..e4d75aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,8 @@ fn show_usage() { println!("add_custom_name"); } -fn run_as_cli() { +fn run_as_cli() -> i32 { + let mut result: i32; let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); let mut option: String = String::new(); @@ -24,6 +25,8 @@ fn run_as_cli() { println!("Please input any of these options or type \'exit\' to leave"); loop { + result = 0; + io::stdin() .read_line(&mut option) .expect("Failed to read input"); @@ -32,42 +35,55 @@ fn run_as_cli() { match option { "usage" => show_usage(), - "generate" => generate_file(&mut client, None), - "convert" => convert_folders(&mut client, None), - "import" => import_file(&mut client, None), - "add_url" => add_url(&mut client, None), - "add_custom_name" => add_artist_with_custom_name(&mut client, None), + "generate" => result = generate_file(&mut client, None), + "convert" => result = convert_folders(&mut client, None), + "import" => result = import_file(&mut client, None), + "add_url" => result = add_url(&mut client, None), + "add_custom_name" => result = add_artist_with_custom_name(&mut client, None), "exit" => break, _ => { eprintln!("Invalid option!"); } } + + if result != 0 { + return result; + } } + + return result; } -fn run_with_input(arguments: Vec) { +fn run_with_input(arguments: Vec) -> i32 { + let mut result: i32 = 0; + if arguments[1] == "usage" { show_usage(); - return; + return result; } let mut client = Client::connect("host=192.168.0.10 port=28945 user=hentai password=h99nqaNPhpfbuuhCDwQXLpZAnoVTjSQP7taoqmQhpzc2rPLVC4JUAKxAHfuuhuU9", NoTls).unwrap(); let value: String = arguments[2].clone(); match arguments[1].as_str() { - "generate" => generate_file(&mut client, Option::Some(value)), - "convert" => convert_folders(&mut client, Option::Some(value)), - "import" => import_file(&mut client, Option::Some(value)), - "add_url" => add_url(&mut client, Option::Some(value)), + "generate" => result = generate_file(&mut client, Option::Some(value)), + "convert" => result = convert_folders(&mut client, Option::Some(value)), + "import" => result = import_file(&mut client, Option::Some(value)), + "add_url" => result = add_url(&mut client, Option::Some(value)), "add_custom_name" => { let values: Vec = vec![arguments[2].clone(), arguments[3].clone()]; - add_artist_with_custom_name(&mut client, Option::Some(values)) + result = add_artist_with_custom_name(&mut client, Option::Some(values)); + } + _ => { + eprintln!("Invalid argument"); + result = 1; } - _ => eprintln!("Invalid argument"), } + + return result; } -fn generate_file(client: &mut postgres::Client, website: Option) { +fn generate_file(client: &mut postgres::Client, website: Option) -> i32 { let mut website_name: String = String::new(); match website { @@ -86,7 +102,7 @@ fn generate_file(client: &mut postgres::Client, website: Option) { let result: bool = output.get(0); if result == false { eprintln!("Invalid website_name"); - return; + return 1; } let filename = format!("{website_name}_generated.txt"); @@ -95,7 +111,7 @@ fn generate_file(client: &mut postgres::Client, website: Option) { Err(error) => { let message = error.to_string(); eprintln!("Problem creating the file: {message}"); - return; + return 1; } }; @@ -109,9 +125,11 @@ fn generate_file(client: &mut postgres::Client, website: Option) { writeln!(file, "{} # {}", url, folder_path).unwrap(); } + + return 0; } -fn convert_folders(client: &mut postgres::Client, path: Option) { +fn convert_folders(client: &mut postgres::Client, path: Option) -> i32 { let mut root_folder: String = String::new(); match path { @@ -129,7 +147,7 @@ fn convert_folders(client: &mut postgres::Client, path: Option) { Err(error) => { let message = error.to_string(); eprintln!("Problem creating the file: {message}"); - return; + return 1; } }; @@ -155,9 +173,11 @@ fn convert_folders(client: &mut postgres::Client, path: Option) { fs::rename(&old_path, &new_path).unwrap(); writeln!(log_file, "Converted {} => {}", old_path, new_path).unwrap(); } + + return 0; } -fn import_file(client: &mut postgres::Client, file: Option) { +fn import_file(client: &mut postgres::Client, file: Option) -> i32 { let mut filename: String = String::new(); match file { @@ -184,7 +204,7 @@ fn import_file(client: &mut postgres::Client, file: Option) { Ok(file) => file, Err(err) => { eprintln!("Problem creating the file: {}", err); - return; + return 1; } }; writeln!(fucked_file, "Them are hella fucked").unwrap(); @@ -203,9 +223,11 @@ fn import_file(client: &mut postgres::Client, file: Option) { } }; } + + return 0; } -fn add_url(client: &mut postgres::Client, uri: Option) { +fn add_url(client: &mut postgres::Client, uri: Option) -> i32 { let mut url: String = String::new(); match uri { @@ -223,16 +245,21 @@ fn add_url(client: &mut postgres::Client, uri: Option) { Ok(val) => val, Err(val) => { eprintln!("{}", val); - return; + return 1; } }; let something: String = output.get(0); println!("{}", something); + + return 0; } -fn add_artist_with_custom_name(client: &mut postgres::Client, uri_and_artist: Option>) { +fn add_artist_with_custom_name( + client: &mut postgres::Client, + uri_and_artist: Option>, +) -> i32 { let mut url: String = String::new(); let mut artist_name: String = String::new(); @@ -262,21 +289,30 @@ fn add_artist_with_custom_name(client: &mut postgres::Client, uri_and_artist: Op Ok(val) => val, Err(val) => { eprintln!("{}", val); - return; + return 1; } }; let something: String = output.get(0); println!("{}", something); + + return 0; } -fn main() { +fn main() -> Result<(), i32> { let args: Vec = args().collect(); + let result: i32; if args.len() == 1 { - run_as_cli(); + result = run_as_cli(); } else { - run_with_input(args); + result = run_with_input(args); } + + if result != 0 { + return Err(result); + } + + return Ok(()); }