Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.

Commit cdbac69

Browse files
feat(#4): add remove command
2 parents ca20489 + 000ad78 commit cdbac69

File tree

6 files changed

+93
-41
lines changed

6 files changed

+93
-41
lines changed

src/commands/commit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn get_zip_file_path() -> PathBuf {
1616
}
1717
pub fn commit() {
1818
let token = super::expect_token();
19-
let app_id = match super::ask_for_app(token.clone()) {
19+
let app_id = match super::ask_for_app(token.clone(), "commit") {
2020
Ok(app_id) => app_id,
2121
Err(error) => {
2222
super::err(&format!("Couldn't fetch apps: {}", error));
@@ -131,7 +131,7 @@ fn upload_zip(token: String, app_id: u128) -> Result<(), String> {
131131
Ok(())
132132
} else {
133133
Err(format!(
134-
"Discloud API returned {} http code: {}",
134+
"Commit failed: API returned {} http code: {}",
135135
res.status().as_u16(),
136136
res.text().unwrap()
137137
))

src/commands/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod commit;
33
pub mod init;
44
pub mod login;
55
pub mod upload;
6+
pub mod remove;
67
use colored::Colorize;
78
use dialoguer::{theme::ColorfulTheme, Select};
89
use spinners::*;
@@ -70,7 +71,7 @@ mod tests {
7071
assert_eq!(super::format_warn("Some warnings"), out)
7172
}
7273
}
73-
pub fn ask_for_app(token: String) -> Result<u128, FetchError> {
74+
pub fn ask_for_app(token: String, action: &str) -> Result<u128, FetchError> {
7475
let user = crate::entities::user::fetch_user(token.clone())?;
7576
match user.apps.len() {
7677
0 => {
@@ -79,14 +80,14 @@ pub fn ask_for_app(token: String) -> Result<u128, FetchError> {
7980
}
8081
1 => Ok(user.apps[0].parse().unwrap()),
8182
_ => {
82-
let apps = crate::entities::app::fetch_apps(token)?;
83+
let apps = crate::entities::app::App::fetch_all(token)?;
8384
let options = apps
8485
.iter()
8586
.map(|app| format!("{}: ({}) {}", app.name, app.lang, app.id))
8687
.collect::<Vec<_>>();
8788
let chosen_opt = Select::with_theme(&ColorfulTheme::default())
8889
.items(&options)
89-
.with_prompt("Which app you want to commit?")
90+
.with_prompt(format!("Which app you want to {}?", action))
9091
.interact()
9192
.unwrap();
9293
Ok(apps[chosen_opt].id.parse().unwrap())

src/commands/remove.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use spinners::*;
2+
pub fn remove() {
3+
let token = super::expect_token();
4+
match super::ask_for_app(token.clone(), "delete") {
5+
Ok(app_id) => {
6+
let mut spinner = Spinner::new(Spinners::Flip, "Deleting your app".into());
7+
match crate::entities::app::App::delete(token.clone(), app_id) {
8+
Ok(()) => {
9+
spinner.stop_with_message(super::format_log("Your app was successfully nuked!"));
10+
}
11+
Err(err) => {
12+
super::err(&format!("Couldn't delete your app: {}", err));
13+
std::process::exit(1);
14+
}
15+
}
16+
17+
}
18+
Err(err) => {
19+
super::err(&format!("Couldn't fetch apps from api: {}", err));
20+
std::process::exit(1);
21+
}
22+
}
23+
24+
}

src/commands/upload.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,13 @@ fn upload_zip(token: String) -> Result<(), String> {
128128
if res.status().is_success() {
129129
let res: UploadResponse = res.json().unwrap();
130130
if res.status == "error" {
131-
Err(format!("Commit failed: {}", res.message.unwrap()))
131+
Err(format!("Upload failed: {}", res.message.unwrap()))
132132
} else {
133133
Ok(())
134134
}
135135
} else {
136136
Err(format!(
137-
"Commit failed: API returned {}",
137+
"Upload failed: API returned {}",
138138
res.status().as_u16()
139139
))
140140
}

src/entities/app.rs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,62 @@ pub struct App {
1313
pub main_file: String,
1414
pub lang: String,
1515
}
16-
17-
pub fn fetch_app(token: String, id: u128) -> Result<App, FetchError> {
18-
#[derive(Deserialize)]
19-
struct AppResponse {
20-
pub apps: App,
21-
}
22-
let client = reqwest::blocking::Client::new();
23-
let req = client
24-
.get(crate::api_url!(format!("/app/{}", id)))
25-
.header("api-token", token);
26-
match req.send() {
27-
Ok(res) => {
28-
if res.status().is_success() {
29-
Ok(res.json::<AppResponse>().unwrap().apps)
30-
} else {
31-
Err(FetchError::APIReturnedError(res.status().as_u16()))
16+
impl App {
17+
pub fn fetch_all(token: String) -> Result<Vec<App>, FetchError> {
18+
#[derive(Deserialize)]
19+
struct AppsResponse {
20+
pub apps: Vec<App>,
21+
}
22+
let client = reqwest::blocking::Client::new();
23+
let req = client
24+
.get(crate::api_url!("/app/all"))
25+
.header("api-token", token);
26+
match req.send() {
27+
Ok(res) => {
28+
if res.status().is_success() {
29+
Ok(res.json::<AppsResponse>().unwrap().apps)
30+
} else {
31+
Err(FetchError::APIReturnedError(res.status().as_u16()))
32+
}
3233
}
34+
Err(err) => Err(FetchError::FailedToConnect(err)),
3335
}
34-
Err(err) => Err(FetchError::FailedToConnect(err)),
3536
}
36-
}
37-
38-
pub fn fetch_apps(token: String) -> Result<Vec<App>, FetchError> {
39-
#[derive(Deserialize)]
40-
struct AppsResponse {
41-
pub apps: Vec<App>,
37+
38+
pub fn fetch(token: String, id: u128) -> Result<App, FetchError> {
39+
#[derive(Deserialize)]
40+
struct AppResponse {
41+
pub apps: App,
42+
}
43+
let client = reqwest::blocking::Client::new();
44+
let req = client
45+
.get(crate::api_url!(format!("/app/{}", id)))
46+
.header("api-token", token);
47+
match req.send() {
48+
Ok(res) => {
49+
if res.status().is_success() {
50+
Ok(res.json::<AppResponse>().unwrap().apps)
51+
} else {
52+
Err(FetchError::APIReturnedError(res.status().as_u16()))
53+
}
54+
}
55+
Err(err) => Err(FetchError::FailedToConnect(err)),
56+
}
4257
}
43-
let client = reqwest::blocking::Client::new();
44-
let req = client
45-
.get(crate::api_url!("/app/all"))
46-
.header("api-token", token);
47-
match req.send() {
48-
Ok(res) => {
49-
if res.status().is_success() {
50-
Ok(res.json::<AppsResponse>().unwrap().apps)
51-
} else {
52-
Err(FetchError::APIReturnedError(res.status().as_u16()))
58+
pub fn delete(token: String, id: u128) -> Result<(), FetchError> {
59+
let client = reqwest::blocking::Client::new();
60+
let req = client
61+
.delete(crate::api_url!(format!("/app/{}/delete", id)))
62+
.header("api-token", token);
63+
match req.send() {
64+
Ok(res) => {
65+
if res.status().is_success() {
66+
Ok(())
67+
} else {
68+
Err(FetchError::APIReturnedError(res.status().as_u16()))
69+
}
5370
}
71+
Err(err) => Err(FetchError::FailedToConnect(err)),
5472
}
55-
Err(err) => Err(FetchError::FailedToConnect(err)),
5673
}
5774
}

src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ fn main() -> std::io::Result<()> {
6262
Command::new("commit")
6363
.about("Commits to an app on discloud. If you have more than one app, it will ask which app you want to commit to.")
6464
.alias("c")
65+
)
66+
.subcommand(
67+
Command::new("remove")
68+
.about("Removes an app. If you have more than one app, it will ask which app you want to delete.")
69+
.alias("rm")
70+
.alias("rb")
6571
);
6672
let matches = cmd.get_matches();
6773
match matches.subcommand() {
@@ -76,6 +82,10 @@ fn main() -> std::io::Result<()> {
7682
commands::commit::commit();
7783
Ok(())
7884
}
85+
Some(("remove", _)) => {
86+
commands::remove::remove();
87+
Ok(())
88+
}
7989
_ => unreachable!(),
8090
}
8191
}

0 commit comments

Comments
 (0)