Skip to content

Commit

Permalink
refactor(app.rs): factorize api response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nicotsx committed Nov 16, 2024
1 parent 3d8beaa commit 32ddd84
Showing 1 changed file with 35 additions and 109 deletions.
144 changes: 35 additions & 109 deletions src/commands/app.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
use reqwest::Method;

use crate::args::{AppCommand, AppSubcommand};
use crate::components::spinner::{self, CustomSpinner};
use crate::utils::api::api_request;
use crate::utils::env::EnvMap;

use crate::components::spinner;
use crate::utils::constants::DEFAULT_NGINX_PORT;

fn handle_api_response(
spin: CustomSpinner,
api_response: Result<reqwest::blocking::Response, std::io::Error>,
success_message: &str,
error_message: &str,
) {
match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed(success_message);
} else {
println!("Error code: {}", response.status());
spin.fail(error_message);
}
}
Err(err) => {
spin.fail(error_message);
println!("Error: {}", err);
}
}
spin.finish();
}

pub fn run(args: AppCommand, env_map: EnvMap) {
let base_url = format!(
"http://{}:{}/api/app-lifecycle",
Expand All @@ -17,138 +40,41 @@ pub fn run(args: AppCommand, env_map: EnvMap) {
match args.subcommand {
AppSubcommand::Start(args) => {
let spin = spinner::new(&format!("Starting app {}...", args.id));
let api_response = api_request(format!("{}/{}/{}", base_url, args.id, "start"), Method::POST, "{}");
let url = format!("{}/{}/{}", base_url, args.id, "start");
let api_response = api_request(url, Method::POST, "{}");
let error_message = format!("Failed to start app {}. See logs/error.log for more details.", args.id);

match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed("App started successfully!");
} else {
println!("Error code: {}", response.status());
spin.fail(&error_message);
}
}
Err(err) => {
spin.fail("Failed to start app.");
println!("Error: {}", err);
}
}
spin.finish();
handle_api_response(spin, api_response, "App started successfully!", &error_message);
}
AppSubcommand::Stop(args) => {
let spin = spinner::new(&format!("Stopping app {}...", args.id));
let api_response = api_request(format!("{}/{}/{}", base_url, args.id, "stop"), Method::POST, "{}");
let url = format!("{}/{}/{}", base_url, args.id, "stop");
let api_response = api_request(url, Method::POST, "{}");
let error_message = format!("Failed to stop app {}. See logs/error.log for more details.", args.id);

match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed("App stopped successfully!");
} else {
println!("Error code: {}", response.status());
spin.fail(&error_message);
}
}
Err(err) => {
spin.fail("Failed to stop app.");
println!("Error: {}", err);
}
}
spin.finish();
handle_api_response(spin, api_response, "App stopped successfully!", &error_message);
}
AppSubcommand::Uninstall(args) => {
let spin = spinner::new(&format!("Uninstalling app {}...", args.id));
let url = format!("{}/{}/{}", base_url, args.id, "uninstall");
let api_response = api_request(url, Method::DELETE, "{\"removeBackups\": false}");
let error_message = format!("Failed to uninstall app {}. See logs/error.log for more details.", args.id);

match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed("App uninstalled successfully!");
} else {
println!("Error code: {}, {}", response.status(), response.text().unwrap());
spin.fail(&error_message);
}
}
Err(err) => {
spin.fail("Failed to uninstall app.");
println!("Error: {}", err);
}
}
spin.finish();
handle_api_response(spin, api_response, "App uninstalled successfully!", &error_message);
}
AppSubcommand::Reset(args) => {
let spin = spinner::new(&format!("Resetting app {}...", args.id));
let api_response = api_request(format!("{}/{}/{}", base_url, args.id, "reset"), Method::POST, "{}");
let url = format!("{}/{}/{}", base_url, args.id, "reset");
let api_response = api_request(url, Method::POST, "{}");
let error_message = format!("Failed to reset app {}. See logs/error.log for more details.", args.id);

match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed("App reset successfully!");
spin.finish();
} else {
println!("Error code: {}", response.status());
spin.fail(&error_message);
spin.finish();
}
}
Err(err) => {
spin.fail("Failed to reset app.");
spin.finish();
println!("Error: {}", err);
}
}
handle_api_response(spin, api_response, "App reset successfully!", &error_message);
}
AppSubcommand::Update(args) => {
let spin = spinner::new(&format!("Updating app {}...", args.id));
let url = format!("{}/{}/{}", base_url, args.id, "update");
let api_response = api_request(url, Method::PATCH, "{\"performBackup\": true}");
let error_message = format!("Failed to update app {}. See logs/error.log for more details.", args.id);

match api_response {
Ok(response) => {
if response.status().is_success() {
spin.succeed("App updated successfully!");
spin.finish();
} else {
println!("Error code: {}", response.status());
spin.fail(&error_message);
spin.finish();
}
}
Err(err) => {
spin.fail("Failed to update app.");
spin.finish();
println!("Error: {}", err);
}
}
handle_api_response(spin, api_response, "App updated successfully!", &error_message);
}
AppSubcommand::StartAll(_) => {
panic!("Not implemented yet");

// let spin = spinner::new("Starting all apps...");
// let api_response = api_request(format!("{}/{}", base_url, "start-all"));
// let error_message = "Failed to start apps. See logs/error.log for more details.";
//
// match api_response {
// Ok(response) => {
// if response.status().is_success() {
// spin.succeed("All apps started successfully!!");
// spin.finish();
// } else {
// spin.fail(error_message);
// spin.finish();
// }
// }
// Err(err) => {
// spin.fail("Failed to start apps.");
// spin.finish();
// println!("Error: {}", err);
// }
// }
}
}
}

0 comments on commit 32ddd84

Please sign in to comment.