Skip to content
This repository was archived by the owner on Sep 1, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
[package]
name = "discloud"
rust-version = "1.63.0"
authors = ["coffee-is-power"]
repository = "https://github.com/discloud/cli-rust/"
version = "0.3.2-alpha"
edition = "2021"
description = "Blazingly fast Discloud CLI"
license = "Apache-2.0"
license-file = "LICENSE"
categories = ["command-line-utilities"]
keywords = ["discloud", "cli", "bot", "fast", "discord"]
readme = "README.md"

[dependencies]
chrono = { version = "0.4.22", features = ["serde"] }
Expand All @@ -14,6 +21,7 @@ directories = "4.0.1"
reqwest = { version = "0.11.11", features = ["blocking", "json", "multipart", "rustls-tls"], default-features=false }
sentry = {version = "0.27.0", default-features = false, features = ["rustls", "reqwest"]}
serde = { version = "1.0.144", features = ["derive"] }
serde-enum-str = "0.2.5"
spinners = "4.1.0"
walkdir = "2.3.2"
zip = "0.6.2"
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod mods;
pub mod aboutme;
pub mod logs;
pub mod stop;
Expand Down
20 changes: 20 additions & 0 deletions src/commands/mods/add.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{commands::{expect_token, ask_for_app}, entities::{moderator::{Mod, Feature}, app::App, user::fetch_user}};
macro_rules! handle_result {
($v:expr) => {
match $v {
Ok(v) => v,
Err(err) => {
super::err(&err.to_string());
std::process::exit(1);
}
}
}
}
pub fn add(id: u128) {
let token = expect_token();
let app_id = handle_result!(ask_for_app(token.clone(), "add a moderator"));
let app = handle_result!(App::fetch(token.clone(), app_id));
let user = handle_result!(fetch_user(token.clone()));
let moderator = handle_result!(Mod::new(token.clone(), &user, &app, vec![Feature::SeeLogs, Feature::Status]));
super::log(&format!("Permissions {:?} have been given to {}", moderator.get_features(), id));
}
2 changes: 2 additions & 0 deletions src/commands/mods/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod add;
use super::*;
2 changes: 2 additions & 0 deletions src/entities/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,6 @@ impl App {
Err(err) => Err(FetchError::FailedToConnect(err)),
}
}


}
1 change: 1 addition & 0 deletions src/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod moderator;
use std::fmt::Display;

pub mod app;
Expand Down
91 changes: 91 additions & 0 deletions src/entities/moderator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::fmt::Debug;

use serde::{Deserialize, Serialize};
use serde_enum_str::*;

use super::{user::User, app::App, FetchError};
#[derive(Deserialize_enum_str, Serialize_enum_str, Clone)]
pub enum Feature {
#[serde(rename = "start_app")]
Start,
#[serde(rename = "stop_app")]
Stop,
#[serde(rename = "restart_app")]
Restart,
#[serde(rename = "logs_app")]
SeeLogs,
#[serde(rename = "commit_app")]
Commit,
#[serde(rename = "status_app")]
Status,
#[serde(rename = "edit_ram")]
SetRam,
#[serde(rename = "backup_app")]
Backup
}
impl Debug for Feature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(
match self {
Self::Backup => "backup",
Self::Commit => "commit",
Self::Restart => "restart",
Self::SeeLogs => "logs",
Self::SetRam => "ram",
Self::Start => "start",
Self::Status => "status",
Self::Stop => "stop"
}
)
}
}
#[derive(Deserialize, Serialize)]
pub struct Mod {
#[serde(rename = "mod_id")]
user_id: u128,
#[serde(rename = "perms")]
features: Vec<Feature>
}
impl Mod {
pub fn new(token: String, user: &User, app: &App, features: Vec<Feature>) -> Result<Mod, FetchError> {
let moderator = Self {
user_id: user.user_id.parse().unwrap(),
features
};
moderator.add_to(token, app)?;
Ok(moderator)
}
pub fn get_features(&self) -> Vec<Feature> {
self.features.clone()
}
pub fn add_to(&self, token: String, app: &App) -> Result<(), FetchError>{
#[derive(Deserialize)]
struct Response {
status: String,
message: Option<String>
}
let client = reqwest::blocking::Client::new();
let req = client
.post(crate::api_url!(format!("/app/{}/team", app.id)))
.header("api-token", token)
.json(self);
match req.send() {
Ok(res) => {
match res.json::<Response>() {
Err(err) => {
Err(FetchError::FailedWithMessage(err.to_string()))
}
Ok(response) => {
if response.status == "ok" {
Ok(())
} else {
Err(FetchError::FailedWithMessage(response.message.unwrap()))
}
}
}

}
Err(err) => Err(FetchError::FailedToConnect(err)),
}
}
}
23 changes: 23 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ fn main() -> std::io::Result<()> {
Command::new("aboutme")
.about("Shows information about you.")
.alias("user")
)
.subcommand(
Command::new("mods")
.about("Manages your apps' mods")
.subcommand_required(true)
.arg_required_else_help(true)
.alias("m")
.subcommand(
Command::new("add")
.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.")
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set).required(true))
)
.after_help("Be careful with what people you add and what permissions you give: With Great Power comes Great Responsability.")
);
let matches = cmd.get_matches();
match matches.subcommand() {
Expand Down Expand Up @@ -144,6 +157,16 @@ fn main() -> std::io::Result<()> {
commands::aboutme::aboutme();
Ok(())
}
Some(("mods", matches)) => {
match matches.subcommand() {
Some(("add", matches)) => {
let id: u128 = *matches.get_one("id").unwrap();
commands::mods::add::add(id);
Ok(())
}
_ => unreachable!()
}
}
_ => unreachable!(),
}
}