Skip to content

Commit

Permalink
fix: Relative path issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Azuyamat committed Dec 17, 2023
1 parent 5661ea9 commit 03d5a52
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
18 changes: 11 additions & 7 deletions src/cli/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
clippy::complexity
)]

use crate::cli::config_cli;
use crate::cli::constructor::{Args, ServerAction, DJ};
use crate::cli::generator;
use crate::config::Config;
use crate::cli::config_cli;
use crate::error::Error;
use crate::minecraft::jars::load;
use crate::minecraft::server::Server;
Expand Down Expand Up @@ -52,7 +52,7 @@ pub(crate) fn execute(args: Args, mut config: Config, theme: &Theme) -> Result<(
location = read_line("🎚️ Please enter the server location:")?;
path = Path::new(&location);
}
let server = jar.download(&version, &build, &name, &path.to_path_buf())?;
let server = jar.download(&version, &build, &name, path)?;
config.add_server(&server);
}
DJ::Start { name } => {
Expand All @@ -67,12 +67,16 @@ pub(crate) fn execute(args: Args, mut config: Config, theme: &Theme) -> Result<(

// Actions
DJ::Server { action } => handle_server_action(action, &mut config, verbose)?,
DJ::Theme { action } => handle_theme_action(action, verbose)
DJ::Theme { action } => handle_theme_action(action, verbose),
}
Ok(())
}

fn handle_server_action(action: ServerAction, config: &mut Config, verbose: bool) -> Result<(), Error> {
fn handle_server_action(
action: ServerAction,
config: &mut Config,
verbose: bool,
) -> Result<(), Error> {
match action {
ServerAction::List => {
config.print_info()?;
Expand Down Expand Up @@ -145,13 +149,13 @@ fn handle_theme_action(action: ThemeAction, verbose: bool) {
match action {
ThemeAction::List => {
todo!()
},
}
ThemeAction::Create => {
todo!()
},
}
ThemeAction::Delete => {
todo!()
},
}
ThemeAction::Info { name } => {
todo!()
}
Expand Down
53 changes: 34 additions & 19 deletions src/cli/generator.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::path::Path;
use inquire::Select;
use crate::config::Config;
use crate::error::Error;
use crate::minecraft::jars::load;
use crate::utils::read_line;
use inquire::Select;
use std::fs;
use std::path::Path;

pub fn prompt_wizard(config: &mut Config) -> Result<(), Error> {
let jars = load()?;
Expand All @@ -12,12 +13,16 @@ pub fn prompt_wizard(config: &mut Config) -> Result<(), Error> {
loop {
server_name = read_line("🎚️ Please enter the server name:")?;
if config.get_server(&server_name).is_some() {
println!("⚠️ A server with the same name already exists! Please enter a different name:");
println!(
"⚠️ A server with the same name already exists! Please enter a different name:"
);
continue;
}
let length = server_name.len() as u8;
if !(1..=100).contains(&length) {
println!("⚠️ Server name must be within 1 and 100 characters. Please enter a different name:");
println!(
"⚠️ Server name must be within 1 and 100 characters. Please enter a different name:"
);
server_name = read_line("🎚️ Please enter the server name:")?;
}
break;
Expand All @@ -29,38 +34,48 @@ pub fn prompt_wizard(config: &mut Config) -> Result<(), Error> {
.map(|j| j.name.as_str())
.collect::<Vec<&str>>(),
)
.prompt()?;
let jar = jars.get_jar(jar_name).ok_or(Error::ResourceNotFound("Jar not found".to_string()))?;
let version = Select::new(
"🎚️ Please enter the server version",
jar.get_versions()?,
)
.prompt()?;
.prompt()?;
let jar = jars
.get_jar(jar_name)
.ok_or(Error::ResourceNotFound("Jar not found".to_string()))?;
let version = Select::new("🎚️ Please enter the server version", jar.get_versions()?).prompt()?;
let builds = jar.get_builds(&version)?;
let latest = builds.first().ok_or(Error::ResourceNotFound("Jar build not found".to_string()))?;
let latest = builds
.first()
.ok_or(Error::ResourceNotFound("Jar build not found".to_string()))?;
let build = Select::new(
&format!("🎚️ Please enter the jar build ({} is latest)", latest),
builds,
)
.prompt()?
.to_string();
.prompt()?
.to_string();
let mut location = read_line("🎚️ Please enter the server location:")?;
let mut path = Path::new(&location);
if location.is_empty() {
// Set path to location where the command was run
location = std::env::current_dir()?.to_str().unwrap().to_string();
path = Path::new(&location);
println!("🎚️ Using current directory as server location ({}).", location);
println!(
"🎚️ Using current directory as server location ({}).",
location
);
}
while !path.exists() {
println!("🚨 Path does not exist!");
println!("🚨 Path does not exist! Should we create it?");
let should_create = read_line("🎚️ Create dir (Y/N):")?;
if should_create.to_lowercase() == "y" {
fs::create_dir_all(path)?;
break;
}
location = read_line("🎚️ Please enter the server location:")?;
path = Path::new(&location);
}
let server = jar
.download(&version, &build, &server_name, &path.to_path_buf())?;
let full_path = fs::canonicalize(path)?;
let full_path = full_path.to_str().unwrap().trim_start_matches("\\\\?\\");
let full_path = Path::new(full_path);
let server = jar.download(&version, &build, &server_name, full_path)?;
println!("🎛️ Server generated!");
config.add_server(&server);
println!("🎛️ Server generated!");
Ok(())
}
}
13 changes: 4 additions & 9 deletions src/minecraft/jars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::utils::{download, Color};
use serde::Deserialize;
use std::fmt::Display;
use std::fs::File;
use std::path::PathBuf;
use std::path::Path;

const JARS_TOML: &str = include_str!("../../jars.toml");

Expand Down Expand Up @@ -142,7 +142,7 @@ impl Jar {
version: &str,
build: &str,
server_name: &str,
location: &PathBuf,
location: &Path,
) -> Result<Server, Error> {
println!("🗂️ Downloading {}...", colorize(&self.name, Color::Green));
let server: Server;
Expand All @@ -157,12 +157,7 @@ impl Jar {
colorize(&download_url, Color::LightPurple)
);
let response = reqwest::blocking::get(&download_url)?;

let path = std::path::Path::new(location).join(server_name);
if !path.exists() {
std::fs::create_dir_all(&path)?;
}
let mut file = File::create(path.join(format!("{}-{}.jar", self.name, version)))?;
let mut file = File::create(location.join(format!("{}-{}.jar", self.name, version)))?;

download(response, &mut file);

Expand All @@ -171,7 +166,7 @@ impl Jar {
self.name.clone(),
version.to_string(),
build.to_string(),
&path,
location,
);
});
println!(
Expand Down

0 comments on commit 03d5a52

Please sign in to comment.