Skip to content

Commit 8c33404

Browse files
committed
refactor
1 parent b6644a1 commit 8c33404

File tree

6 files changed

+230
-192
lines changed

6 files changed

+230
-192
lines changed

src/cli.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::software::Software;
2+
use clap::{Parser, Subcommand};
3+
4+
#[derive(Parser)]
5+
#[command(
6+
version = "2.2.2",
7+
author = "Maoyue (MagicTeaMC)",
8+
about = "Manage Minecraft server / proxy quickly and easily!"
9+
)]
10+
pub struct CLI {
11+
#[command(subcommand)]
12+
pub command: Commands,
13+
}
14+
15+
#[derive(Subcommand)]
16+
pub enum Commands {
17+
/// initial setup
18+
Setup {
19+
/// software to use (paper/folia/purpur/velocity/gate/nukkit/geyser)
20+
#[arg(short, long, value_enum)]
21+
software: Option<Software>,
22+
23+
/// Minecraft version (eg. 1.21.1)
24+
#[arg(short, long)]
25+
mc_version: Option<String>,
26+
27+
/// do you agree www.minecraft.net/en-us/eula?
28+
#[arg(short, long)]
29+
eula: Option<bool>,
30+
31+
/// skip confirmation prompt
32+
#[arg(short, default_value_t = false)]
33+
yes: bool,
34+
},
35+
/// update to latest build of this version
36+
Update,
37+
/// upgrade to another version
38+
Upgrade {
39+
/// your target minecraft version
40+
#[arg(short, long)]
41+
version: Option<String>,
42+
},
43+
}

src/config.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::software::Software;
2+
use serde::{Deserialize, Serialize};
3+
use std::{fs, path::Path};
4+
5+
const CONFIG_FILE: &str = "mcsast.config.json";
6+
7+
#[derive(Serialize, Deserialize)]
8+
pub struct Config {
9+
pub software: Software,
10+
pub minecraft_version: String,
11+
pub eula_accepted: bool,
12+
}
13+
14+
impl Config {
15+
pub fn load() -> Result<Self, Box<dyn std::error::Error>> {
16+
if !Path::new(CONFIG_FILE).exists() {
17+
return Err("No config file found. Please run 'mcsast setup' first.".into());
18+
}
19+
20+
let content = fs::read_to_string(CONFIG_FILE)?;
21+
let config: Config = serde_json::from_str(&content)?;
22+
Ok(config)
23+
}
24+
25+
pub fn save(&self) -> Result<(), Box<dyn std::error::Error>> {
26+
let content = serde_json::to_string_pretty(self)?;
27+
fs::write(CONFIG_FILE, content)?;
28+
Ok(())
29+
}
30+
31+
pub fn config_file() -> &'static str {
32+
CONFIG_FILE
33+
}
34+
}

src/softwares.rs renamed to src/get_files.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ pub fn get_geyser(_version: String) -> Result<(), Box<dyn std::error::Error>> {
5050
let client = reqwest::blocking::Client::builder()
5151
.timeout(std::time::Duration::from_secs(120))
5252
.build()?;
53-
53+
5454
let url = "https://download.geysermc.org/v2/projects/geyser/versions/latest/builds/latest/downloads/standalone";
55-
55+
5656
let res = client.get(url).send()?;
5757

5858
if res.status().is_success() {
@@ -67,9 +67,9 @@ pub fn get_nukkit(_version: String) -> Result<(), Box<dyn std::error::Error>> {
6767
let client = reqwest::blocking::Client::builder()
6868
.timeout(std::time::Duration::from_secs(120))
6969
.build()?;
70-
70+
7171
let url = "https://repo.opencollab.dev/api/maven/latest/file/maven-snapshots/cn/nukkit/nukkit/1.0-SNAPSHOT?extension=jar";
72-
72+
7373
let res = client.get(url).send()?;
7474

7575
if res.status().is_success() {

0 commit comments

Comments
 (0)