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

Commit 2e20a8c

Browse files
committed
feat: add status command
1 parent 57ed021 commit 2e20a8c

File tree

13 files changed

+83
-15
lines changed

13 files changed

+83
-15
lines changed

src/commands.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod aboutme;
22
pub mod apps;
33
pub mod authstatus;
44
pub mod commit;
5+
pub mod status;
56
pub mod init;
67
pub mod login;
78
pub mod logs;
@@ -35,7 +36,7 @@ macro_rules! handle_result {
3536
}
3637
};
3738
}
38-
use crate::entities::FetchError;
39+
use crate::entities::{FetchError, app::App};
3940
#[tracing::instrument]
4041
pub fn expect_token() -> String {
4142
if crate::auth::validate_token() {
@@ -99,15 +100,43 @@ mod tests {
99100
assert_eq!(super::format_warn("Some warnings"), out)
100101
}
101102
}
102-
pub fn ask_for_app(token: String, action: &str, teams: bool) -> Result<u128, FetchError> {
103+
104+
pub fn ask_for_app(token: String, action: &str, teams: bool) -> Result<App, FetchError> {
105+
let mut apps = if teams {
106+
crate::entities::app::App::fetch_foreign_apps(token)
107+
} else {
108+
crate::entities::app::App::fetch_all(token)
109+
}?;
110+
match apps.len() {
111+
0 => {
112+
err("You don't have any apps!");
113+
std::process::exit(1);
114+
},
115+
1 => Ok(apps.remove(0)),
116+
_ => {
117+
let options = apps
118+
.iter()
119+
.map(|app| format!("{}: ({}) {}", app.name, app.lang, app.id))
120+
.collect::<Vec<_>>();
121+
let chosen_opt = Select::with_theme(&ColorfulTheme::default())
122+
.items(&options)
123+
.with_prompt(format!("Which app you want to {}?", action))
124+
.interact()
125+
.unwrap();
126+
Ok(apps.remove(chosen_opt))
127+
}
128+
}
129+
}
130+
131+
pub fn ask_for_app_id(token: String, action: &str, teams: bool) -> Result<u128, FetchError> {
103132
let apps = if teams {
104133
crate::entities::app::App::fetch_foreign_apps(token)
105134
} else {
106135
crate::entities::app::App::fetch_all(token)
107136
}?;
108137
match apps.len() {
109138
0 => {
110-
format_err("You don't have any apps!");
139+
err("You don't have any apps!");
111140
std::process::exit(1);
112141
},
113142
1 => Ok(apps[0].id.parse().unwrap()),

src/commands/commit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn get_zip_file_path() -> PathBuf {
1818
#[tracing::instrument]
1919
pub fn commit(teams: bool) {
2020
let token = super::expect_token();
21-
let app_id = match super::ask_for_app(token.clone(), "commit", teams) {
21+
let app_id = match super::ask_for_app_id(token.clone(), "commit", teams) {
2222
Ok(app_id) => app_id,
2323
Err(error) => {
2424
super::err(&format!("Couldn't fetch apps: {}", error));

src/commands/logs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use spinners::{Spinner, Spinners};
33
#[tracing::instrument]
44
pub fn logs(teams: bool) {
55
let token = super::expect_token();
6-
match super::ask_for_app(token.clone(), "show the logs", teams) {
6+
match super::ask_for_app_id(token.clone(), "show the logs", teams) {
77
Ok(app_id) => {
88
let mut spinner = Spinner::new(Spinners::Bounce, "Downloading the logs".into());
99
match crate::entities::app::App::get_logs(token, app_id, teams) {

src/commands/mods/add.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use spinners::Spinner;
22

33
use crate::{
4-
commands::{ask_for_app, expect_token},
4+
commands::{ask_for_app_id, expect_token},
55
entities::moderator::{Feature, Mod},
66
};
77

88
#[tracing::instrument]
99
pub fn add(id: u128) {
1010
let token = expect_token();
11-
let app_id = crate::handle_result!(ask_for_app(token.clone(), "add a moderator", false));
11+
let app_id = crate::handle_result!(ask_for_app_id(token.clone(), "add a moderator", false));
1212
let mut spinner = Spinner::new(
1313
spinners::Spinners::Bounce,
1414
format!("Adding {} as a moderator", id),

src/commands/mods/allow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::entities::moderator::Feature;
22
use spinners::*;
33
pub fn allow(id: u128, features: Vec<Feature>) {
44
let token = super::expect_token();
5-
let app_id = crate::handle_result!(super::ask_for_app(
5+
let app_id = crate::handle_result!(super::ask_for_app_id(
66
token.clone(),
77
"modify the mod's permissions",
88
false

src/commands/mods/deny.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ where
88
}
99
pub fn deny(id: u128, features: Vec<Feature>) {
1010
let token = super::expect_token();
11-
let app_id = crate::handle_result!(super::ask_for_app(
11+
let app_id = crate::handle_result!(super::ask_for_app_id(
1212
token.clone(),
1313
"modify the mod's permissions",
1414
false

src/commands/mods/remove.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ use spinners::Spinner;
22

33
use crate::handle_result;
44
use crate::{
5-
commands::{ask_for_app, expect_token},
5+
commands::{ask_for_app_id, expect_token},
66
entities::{app::App, moderator::Mod},
77
};
88
#[tracing::instrument]
99
pub fn remove(id: u128) {
1010
let token = expect_token();
11-
let app_id = handle_result!(ask_for_app(token.clone(), "remove the moderator", false));
11+
let app_id = handle_result!(ask_for_app_id(token.clone(), "remove the moderator", false));
1212
let mut spinner = Spinner::new(
1313
spinners::Spinners::Moon,
1414
format!("Sending {} to the moon", id),

src/commands/remove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use spinners::*;
22
#[tracing::instrument]
33
pub fn remove() {
44
let token = super::expect_token();
5-
match super::ask_for_app(token.clone(), "delete", false) {
5+
match super::ask_for_app_id(token.clone(), "delete", false) {
66
Ok(app_id) => {
77
let mut spinner = Spinner::new(Spinners::Flip, "Deleting your app".into());
88
match crate::entities::app::App::delete(token, app_id) {

src/commands/restart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use spinners::*;
22
#[tracing::instrument]
33
pub fn restart(teams: bool) {
44
let token = super::expect_token();
5-
match super::ask_for_app(token.clone(), "restart", teams) {
5+
match super::ask_for_app_id(token.clone(), "restart", teams) {
66
Ok(app_id) => {
77
let mut spinner = Spinner::new(Spinners::Earth, "Restarting your app".into());
88
match crate::entities::app::App::restart(token, app_id, teams) {

src/commands/start.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use spinners::*;
22
#[tracing::instrument]
33
pub fn start(teams: bool) {
44
let token = super::expect_token();
5-
match super::ask_for_app(token.clone(), "start", teams) {
5+
match super::ask_for_app_id(token.clone(), "start", teams) {
66
Ok(app_id) => {
77
let mut spinner = Spinner::new(Spinners::Earth, "Starting your app".into());
88
match crate::entities::app::App::start(token, app_id, teams) {

0 commit comments

Comments
 (0)