Skip to content

Commit

Permalink
clippy and fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jul 27, 2023
1 parent a750826 commit 2f69f9b
Show file tree
Hide file tree
Showing 16 changed files with 390 additions and 365 deletions.
40 changes: 22 additions & 18 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::{path::PathBuf, time::Instant};
use anyhow::{anyhow, Context, Result};
use clap::{arg, value_parser, ArgMatches, Command};
use console::style;
use tokio::fs::{File, self};
use tokio::fs::{self, File};

use crate::{create_http_client, downloadable::Downloadable, model::Server, util};

pub mod serverjar;
pub mod addons;
pub mod worlds;
pub mod bootstrap;
pub mod scripts;
pub mod serverjar;
pub mod worlds;

pub fn cli() -> Command {
Command::new("build")
Expand Down Expand Up @@ -139,8 +139,10 @@ impl BuildContext {
folder_path: Option<&str>,
report_back: F,
) -> Result<String> {
let file_name = dl.get_filename(&self.server, &self.http_client)
.await.with_context(|| format!("Getting filename of Downloadable: {dl:#?}"))?;
let file_name = dl
.get_filename(&self.server, &self.http_client)
.await
.with_context(|| format!("Getting filename of Downloadable: {dl:#?}"))?;

let file_path = if let Some(path) = folder_path {
self.output_dir.join(path)
Expand All @@ -154,21 +156,23 @@ impl BuildContext {
} else {
report_back(ReportBackState::Downloading, &file_name);

let file = File::create(&file_path).await.with_context(|| format!(
"Failed to create output file: {}",
file_path.to_string_lossy()
))?;
let file = File::create(&file_path).await.with_context(|| {
format!(
"Failed to create output file: {}",
file_path.to_string_lossy()
)
})?;

let result = util::download_with_progress(
file,
&file_name,
dl,
Some(&file_name),
&self.server,
&self.http_client,
)
.await
.with_context(|| format!("Downloading Downloadable: {dl:#?}"));
file,
&file_name,
dl,
Some(&file_name),
&self.server,
&self.http_client,
)
.await
.with_context(|| format!("Downloading Downloadable: {dl:#?}"));

if result.is_err() {
// try to remove file if errored
Expand Down
44 changes: 20 additions & 24 deletions src/commands/build/worlds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,27 @@ impl BuildContext {

for (idx, dp) in world.datapacks.iter().enumerate() {
let path = format!("{name}/datapacks");
self.downloadable(
dp,
Some(&path),
|state, file_name| {
match state {
ReportBackState::Skipped => {
println!(
" {:pad_len$}({:dp_len$}/{datapack_count}) Skipping : {}",
idx + 1,
"",
style(file_name).dim()
);
}
ReportBackState::Downloaded => {
println!(
" {:pad_len$}({:dp_len$}/{datapack_count}) {} : {}",
idx + 1,
"",
style("Downloaded").green().bold(),
style(file_name).dim()
);
}
ReportBackState::Downloading => {}
self.downloadable(dp, Some(&path), |state, file_name| match state {
ReportBackState::Skipped => {
println!(
" {:pad_len$}({:dp_len$}/{datapack_count}) Skipping : {}",
idx + 1,
"",
style(file_name).dim()
);
}
}).await?;
ReportBackState::Downloaded => {
println!(
" {:pad_len$}({:dp_len$}/{datapack_count}) {} : {}",
idx + 1,
"",
style("Downloaded").green().bold(),
style(file_name).dim()
);
}
ReportBackState::Downloading => {}
})
.await?;
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/commands/eject.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::fs;

use anyhow::{Result, Context};
use anyhow::{Context, Result};
use clap::Command;
use console::style;
use dialoguer::{Input, theme::ColorfulTheme};
use dialoguer::{theme::ColorfulTheme, Input};

use crate::model::Server;

Expand All @@ -13,19 +13,20 @@ pub fn cli() -> Command {
.about("Eject - remove everything related to mcman")
}

pub async fn run() -> Result<()> {
#[allow(unused_must_use)]
pub fn run() -> Result<()> {
let server = Server::load().context("Failed to load server.toml")?;

if Input::with_theme(&ColorfulTheme::default())
.with_prompt("Are you sure you want to delete everything? This is irreversible. Type this server's name to confirm.")
.default(String::new())
.interact_text()? == server.name {
println!(" > {}", style("Deleting server.toml...").yellow());
_ = fs::remove_file(server.path.join("server.toml"))?;
fs::remove_file(server.path.join("server.toml"))?;
println!(" > {}", style("Deleting config/...").yellow());
_ = fs::remove_dir_all(server.path.join("config"));
fs::remove_dir_all(server.path.join("config"));
println!(" > {}", style("Deleting server/...").yellow());
_ = fs::remove_dir_all(server.path.join("server"))?;
fs::remove_dir_all(server.path.join("server"))?;
println!(" > Ejected successfully.");
} else {
println!(" > {}", style("Cancelled").green().bold());
Expand Down
27 changes: 15 additions & 12 deletions src/commands/export/mrpack.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::{arg, ArgMatches, Command, value_parser};
use clap::{arg, value_parser, ArgMatches, Command};

use crate::{create_http_client, model::Server, util::mrpack::export_mrpack};

Expand All @@ -10,8 +10,8 @@ pub fn cli() -> Command {
.about("Export as an mrpack")
.arg(
arg!([filename] "Export as filename")
.value_parser(value_parser!(PathBuf))
.required(false)
.value_parser(value_parser!(PathBuf))
.required(false),
)
.arg(arg!(-v --version <version> "Set the version ID of the mrpack"))
}
Expand All @@ -20,11 +20,13 @@ pub async fn run(matches: &ArgMatches) -> Result<()> {
let server = Server::load().context("Failed to load server.toml")?;
let http_client = create_http_client()?;

let s = server.name.clone().replace(|c: char| !c.is_alphanumeric(), "");
let s = server
.name
.clone()
.replace(|c: char| !c.is_alphanumeric(), "");

let default_output = PathBuf::from(
if s.is_empty() { "server".to_owned() } else { s } + ".mrpack"
);
let default_output =
PathBuf::from(if s.is_empty() { "server".to_owned() } else { s } + ".mrpack");

let output_filename = matches
.get_one::<PathBuf>("filename")
Expand All @@ -39,16 +41,17 @@ pub async fn run(matches: &ArgMatches) -> Result<()> {

let version_id = matches.get_one::<String>("version");

let output_file = std::fs::File::create(output_filename)
.context("Creating mrpack output file")?;
let output_file =
std::fs::File::create(output_filename).context("Creating mrpack output file")?;

export_mrpack(
&http_client,
&server,
None,
version_id.unwrap_or(&String::new()),
output_file
).await?;
output_file,
)
.await?;

Ok(())
}
}
24 changes: 15 additions & 9 deletions src/commands/export/packwiz.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::{arg, ArgMatches, Command, value_parser};
use clap::{arg, value_parser, ArgMatches, Command};

use crate::{create_http_client, model::Server, util::packwiz::{export_packwiz, PackwizExportOptions}};
use crate::{
create_http_client,
model::Server,
util::packwiz::{export_packwiz, PackwizExportOptions},
};

pub fn cli() -> Command {
Command::new("packwiz")
Expand All @@ -13,9 +17,7 @@ pub fn cli() -> Command {
arg!(-o --output [FILE] "The output directory for the packwiz files")
.value_parser(value_parser!(PathBuf)),
)
.arg(
arg!(--cfcdn "Use edge.forgecdn.net instead of metadata:curseforge"),
)
.arg(arg!(--cfcdn "Use edge.forgecdn.net instead of metadata:curseforge"))
}

pub async fn run(matches: &ArgMatches) -> Result<()> {
Expand All @@ -30,9 +32,13 @@ pub async fn run(matches: &ArgMatches) -> Result<()> {

let cf_usecdn = matches.get_flag("cfcdn");

export_packwiz(&output_dir, &http_client, &server, &PackwizExportOptions {
cf_usecdn
}).await?;
export_packwiz(
&output_dir,
&http_client,
&server,
&PackwizExportOptions { cf_usecdn },
)
.await?;

Ok(())
}
}
4 changes: 2 additions & 2 deletions src/commands/import/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use anyhow::Result;
use clap::{ArgMatches, Command};

mod url;
mod customs;
mod datapack;
mod mrpack;
mod packwiz;
mod customs;
mod url;

pub fn cli() -> Command {
Command::new("import")
Expand Down
Loading

0 comments on commit 2f69f9b

Please sign in to comment.