Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Aug 29, 2023
1 parent ac61ee9 commit 2bdd82e
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 232 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ walkdir = "2.3"
zip = "0.6"
sha2 = "0.10"
notify = { version = "6.0.1", default-features = false }
glob-match = "0.2"
glob = "0.3"
async-trait = "0.1.72"
roxmltree = "0.18"
cached = "0.44.0"
1 change: 1 addition & 0 deletions examples/datapacks/config/test.abc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aeee
2 changes: 1 addition & 1 deletion examples/datapacks/server.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ proxy_flags = false
nogui = true
eula_args = true

[[plugins]]
[[worlds.world.datapacks]]
type = "modrinth"
id = "vanilla-refresh"
version = "c1SnJlLJ"
Expand Down
157 changes: 0 additions & 157 deletions src/bootstrapper/mod.rs

This file was deleted.

38 changes: 36 additions & 2 deletions src/commands/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::fs::{self, File};
use std::io::Write;

use crate::create_http_client;
use crate::model::Server;
use crate::model::{Server, World};
use crate::util::md::MarkdownTable;
use anyhow::{Context, Result};
use clap::Command;
Expand All @@ -28,6 +28,10 @@ pub static ADDONS_REGEX: &str = r"(<!--start:mcman-addons-->)([\w\W]*)(<!--end:m
pub static ADDONS_START: &str = "<!--start:mcman-addons-->";
pub static ADDONS_END: &str = "<!--end:mcman-addons-->";

pub static DP_REGEX: &str = r"(<!--start:mcman-datapacks-->)([\w\W]*)(<!--end:mcman-datapacks-->)";
pub static DP_START: &str = "<!--start:mcman-datapacks-->";
pub static DP_END: &str = "<!--end:mcman-datapacks-->";

pub async fn run() -> Result<()> {
let mut server = Server::load().context("Failed to load server.toml")?;
let http_client = create_http_client()?;
Expand Down Expand Up @@ -65,8 +69,22 @@ pub async fn update_files(http_client: &reqwest::Client, server: &Server) -> Res
let addon_list_text =
{ ADDONS_START.to_owned() + NOTICE + "\n" + &addons_table.render() + "\n" + ADDONS_END };

let dp_text =
{
let mut sections = vec![];

for (name, w) in &server.worlds {
let table = create_table_world(http_client, w).await?.render();

sections.push(format!("# {name}\n\n{table}"));
}

DP_START.to_owned() + NOTICE + "\n" + &sections.join("\n\n") + "\n" + DP_END
};

let serv_regex = Regex::new(SERVERINFO_REGEX).unwrap();
let addon_regex = Regex::new(ADDONS_REGEX).unwrap();
let dp_regex = Regex::new(DP_REGEX).unwrap();

let len = server.markdown.files.len();
for (idx, filename) in server.markdown.files.iter().enumerate() {
Expand All @@ -93,8 +111,11 @@ pub async fn update_files(http_client: &reqwest::Client, server: &Server) -> Res
let stage2 =
addon_regex.replace_all(&stage1, |_caps: &regex::Captures| addon_list_text.clone());

let stage3 =
dp_regex.replace_all(&stage2, |_caps: &regex::Captures| dp_text.clone());

let mut f = File::create(&path)?;
f.write_all(stage2.as_bytes())?;
f.write_all(stage3.as_bytes())?;

println!(
" ({:w$}/{len}) Updated {}!",
Expand Down Expand Up @@ -135,6 +156,19 @@ pub async fn create_table_addons(
Ok(table)
}

pub async fn create_table_world(
http_client: &reqwest::Client,
world: &World
) -> Result<MarkdownTable> {
let mut table = MarkdownTable::new();

for dp in &world.datapacks {
table.add_from_map(&dp.fetch_info_to_map(http_client).await?);
}

Ok(table)
}

pub fn create_table_server_console(server: &Server) -> MarkdownTable {
let mut map = IndexMap::new();

Expand Down
79 changes: 41 additions & 38 deletions src/commands/pull.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,76 @@
use std::{collections::HashMap, fs, path::PathBuf};
use std::{fs, path::PathBuf};

use anyhow::{anyhow, bail, Context, Result};
use clap::{arg, value_parser, ArgMatches, Command};
use clap::{arg, ArgMatches, Command};
use console::style;
use dialoguer::{theme::ColorfulTheme, Confirm};
use glob::glob;
use pathdiff::diff_paths;

use crate::{bootstrapper::BootstrapContext, model::Server};
use crate::model::Server;

pub fn cli() -> Command {
Command::new("pull")
.about("Pull a config file from server/ to config/")
.about("Pull files from server/ to config/")
.arg(
arg!(<file> "File to pull")
.value_parser(value_parser!(PathBuf))
arg!(<file> "Files to pull")
.required(true),
)
}

pub fn run(matches: &ArgMatches) -> Result<()> {
let server = Server::load().context("Failed to load server.toml")?;

let path = matches.get_one::<PathBuf>("file").unwrap();
let cannon = fs::canonicalize(path).context("Resolving absolute path of file")?;
let diff =
diff_paths(&cannon, fs::canonicalize(&server.path)?).ok_or(anyhow!("Cannot diff paths"))?;
let files = matches.get_one::<String>("file").unwrap();

if !diff.starts_with("server") {
bail!("You aren't inside server/");
}
for entry in glob(files)? {
let entry = entry?;

// i got lazy ok
let diff =
diff_paths(&entry, fs::canonicalize(&server.path)?).ok_or(anyhow!("Cannot diff paths"))?;

let cx = BootstrapContext {
output_dir: server.path.join("config"),
vars: HashMap::new(),
};
if !diff.starts_with("server") {
bail!("You aren't inside server/");
}

let destination = cx.get_output_path(&diff);
let mut destination = PathBuf::new();
let mut iter = diff.components();
iter.next().expect("Path to have atleast 1 component");
destination.push(&server.path);
destination.push("config");
destination.extend(iter);

fs::create_dir_all(destination.parent().unwrap()).context("Failed to create dirs")?;

fs::create_dir_all(destination.parent().unwrap()).context("Failed to create dirs")?;

if destination.exists()
if destination.exists()
&& !Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(format!(
"File '{}' already exists, overwrite?",
destination.to_string_lossy()
destination.display()
))
.default(false)
.interact()?
{
return Ok(());
}
{
continue;
}

fs::copy(&cannon, &destination)?;
fs::copy(&entry, &destination)?;

println!(
" {} => {}",
style(&diff.to_string_lossy()).dim(),
style(
diff_paths(
fs::canonicalize(&destination)?,
fs::canonicalize(&server.path)?
println!(
" {} => {}",
style(&diff.to_string_lossy()).dim(),
style(
diff_paths(
fs::canonicalize(&destination)?,
fs::canonicalize(&server.path)?
)
.unwrap_or_default()
.to_string_lossy()
)
.unwrap_or_default()
.to_string_lossy()
)
.dim(),
);
.dim(),
);
}

Ok(())
}
Loading

0 comments on commit 2bdd82e

Please sign in to comment.