From 4f6f6d5d6a2ca04eaf525fbfb6f965e9200bae91 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 7 Feb 2025 01:41:37 -0600 Subject: more work on cli --- Cargo.lock | 22 ++++++++++++++++++++++ ozone-cli/Cargo.toml | 1 + ozone-cli/src/main.rs | 31 ++++++++++++++++++++++++++----- ozone/src/launcher/settings.rs | 39 +++++++++++++++++++++++++++++++++++++++ ozone/src/launcher/version.rs | 2 +- 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ea5d97..b818ed7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -826,6 +826,18 @@ dependencies = [ "strsim", ] +[[package]] +name = "clap_complete" +version = "4.5.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "375f9d8255adeeedd51053574fd8d4ba875ea5fa558e86617b07f09f1680c8b6" +dependencies = [ + "clap", + "clap_lex", + "is_executable", + "shlex", +] + [[package]] name = "clap_derive" version = "4.5.24" @@ -2622,6 +2634,15 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +[[package]] +name = "is_executable" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a1b5bad6f9072935961dfbf1cced2f3d129963d091b6f69f007fe04e758ae2" +dependencies = [ + "winapi", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -3515,6 +3536,7 @@ name = "ozone-cli" version = "0.1.0" dependencies = [ "clap", + "clap_complete", "log", "ozone", "simple_logger", diff --git a/ozone-cli/Cargo.toml b/ozone-cli/Cargo.toml index dbbda5d..7f1aad2 100644 --- a/ozone-cli/Cargo.toml +++ b/ozone-cli/Cargo.toml @@ -9,3 +9,4 @@ tokio = { version = "1.43.0", features = ["rt", "rt-multi-thread", "macros"] } simple_logger = { version = "5.0.0", features = ["colors"] } log = "0.4.25" clap = { version = "4.5.27", features = ["derive", "cargo"] } +clap_complete = { version = "4.5.44", features = ["unstable-dynamic"] } diff --git a/ozone-cli/src/main.rs b/ozone-cli/src/main.rs index 4b04e48..d18e9b8 100644 --- a/ozone-cli/src/main.rs +++ b/ozone-cli/src/main.rs @@ -2,7 +2,7 @@ mod cli; use std::error::Error; use std::process::ExitCode; -use log::{error, info}; +use log::{error, info, trace}; use clap::Parser; use ozone::launcher::{Launcher, Settings}; @@ -14,14 +14,32 @@ async fn main_inner(cli: Cli) -> Result> { return Ok(ExitCode::FAILURE); // we print our own error message }; - info!("Sensible home could be {home:?}"); + trace!("Sensible home could be {home:?}"); let mut settings = Settings::load(home.join("ozone.json")).await?; match &cli.subcmd { RootCommand::Profile(p) => match p.command() { ProfileCommand::List => { + let mut first = true; + + if settings.get_profiles().is_empty() { + eprintln!("There are no profiles. Create one with `profile create '."); + return Ok(ExitCode::SUCCESS); + } + for (name, profile) in settings.get_profiles().iter() { - println!("{name}: {profile:#?}"); + 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!)" }; + + println!("Profile {name}:{sel}"); + println!(" Version: {}", profile.get_version()); + println!(" Instance: {}{}", profile.get_instance_name(), exists); } }, ProfileCommand::Create(args) => { @@ -43,7 +61,10 @@ async fn main_inner(cli: Cli) -> Result> { } // creating a new profile from scratch - + todo!() + }, + ProfileCommand::Select(args) => { + } _ => todo!() }, @@ -73,7 +94,7 @@ async fn main_inner(cli: Cli) -> Result> { async fn main() -> ExitCode { simple_logger::SimpleLogger::new().env().init().unwrap(); - let arg = dbg!(Cli::parse()); + let arg = Cli::parse(); main_inner(arg).await.unwrap_or_else(|e| { error!("Launcher initialization error:"); diff --git a/ozone/src/launcher/settings.rs b/ozone/src/launcher/settings.rs index ca0bc33..c9f2dd5 100644 --- a/ozone/src/launcher/settings.rs +++ b/ozone/src/launcher/settings.rs @@ -22,6 +22,9 @@ pub struct SettingsData { pub profiles: HashMap, pub instances: HashMap, + selected_profile: Option, + selected_instance: Option, + #[serde(default = "uuid::Uuid::new_v4")] pub client_id: Uuid } @@ -31,6 +34,14 @@ impl SettingsData { if let Some((name, profile)) = self.profiles.iter().find(|(name, profile)| !self.instances.contains_key(&profile.instance)) { return Err(format!("profile {} refers to instance {}, which does not exist.", name, &profile.instance)); } + + if self.selected_profile.as_ref().is_some_and(|p| !self.profiles.contains_key(p)) { + return Err(format!("selected profile {} does not exist", self.selected_profile.as_ref().unwrap())); + } + + if self.selected_instance.as_ref().is_some_and(|p| !self.instances.contains_key(p)) { + return Err(format!("selected instance {} does not exist", self.selected_instance.as_ref().unwrap())); + } Ok(()) } @@ -73,6 +84,8 @@ impl Default for SettingsData { SettingsData { instances: [(String::from(constants::DEF_INSTANCE_NAME), PathBuf::from(constants::DEF_INSTANCE_NAME).into())].into_iter().collect(), profiles: [(String::from(constants::DEF_PROFILE_NAME), Profile::new(constants::DEF_INSTANCE_NAME))].into_iter().collect(), + selected_profile: Some(String::from(constants::DEF_PROFILE_NAME)), + selected_instance: Some(String::from(constants::DEF_INSTANCE_NAME)), client_id: Uuid::new_v4() } } @@ -153,6 +166,22 @@ impl Settings { pub fn get_client_id(&self) -> Uuid { self.inner.client_id } + + pub fn get_selected_instance_name(&self) -> Option<&str> { + self.inner.selected_instance.as_deref() + } + + pub fn get_selected_instance(&self) -> Option<&Instance> { + self.inner.selected_instance.as_ref().and_then(|s| self.inner.instances.get(s)) + } + + pub fn get_selected_profile_name(&self) -> Option<&str> { + self.inner.selected_profile.as_deref() + } + + pub fn get_selected_profile(&self) -> Option<&Profile> { + self.inner.selected_profile.as_ref().and_then(|s| self.inner.profiles.get(s)) + } } #[derive(Deserialize, Serialize, Debug, Clone)] @@ -169,6 +198,16 @@ pub enum ProfileVersion { Specific(String) } +impl Display for ProfileVersion { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ProfileVersion::LatestSnapshot => f.write_str("Latest Snapshot"), + ProfileVersion::LatestRelease => f.write_str("Latest Release"), + ProfileVersion::Specific(version) => f.write_str(version.as_str()) + } + } +} + #[derive(Deserialize, Serialize, Debug, Clone, Copy)] pub struct Resolution { pub width: u32, diff --git a/ozone/src/launcher/version.rs b/ozone/src/launcher/version.rs index 705d4d5..c7268db 100644 --- a/ozone/src/launcher/version.rs +++ b/ozone/src/launcher/version.rs @@ -165,7 +165,7 @@ impl RemoteVersionList { } async fn new(home: impl AsRef) -> Result { - let manifest: VersionManifest = Self::load_manifest(&home)?; + let manifest: VersionManifest = Self::load_manifest(&home).await?; let mut versions = HashMap::new(); for v in manifest.versions { -- cgit v1.2.3-70-g09d2