This repository was archived by the owner on Sep 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
334 lines (304 loc) · 11.3 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
pub mod auth;
mod commands;
pub mod config_dir;
pub mod entities;
pub mod zip_directory;
use clap::*;
use entities::moderator::Feature;
use tracing_subscriber::prelude::*;
#[macro_export]
macro_rules! api_url {
() => {
"https://api.discloud.app/v2"
};
($api:expr) => {
format!("{}{}", $crate::api_url!(), $api)
};
($api:literal) => {
concat!($crate::api_url!(), $api)
};
}
fn main() {
tracing_subscriber::Registry::default()
.with(sentry::integrations::tracing::layer())
.init();
fn parse_feature(feature: &str) -> Result<Feature, String> {
match feature {
"start" => Ok(Feature::Start),
"stop" => Ok(Feature::Stop),
"restart" => Ok(Feature::Restart),
"logs" => Ok(Feature::SeeLogs),
"commit" => Ok(Feature::Commit),
"status" => Ok(Feature::Status),
"setram" => Ok(Feature::SetRam),
"backup" => Ok(Feature::Backup),
_ => Err(format!("Invalid permission: {}", feature)),
}
}
let _guard = sentry::init((
"https://[email protected]/3",
sentry::ClientOptions {
release: sentry::release_name!(),
traces_sample_rate: if cfg!(debug_assertions) { 1.0 } else { 0.2 },
..Default::default()
},
));
if let Some(dir) = config_dir::get_proj_dir() {
if let Err(err) = std::fs::create_dir_all(dir) {
eprintln!("ERROR: Couldn't create a directory for config files: {err}");
}
} else {
eprintln!("ERROR: Couldn't find a directory for config files.");
return;
}
let cmd = Command::new("discloud")
.about("Blazingly Fast CLI for discloud")
.subcommand_required(true)
.arg_required_else_help(true)
.author("Tiago Dinis")
.version(env!("CARGO_PKG_VERSION"))
.subcommand(
Command::new("login")
.about("Sets the Discloud API token, use .api command on #commands to generate one")
.alias("l")
.arg(
Arg::new("token")
.required(true)
.action(ArgAction::Set)
)
)
.subcommand(
Command::new("authstatus")
.about("Checks if you're logged in")
)
.subcommand(
Command::new("init")
.about("Creates a discloud.config file")
.alias("i")
)
.subcommand(
Command::new("upload")
.about("Creates an app on discloud")
.alias("up")
)
.subcommand(
Command::new("commit")
.about("Commits to an app on discloud. If you have more than one app, it will ask which app you want to commit to.")
.alias("c")
)
.subcommand(
Command::new("remove")
.about("Removes an app. If you have more than one app, it will ask which app you want to delete.")
.alias("rm")
.alias("rb")
)
.subcommand(
Command::new("apps")
.about("Shows all your apps.")
.alias("ls")
.alias("list")
)
.subcommand(
Command::new("stop")
.about("Stops an app.")
.alias("shutdown")
.alias("down")
)
.subcommand(
Command::new("start")
.about("Starts a stopped app.")
)
.subcommand(
Command::new("restart")
.about("Restarts an app.")
.alias("reboot")
.alias("r")
)
.subcommand(
Command::new("logs")
.about("Prints logs of an app.")
.alias("terminal")
.alias("t")
)
.subcommand(
Command::new("aboutme")
.about("Shows information about you.")
.alias("user")
)
.subcommand(
Command::new("teams")
.about("Allows to interact with other people's bots, you can use this once someone adds you as a moderator.")
.subcommand_required(true)
.arg_required_else_help(true)
.subcommand(
Command::new("apps")
.about("Shows all bots you have access to.")
.alias("ls")
.alias("list")
)
.subcommand(
Command::new("start")
.about("Starts a stopped app.")
)
.subcommand(
Command::new("restart")
.about("Restarts an app.")
.alias("reboot")
.alias("r")
)
.subcommand(
Command::new("logs")
.about("Prints logs of an app.")
.alias("terminal")
.alias("t")
)
.subcommand(
Command::new("stop")
.about("Stops an app.")
.alias("shutdown")
.alias("down")
)
.subcommand(
Command::new("commit")
.about("Commits to an app on discloud. If you have more than one shared app, it will ask which app you want to commit to.")
.alias("c")
)
)
.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))
)
.subcommand(
Command::new("remove")
.alias("rm")
.about("Removes a moderator from your app.")
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set).required(true))
)
.subcommand(
Command::new("allow")
.about("Gives permissions to a moderator")
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set)
.required(true))
.arg(
Arg::new("perm")
.value_parser(parse_feature)
.action(clap::ArgAction::Append)
.required(true)
)
)
.subcommand(
Command::new("deny")
.about("Removes permissions from a moderator")
.arg(Arg::new("id").value_parser(value_parser!(u128)).action(clap::ArgAction::Set)
.required(true))
.arg(
Arg::new("perm")
.value_parser(parse_feature)
.action(clap::ArgAction::Append)
.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() {
Some(("login", login_matches)) => {
if let Err(err) = commands::login::login(login_matches) {
sentry::capture_error(&err);
}
},
Some(("authstatus", _)) => {
if let Err(err) = commands::authstatus::authstatus() {
sentry::capture_error(&err);
}
},
Some(("init", _)) => {
if let Err(err) = commands::init::init() {
sentry::capture_error(&err);
}
},
Some(("upload", _)) => {
commands::upload::upload();
}
Some(("commit", _)) => {
commands::commit::commit(false);
}
Some(("remove", _)) => {
commands::remove::remove();
}
Some(("apps", _)) => {
commands::apps::apps(false);
}
Some(("stop", _)) => {
commands::stop::stop(false);
}
Some(("start", _)) => {
commands::start::start(false);
}
Some(("restart", _)) => {
commands::restart::restart(false);
}
Some(("logs", _)) => {
commands::logs::logs(false);
}
Some(("aboutme", _)) => {
commands::aboutme::aboutme();
}
Some(("mods", matches)) => match matches.subcommand() {
Some(("add", matches)) => {
let id: u128 = *matches.get_one("id").unwrap();
commands::mods::add::add(id);
}
Some(("remove", matches)) => {
let id: u128 = *matches.get_one("id").unwrap();
commands::mods::remove::remove(id);
}
Some(("deny", matches)) => {
let id: u128 = *matches.get_one("id").unwrap();
let features: Vec<Feature> = matches.get_many("perm").unwrap().cloned().collect();
commands::mods::deny::deny(id, features);
}
Some(("allow", matches)) => {
let id: u128 = *matches.get_one("id").unwrap();
let features: Vec<Feature> = matches.get_many("perm").unwrap().cloned().collect();
commands::mods::allow::allow(id, features);
}
cmd => {
eprintln!("{:#?} command is not implemented yet.", cmd.unwrap().0);
}
},
Some(("teams", matches)) => match matches.subcommand() {
Some(("commit", _)) => {
commands::commit::commit(true);
}
Some(("apps", _)) => {
commands::apps::apps(true);
}
Some(("stop", _)) => {
commands::stop::stop(true);
}
Some(("start", _)) => {
commands::start::start(true);
}
Some(("restart", _)) => {
commands::restart::restart(true);
}
Some(("logs", _)) => {
commands::logs::logs(true);
}
cmd => {
eprintln!("{:#?} command is not implemented yet.", cmd.unwrap().0);
}
},
cmd => {
eprintln!("{:#?} command is not implemented yet.", cmd.unwrap().0);
}
}
}