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

Commit 758d6fa

Browse files
committed
feat(mods): add mods add <id> command
1 parent 5860a01 commit 758d6fa

File tree

8 files changed

+211
-0
lines changed

8 files changed

+211
-0
lines changed

Cargo.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod mods;
12
pub mod aboutme;
23
pub mod logs;
34
pub mod stop;

src/commands/mods/add.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::{commands::{expect_token, ask_for_app}, entities::{moderator::{Mod, Feature}, app::App, user::fetch_user}};
2+
macro_rules! handle_result {
3+
($v:expr) => {
4+
match $v {
5+
Ok(v) => v,
6+
Err(err) => {
7+
super::err(&err.to_string());
8+
std::process::exit(1);
9+
}
10+
}
11+
}
12+
}
13+
pub fn add(id: u128) {
14+
let token = expect_token();
15+
let app_id = handle_result!(ask_for_app(token.clone(), "add a moderator"));
16+
let app = handle_result!(App::fetch(token.clone(), app_id));
17+
let user = handle_result!(fetch_user(token.clone()));
18+
let moderator = handle_result!(Mod::new(token.clone(), &user, &app, vec![Feature::SeeLogs, Feature::Status]));
19+
super::log(&format!("Permissions {:?} have been given to {}", moderator.get_features(), id));
20+
}

src/commands/mods/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod add;
2+
use super::*;

src/entities/app.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,6 @@ impl App {
147147
Err(err) => Err(FetchError::FailedToConnect(err)),
148148
}
149149
}
150+
151+
150152
}

src/entities/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod moderator;
12
use std::fmt::Display;
23

34
pub mod app;

src/entities/moderator.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use std::fmt::Debug;
2+
3+
use serde::{Deserialize, Serialize};
4+
use serde_enum_str::*;
5+
6+
use super::{user::User, app::App, FetchError};
7+
#[derive(Deserialize_enum_str, Serialize_enum_str, Clone)]
8+
pub enum Feature {
9+
#[serde(rename = "start_app")]
10+
Start,
11+
#[serde(rename = "stop_app")]
12+
Stop,
13+
#[serde(rename = "restart_app")]
14+
Restart,
15+
#[serde(rename = "logs_app")]
16+
SeeLogs,
17+
#[serde(rename = "commit_app")]
18+
Commit,
19+
#[serde(rename = "status_app")]
20+
Status,
21+
#[serde(rename = "edit_ram")]
22+
SetRam,
23+
#[serde(rename = "backup_app")]
24+
Backup
25+
}
26+
impl Debug for Feature {
27+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28+
f.write_str(
29+
match self {
30+
Self::Backup => "backup",
31+
Self::Commit => "commit",
32+
Self::Restart => "restart",
33+
Self::SeeLogs => "logs",
34+
Self::SetRam => "ram",
35+
Self::Start => "start",
36+
Self::Status => "status",
37+
Self::Stop => "stop"
38+
}
39+
)
40+
}
41+
}
42+
#[derive(Deserialize, Serialize)]
43+
pub struct Mod {
44+
#[serde(rename = "mod_id")]
45+
user_id: u128,
46+
#[serde(rename = "perms")]
47+
features: Vec<Feature>
48+
}
49+
impl Mod {
50+
pub fn new(token: String, user: &User, app: &App, features: Vec<Feature>) -> Result<Mod, FetchError> {
51+
let moderator = Self {
52+
user_id: user.user_id.parse().unwrap(),
53+
features
54+
};
55+
moderator.add_to(token, app)?;
56+
Ok(moderator)
57+
}
58+
pub fn get_features(&self) -> Vec<Feature> {
59+
self.features.clone()
60+
}
61+
pub fn add_to(&self, token: String, app: &App) -> Result<(), FetchError>{
62+
#[derive(Deserialize)]
63+
struct Response {
64+
status: String,
65+
message: Option<String>
66+
}
67+
let client = reqwest::blocking::Client::new();
68+
let req = client
69+
.post(crate::api_url!(format!("/app/{}/team", app.id)))
70+
.header("api-token", token)
71+
.json(self);
72+
match req.send() {
73+
Ok(res) => {
74+
match res.json::<Response>() {
75+
Err(err) => {
76+
Err(FetchError::FailedWithMessage(err.to_string()))
77+
}
78+
Ok(response) => {
79+
if response.status == "ok" {
80+
Ok(())
81+
} else {
82+
Err(FetchError::FailedWithMessage(response.message.unwrap()))
83+
}
84+
}
85+
}
86+
87+
}
88+
Err(err) => Err(FetchError::FailedToConnect(err)),
89+
}
90+
}
91+
}

src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ fn main() -> std::io::Result<()> {
101101
Command::new("aboutme")
102102
.about("Shows information about you.")
103103
.alias("user")
104+
)
105+
.subcommand(
106+
Command::new("mods")
107+
.about("Manages your apps' mods")
108+
.subcommand_required(true)
109+
.arg_required_else_help(true)
110+
.alias("m")
111+
.subcommand(
112+
Command::new("add")
113+
.about("Adds a mod to an app, by default, the mod can only see the logs and status, use `discloud mods allow` to allow more actions.")
114+
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set).required(true))
115+
)
116+
.after_help("Be careful with what people you add and what permissions you give: With Great Power comes Great Responsability.")
104117
);
105118
let matches = cmd.get_matches();
106119
match matches.subcommand() {
@@ -144,6 +157,16 @@ fn main() -> std::io::Result<()> {
144157
commands::aboutme::aboutme();
145158
Ok(())
146159
}
160+
Some(("mods", matches)) => {
161+
match matches.subcommand() {
162+
Some(("add", matches)) => {
163+
let id: u128 = *matches.get_one("id").unwrap();
164+
commands::mods::add::add(id);
165+
Ok(())
166+
}
167+
_ => unreachable!()
168+
}
169+
}
147170
_ => unreachable!(),
148171
}
149172
}

0 commit comments

Comments
 (0)