diff options
Diffstat (limited to 'ozone-cli/src/main.rs')
| -rw-r--r-- | ozone-cli/src/main.rs | 130 |
1 files changed, 82 insertions, 48 deletions
diff --git a/ozone-cli/src/main.rs b/ozone-cli/src/main.rs index e0cbada..dc4ca4c 100644 --- a/ozone-cli/src/main.rs +++ b/ozone-cli/src/main.rs @@ -4,9 +4,10 @@ use std::error::Error; use std::process::ExitCode; use log::{error, info, trace, LevelFilter}; use clap::Parser; -use ozone::launcher::{Instance, Launcher, Profile, Settings}; +use ozone::launcher::{Instance, Launcher, Settings}; use ozone::launcher::version::{VersionList, VersionResult}; -use crate::cli::{Cli, ProfileCommand, RootCommand}; +use uuid::Uuid; +use crate::cli::{Cli, InstanceCommand, RootCommand}; async fn main_inner(cli: Cli) -> Result<ExitCode, Box<dyn Error>> { let Some(home) = cli.home.or_else(Launcher::sensible_home) else { @@ -18,83 +19,116 @@ async fn main_inner(cli: Cli) -> Result<ExitCode, Box<dyn Error>> { let mut settings = Settings::load(home.join("ozone.json")).await?; match &cli.subcmd { - RootCommand::Profile(p) => match p.command() { - ProfileCommand::List => { + RootCommand::Instance(p) => match p.command() { + InstanceCommand::List => { let mut first = true; - if settings.get_profiles().is_empty() { - eprintln!("There are no profiles. Create one with `profile create <name>'."); + if settings.instances.is_empty() { + eprintln!("There are no instances. Create one with `profile create <name>'."); return Ok(ExitCode::SUCCESS); } - for (name, profile) in settings.get_profiles().iter() { + for (cur_id, instance) in settings.instances.iter() { if !first { println!(); } first = false; - let sel = if settings.get_selected_profile_name().is_some_and(|n| n == name) { " (selected)" } else { "" }; - let exists = if settings.get_instances().contains_key(profile.get_instance_name()) { "" } else { " (missing!)" }; + let cur_id = *cur_id; + let sel = if settings.selected_instance.is_some_and(|id| id == cur_id) { " (selected)" } else { "" }; - println!("Profile {name}:{sel}"); - println!(" Version: {}", profile.game_version); - println!(" Instance: {}{}", profile.get_instance_name(), exists); + println!("Instance `{}'{sel}:", instance.name); + println!(" Id: {}", cur_id); + println!(" Version: {}", instance.game_version); + println!(" Location: {}", home.join(Settings::get_instance_path(cur_id)).display()); } }, - ProfileCommand::Create(args) => { - if settings.profiles.contains_key(&args.name) { - eprintln!("A profile with that name already exists."); + InstanceCommand::Create(args) => { + if args.name.is_empty() { + eprintln!("The instance must not have an empty name."); return Ok(ExitCode::FAILURE); } - if let Err(e) = Profile::check_name(&args.name) { - eprintln!("The profile name is invalid: {e}"); - return Ok(ExitCode::FAILURE); - } - - let mut profile = if let Some(ref src) = args.clone { - if let Some(profile) = settings.get_profiles().get(src) { - profile.clone() + let mut inst = if args.clone { + if let Some(selected_inst) = settings.selected_instance.and_then(|i| settings.instances.get(&i)) { + let mut inst = selected_inst.clone(); + inst.name.replace_range(.., &args.name); + inst } else { - eprintln!("Unknown profile `{src}'."); + eprintln!("You do not have an instance selected."); return Ok(ExitCode::FAILURE); } } else { - let inst_name = args.instance.as_ref().unwrap_or(&args.name); - if let Err(e) = Instance::check_name(inst_name) { - eprintln!("The profile name is invalid for an instance: {e}"); - eprintln!("Please specify an instance name manually with --instance."); + Instance::new(&args.name) + }; + + if let Some(ref ver_name) = args.settings.version { + // FIXME: don't hardcode "versions" path + let versions = VersionList::new(home.join("versions"), !cli.offline).await?; + if matches!(versions.get_version_lazy(ver_name), VersionResult::None) { + eprintln!("The version `{}' could not be found.", ver_name); return Ok(ExitCode::FAILURE); } + } + + args.settings.apply_to(&mut inst); + + let new_id = Uuid::new_v4(); + settings.instances.insert(new_id, inst); + + if !args.no_select { + settings.selected_instance = Some(new_id); + } - Profile::new(inst_name) - }; - - // creating a new profile from scratch - todo!() + settings.save().await?; }, - ProfileCommand::Select(args) => { - let ver = VersionList::new(home.join("versions"), !cli.offline).await?; - - match ver.get_version_lazy(&args.profile) { - VersionResult::None => { - println!("Unknown version"); - }, - VersionResult::Remote(v) => { - println!("Remote version: {v:?}"); - }, - VersionResult::Complete(v) => { - println!("Complete version: {v:?}"); + InstanceCommand::Select(args) => { + if let Ok(uuid) = args.instance.parse::<Uuid>() { + if settings.instances.get(&uuid).is_none() { + eprintln!("No instances were found by that UUID."); + return Ok(ExitCode::FAILURE); } + + settings.selected_instance = Some(uuid); + settings.save().await?; + + return Ok(ExitCode::SUCCESS); } + + let search_norm = args.instance.to_lowercase(); + + let found: Vec<_> = settings.instances.iter() + .filter(|(_, inst)| { + // FIXME: find a better way of doing this matching + inst.name.to_lowercase().starts_with(&search_norm) + }).collect(); + + if found.is_empty() { + eprintln!("No instances were found."); + return Ok(ExitCode::FAILURE); + } + + if found.len() > 1 { + eprintln!("Ambiguous argument. Found {} instances:", found.len()); + for (id, inst) in found { + eprintln!("- {} ({id})", inst.name); + } + + return Ok(ExitCode::FAILURE); + } + + let (found_id, found_inst) = found.first().unwrap(); + println!("Selected instance {} ({found_id}).", found_inst.name); + + settings.selected_instance = Some(**found_id); + settings.save().await?; } - _ => todo!() }, RootCommand::Launch => { settings.save().await?; - let launcher = Launcher::new(&home, !cli.offline).await?; +/* let launcher = Launcher::new(&home, !cli.offline).await?; let profile = settings.get_profiles().get("default").unwrap(); let launch = launcher.prepare_launch(profile, settings.get_instances().get(profile.get_instance_name()).unwrap(), settings.get_client_id()).await.map_err(|e| { @@ -105,7 +139,7 @@ async fn main_inner(cli: Cli) -> Result<ExitCode, Box<dyn Error>> { dbg!(&launch); info!("ok"); - ozone::launcher::run_the_game(&launch)?; + ozone::launcher::run_the_game(&launch)?;*/ } _ => todo!() } |
