Skip to content

Commit

Permalink
network stuff and fix util semver fn
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Nov 13, 2023
1 parent 968004a commit 41dc4b5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
65 changes: 64 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,75 @@ impl App {
"MOD_COUNT" => Some(self.server.mods.len().to_string()),
"WORLD_COUNT" => Some(self.server.worlds.len().to_string()),
"CLIENTSIDE_MOD_COUNT" => Some(self.server.clientsidemods.len().to_string()),

"NETWORK_NAME" => Some(self.network.as_ref()?.name.clone()),
"NETWORK_PORT" => Some(self.network.as_ref()?.port.to_string()),

"NETWORK_VELOCITY_SERVERS" => {
if let Some(nw) = &self.network {
Some("# generated by mcman\n".to_owned() + &nw.servers.iter().map(|(name, serv)|
format!(
"{name} = \"{}:{}\"",
std::env::var(format!("IP_{name}")).ok()
.or(serv.ip_address.clone())
.unwrap_or("127.0.0.1".to_owned()),
std::env::var(format!("PORT_{name}")).ok()
.unwrap_or(serv.port.to_string()),
)
).collect::<Vec<_>>().join("\n"))
} else {
None
}

Check failure on line 123 in src/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

manual implementation of `Option::map`

error: manual implementation of `Option::map` --> src/app/mod.rs:110:17 | 110 | / if let Some(nw) = &self.network { 111 | | Some("# generated by mcman\n".to_owned() + &nw.servers.iter().map(|(name, serv)| 112 | | format!( 113 | | "{name} = \"{}:{}\"", ... | 122 | | None 123 | | } | |_________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map help: try | 110 ~ self.network.as_ref().map(|nw| "# generated by mcman\n".to_owned() + &nw.servers.iter().map(|(name, serv)| 111 + format!( 112 + "{name} = \"{}:{}\"", 113 + std::env::var(format!("IP_{name}")).ok() 114 + .or(serv.ip_address.clone()) 115 + .unwrap_or("127.0.0.1".to_owned()), 116 + std::env::var(format!("PORT_{name}")).ok() 117 + .unwrap_or(serv.port.to_string()), 118 + ) 119 + ).collect::<Vec<_>>().join("\n")) |
},

"NETWORK_BUNGEECORD_SERVERS" => {
if let Some(nw) = &self.network {
Some("# generated by mcman\nservers:".to_owned() + &nw.servers.iter().map(|(name, serv)|
format!(
" {name}:\n motd: {}\n address: {}:{}\n restricted: false",
self.var("MOTD").unwrap_or("a mcman-powered server".to_owned()),
std::env::var(format!("IP_{name}")).ok()
.or(serv.ip_address.clone())
.unwrap_or("127.0.0.1".to_owned()),
std::env::var(format!("PORT_{name}")).ok()
.unwrap_or(serv.port.to_string()),
)
).collect::<Vec<_>>().join("\n"))
} else {
None
}

Check failure on line 141 in src/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

manual implementation of `Option::map`

error: manual implementation of `Option::map` --> src/app/mod.rs:127:17 | 127 | / if let Some(nw) = &self.network { 128 | | Some("# generated by mcman\nservers:".to_owned() + &nw.servers.iter().map(|(name, serv)| 129 | | format!( 130 | | " {name}:\n motd: {}\n address: {}:{}\n restricted: false", ... | 140 | | None 141 | | } | |_________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map help: try | 127 ~ self.network.as_ref().map(|nw| "# generated by mcman\nservers:".to_owned() + &nw.servers.iter().map(|(name, serv)| 128 + format!( 129 + " {name}:\n motd: {}\n address: {}:{}\n restricted: false", 130 + self.var("MOTD").unwrap_or("a mcman-powered server".to_owned()), 131 + std::env::var(format!("IP_{name}")).ok() 132 + .or(serv.ip_address.clone()) 133 + .unwrap_or("127.0.0.1".to_owned()), 134 + std::env::var(format!("PORT_{name}")).ok() 135 + .unwrap_or(serv.port.to_string()), 136 + ) 137 + ).collect::<Vec<_>>().join("\n")) |
},

"TECHNOBLADE" => Some("Technoblade never dies".to_owned()),

k => if let Some(v) = std::env::var(k).ok() {

Check failure on line 146 in src/app/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

matching on `Some` with `ok()` is redundant

error: matching on `Some` with `ok()` is redundant --> src/app/mod.rs:146:18 | 146 | k => if let Some(v) = std::env::var(k).ok() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_result_ok = note: `#[deny(clippy::match_result_ok)]` implied by `#[deny(clippy::all)]` help: consider matching on `Ok(v)` and removing the call to `ok` instead | 146 | k => if let Ok(v) = std::env::var(k) { | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Some(v)
} else {
if k.starts_with("NW_") {
if let Some(nw) = &self.network {
nw.variables.get(k.strip_prefix("NW_").unwrap()).cloned()
if k.starts_with("NW_SERVER_") {
let (name, ty) = k.strip_prefix("NW_SERVER_")
.unwrap()
.split_once('_')?;

let serv = nw.servers.get(&name.to_lowercase())?;

let ip = std::env::var(format!("IP_{name}")).ok()
.or(serv.ip_address.clone())
.unwrap_or("127.0.0.1".to_owned());

let port = std::env::var(format!("PORT_{name}")).ok()
.unwrap_or(serv.port.to_string());

match ty.to_lowercase().as_str() {
"ip" => Some(ip),
"port" => Some(port),
"address" => Some(format!("{ip}:{port}")),
_ => None,
}
} else {
nw.variables.get(k.strip_prefix("NW_").unwrap()).cloned()
}
} else {
None
}
Expand Down
9 changes: 7 additions & 2 deletions src/hot_reload/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,11 @@ impl<'a> DevSession<'a> {

self.builder.app.info("Server process exited")?;

is_stopping = false;
child = None;
stdout_lines = None;

if self.test_mode {
tx.send(Command::WaitUntilExit).await?;
tx.send(Command::EndSession).await?;
}
},
Expand All @@ -260,6 +263,8 @@ impl<'a> DevSession<'a> {
}
}

// end of loop > tokio::select!

if let Some(ref mut child) = &mut child {
self.builder.app.info("Killing undead child process...")?;
child.kill().await?;
Expand Down Expand Up @@ -337,7 +342,7 @@ impl<'a> DevSession<'a> {
drop(content);

pb.finish_and_clear();
self.builder.app.success("Log uploaded to mclo.gs")?;
self.builder.app.log(" - Log uploaded to mclo.gs")?;
mp.suspend(|| {
println!();
println!(" -- [ {} ] --", log.url);
Expand Down
4 changes: 4 additions & 0 deletions src/model/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Network {
pub name: String,
pub proxy: String,
pub port: u16,
pub servers: HashMap<String, ServerEntry>,
pub variables: HashMap<String, String>,

#[serde(skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Default for Network {
name: String::new(),
proxy: "proxy".to_owned(),
port: 25565,
servers: HashMap::new(),
variables: HashMap::new(),
plugins: vec![],
mods: vec![],
Expand All @@ -85,4 +87,6 @@ impl Default for Network {
#[serde(default)]
pub struct ServerEntry {
pub port: u16,
pub ip_address: Option<String>,
pub groups: Vec<String>,
}
36 changes: 32 additions & 4 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cmp::Ordering;

use anyhow::Result;
use regex::Regex;

Expand Down Expand Up @@ -25,12 +27,38 @@ pub fn is_default_str(s: &str) -> bool {
pub fn get_latest_semver(list: &[String]) -> Option<String> {
let mut list = list
.iter()
.filter_map(|s| semver::Version::parse(s).ok())
.collect::<Vec<_>>();
.map(|s| s.split('.').collect())
.collect::<Vec<Vec<_>>>();

list.sort_by(|a, b| {
let mut ia = a.iter();
let mut ib = b.iter();
loop {
break match (ia.next(), ib.next()) {
(Some(a), Some(b)) => {
let a = a.parse::<i32>();
let b = b.parse::<i32>();

list.sort_by(semver::Version::cmp);
match (a, b) {
(Ok(a), Ok(b)) => {
match a.cmp(&b) {
Ordering::Equal => continue,
ord => ord,
}
}
(Err(_), Ok(_)) => Ordering::Less,
(Ok(_), Err(_)) => Ordering::Greater,
_ => Ordering::Equal,
}
}
(Some(_), None) => Ordering::Greater,
(None, Some(_)) => Ordering::Less,
_ => Ordering::Equal,
}
}
});

list.last().map(ToString::to_string)
list.last().map(|v| v.join("."))
}

/// ci.luckto.me => ci-lucko-me
Expand Down

0 comments on commit 41dc4b5

Please sign in to comment.