|
| 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 | +} |
0 commit comments