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

Commit fa6ec3e

Browse files
feat(#1): add init command
2 parents 4f54014 + 1bb9f66 commit fa6ec3e

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2021"
88
[dependencies]
99
clap = { version = "3.2.20", features = ["derive"] }
1010
colored = "2.0.0"
11+
dialoguer = "0.10.2"
1112
directories = "4.0.1"
1213
reqwest = { version = "0.11.11", features = ["blocking", "json"] }
1314
sentry = "0.27.0"

src/commands/init.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
use colored::Colorize;
2+
use dialoguer::theme::ColorfulTheme;
3+
4+
fn vec_from_str(s: String) -> Vec<String> {
5+
s.split(",").map(|s|s.trim().into()).collect()
6+
}
7+
8+
#[derive(Default)]
9+
enum AppTyp {
10+
#[default]
11+
Bot,
12+
Site,
13+
}
14+
#[derive(Default)]
15+
struct App {
16+
typ: AppTyp,
17+
id: u128,
18+
subdomain: String,
19+
ram: u64,
20+
main: String,
21+
autorestart: bool,
22+
apt: Vec<String>,
23+
}
24+
impl App {
25+
fn get_config(&self) -> String {
26+
match &self.typ {
27+
AppTyp::Site => {
28+
if self.apt.len() > 0 {
29+
format!("ID={}\nMAIN={}\nAUTORESTART={}\nRAM={}\nAPT={}\nTYPE=site\nVERSION=latest", self.subdomain, self.main, self.autorestart, self.ram, self.apt.join(","))
30+
} else {
31+
format!("ID={}\nMAIN={}\nAUTORESTART={}\nRAM={}\nTYPE=site\nVERSION=latest", self.subdomain, self.main, self.autorestart, self.ram)
32+
}
33+
}
34+
AppTyp::Bot => {
35+
if self.apt.len() > 0 {
36+
format!("ID={}\nMAIN={}\nAUTORESTART={}\nRAM={}\nAPT={}\nTYPE=bot\nVERSION=latest", self.id, self.main, self.autorestart, self.ram, self.apt.join(","))
37+
} else {
38+
format!("ID={}\nMAIN={}\nAUTORESTART={}\nRAM={}\nTYPE=bot\nVERSION=latest", self.id, self.main, self.autorestart, self.ram)
39+
}
40+
}
41+
}
42+
}
43+
}
44+
pub fn init() -> std::io::Result<()> {
45+
use dialoguer::Input;
46+
if std::path::Path::new("discloud.config").exists() {
47+
println!("{} discloud.config already exists", "!".yellow().bold());
48+
}
49+
let typ: String = Input::with_theme(&ColorfulTheme::default())
50+
.with_prompt("Type")
51+
.default("bot".into())
52+
.show_default(true)
53+
.validate_with(|t: &String| {
54+
if t == "bot" || t == "site" {
55+
Ok(())
56+
} else {
57+
Err("Only `bot` and `site` are valid")
58+
}
59+
})
60+
.interact_text()?;
61+
let mut app: App = Default::default();
62+
match typ.as_str() {
63+
"bot" => {
64+
app.typ = AppTyp::Bot;
65+
app.id = Input::with_theme(&ColorfulTheme::default())
66+
.with_prompt("Bot ID")
67+
.interact_text()?;
68+
}
69+
"site" => {
70+
app.typ = AppTyp::Site;
71+
app.subdomain = Input::with_theme(&ColorfulTheme::default())
72+
.with_prompt("Subdomain")
73+
.interact_text()?;
74+
}
75+
_ => unreachable!(),
76+
}
77+
app.main = Input::with_theme(&ColorfulTheme::default())
78+
.with_prompt("Main File")
79+
.interact_text()?;
80+
app.autorestart = Input::with_theme(&ColorfulTheme::default())
81+
.with_prompt("AutoRestart?")
82+
.default(false)
83+
.interact_text()?;
84+
app.ram = Input::with_theme(&ColorfulTheme::default())
85+
.with_prompt("Memory (MB)")
86+
.interact_text()?;
87+
let apt: String = Input::with_theme(&ColorfulTheme::default())
88+
.with_prompt("APT Packages")
89+
.allow_empty(true)
90+
.interact_text()?;
91+
if apt.len() > 0 {
92+
app.apt = vec_from_str(apt);
93+
}
94+
std::fs::write("discloud.config", app.get_config())?;
95+
println!("{} discloud.config was created succesfully!", "✔".green().bold());
96+
Ok(())
97+
}

src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod login;
22
pub mod authstatus;
3+
pub mod init;
34
use spinners::*;
45
use colored::Colorize;
56
pub fn check_token() {

src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,17 @@ fn main() -> std::io::Result<()>{
3535
.subcommand(
3636
Command::new("authstatus")
3737
.about("Checks if you're logged in")
38+
)
39+
.subcommand(
40+
Command::new("init")
41+
.about("Creates a discloud.config file")
42+
.alias("i")
3843
);
3944
let matches = cmd.get_matches();
4045
match matches.subcommand() {
4146
Some(("login", login_matches)) => commands::login::login(login_matches),
4247
Some(("authstatus", _)) => commands::authstatus::authstatus(),
48+
Some(("init", _)) => commands::init::init(),
4349
_ => unreachable!()
4450
}
4551
}

0 commit comments

Comments
 (0)