diff --git a/Cargo.lock b/Cargo.lock index e29cc6a..7c3c3ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1113,10 +1113,10 @@ name = "lact" version = "0.4.3" dependencies = [ "anyhow", - "clap", "lact-cli", "lact-daemon", "lact-gui", + "lact-schema", ] [[package]] @@ -1124,8 +1124,8 @@ name = "lact-cli" version = "0.4.3" dependencies = [ "anyhow", - "clap", "lact-client", + "lact-schema", ] [[package]] @@ -1181,6 +1181,7 @@ name = "lact-schema" version = "0.4.3" dependencies = [ "amdgpu-sysfs", + "clap", "indexmap", "serde", "serde_json", diff --git a/lact-cli/Cargo.toml b/lact-cli/Cargo.toml index 9b3cdb1..e906337 100644 --- a/lact-cli/Cargo.toml +++ b/lact-cli/Cargo.toml @@ -5,5 +5,5 @@ edition = "2021" [dependencies] lact-client = { path = "../lact-client" } +lact-schema = { path = "../lact-schema", features = ["args"] } anyhow = "1.0.71" -clap = { version = "4.2.7", features = ["derive"] } diff --git a/lact-cli/src/args.rs b/lact-cli/src/args.rs deleted file mode 100644 index 6ac7e07..0000000 --- a/lact-cli/src/args.rs +++ /dev/null @@ -1,35 +0,0 @@ -use clap::{Parser, Subcommand}; -use lact_client::DaemonClient; - -#[derive(Parser)] -#[command(author, version, about)] -pub struct CliArgs { - pub gpu_id: Option, - #[command(subcommand)] - pub subcommand: CliCommand, -} - -#[derive(Subcommand)] -pub enum CliCommand { - /// List GPUs - ListGpus, - /// Show GPU info - Info, -} - -impl CliArgs { - pub fn gpu_ids(&self, client: &DaemonClient) -> Vec { - match self.gpu_id { - Some(ref id) => vec![id.clone()], - None => { - let buffer = client.list_devices().expect("Could not list GPUs"); - buffer - .inner() - .expect("Could not deserialize GPUs response") - .into_iter() - .map(|entry| entry.id.to_owned()) - .collect() - } - } - } -} diff --git a/lact-cli/src/lib.rs b/lact-cli/src/lib.rs index 3ff0110..d3caadb 100644 --- a/lact-cli/src/lib.rs +++ b/lact-cli/src/lib.rs @@ -1,8 +1,6 @@ -pub mod args; - use anyhow::{Context, Result}; -use args::{CliArgs, CliCommand}; use lact_client::DaemonClient; +use lact_schema::args::{CliArgs, CliCommand}; pub fn run(args: CliArgs) -> Result<()> { let client = DaemonClient::connect()?; @@ -28,7 +26,7 @@ fn list_gpus(_: &CliArgs, client: &DaemonClient) -> Result<()> { } fn info(args: &CliArgs, client: &DaemonClient) -> Result<()> { - for id in args.gpu_ids(client) { + for id in extract_gpu_ids(args, client) { let info_buffer = client.get_device_info(&id)?; let info = info_buffer.inner()?; let pci_info = info.pci_info.context("GPU reports no pci info")?; @@ -47,3 +45,18 @@ fn info(args: &CliArgs, client: &DaemonClient) -> Result<()> { } Ok(()) } + +fn extract_gpu_ids(args: &CliArgs, client: &DaemonClient) -> Vec { + match args.gpu_id { + Some(ref id) => vec![id.clone()], + None => { + let buffer = client.list_devices().expect("Could not list GPUs"); + buffer + .inner() + .expect("Could not deserialize GPUs response") + .into_iter() + .map(|entry| entry.id.to_owned()) + .collect() + } + } +} diff --git a/lact-gui/src/lib.rs b/lact-gui/src/lib.rs index 1398629..421b91f 100644 --- a/lact-gui/src/lib.rs +++ b/lact-gui/src/lib.rs @@ -1,18 +1,19 @@ mod app; -use anyhow::anyhow; +use anyhow::{anyhow, Context}; use app::App; -use lact_client::DaemonClient; +use lact_client::{schema::args::GuiArgs, DaemonClient}; use std::os::unix::net::UnixStream; use tracing::{error, info, metadata::LevelFilter}; use tracing_subscriber::EnvFilter; const APP_ID: &str = "io.github.lact-linux"; -pub fn run() -> anyhow::Result<()> { +pub fn run(args: GuiArgs) -> anyhow::Result<()> { let env_filter = EnvFilter::builder() .with_default_directive(LevelFilter::INFO.into()) - .from_env_lossy(); + .parse(args.log_level.unwrap_or_default()) + .context("Invalid log level")?; tracing_subscriber::fmt().with_env_filter(env_filter).init(); if let Err(err) = gtk::init() { diff --git a/lact-schema/Cargo.toml b/lact-schema/Cargo.toml index 4722adc..ba607fd 100644 --- a/lact-schema/Cargo.toml +++ b/lact-schema/Cargo.toml @@ -3,10 +3,14 @@ name = "lact-schema" version = "0.4.3" edition = "2021" +[features] +args = ["clap"] + [dependencies] amdgpu-sysfs = { version = "0.11.0", features = ["serde"] } serde = { version = "1.0", features = ["derive"] } indexmap = { version = "*", features = ["serde"] } +clap = { version = "4.2.7", features = ["derive"], optional = true } [dev-dependencies] serde_json = "1.0" diff --git a/lact-schema/src/args.rs b/lact-schema/src/args.rs new file mode 100644 index 0000000..ec91ccf --- /dev/null +++ b/lact-schema/src/args.rs @@ -0,0 +1,42 @@ +pub use clap; + +use clap::{Parser, Subcommand}; + +#[derive(Parser)] +pub struct Args { + #[command(subcommand)] + pub command: Option, +} + +#[derive(Subcommand)] +pub enum Command { + /// Run the daemon + Daemon, + /// Run the GUI + Gui(GuiArgs), + /// Run the CLI + Cli(CliArgs), +} + +#[derive(Default, Parser)] +pub struct GuiArgs { + #[arg(long)] + pub log_level: Option, +} + +#[derive(Parser)] +#[command(author, version, about)] +pub struct CliArgs { + #[arg(short, long)] + pub gpu_id: Option, + #[command(subcommand)] + pub subcommand: CliCommand, +} + +#[derive(Subcommand)] +pub enum CliCommand { + /// List GPUs + ListGpus, + /// Show GPU info + Info, +} diff --git a/lact-schema/src/lib.rs b/lact-schema/src/lib.rs index ee05b91..d9de204 100644 --- a/lact-schema/src/lib.rs +++ b/lact-schema/src/lib.rs @@ -1,3 +1,5 @@ +#[cfg(feature = "args")] +pub mod args; pub mod request; mod response; diff --git a/lact/Cargo.toml b/lact/Cargo.toml index 7e518c7..5ab32d4 100644 --- a/lact/Cargo.toml +++ b/lact/Cargo.toml @@ -9,7 +9,7 @@ drm = ["lact-daemon/drm"] [dependencies] lact-daemon = { path = "../lact-daemon", default-features = false } +lact-schema = { path = "../lact-schema", features = ["args"] } lact-cli = { path = "../lact-cli" } lact-gui = { path = "../lact-gui", optional = true } anyhow = "1.0.71" -clap = { version = "4.2.7", features = ["derive"] } diff --git a/lact/src/args.rs b/lact/src/args.rs deleted file mode 100644 index 336c40e..0000000 --- a/lact/src/args.rs +++ /dev/null @@ -1,17 +0,0 @@ -use clap::{Parser, Subcommand}; -use lact_cli::args::CliArgs; - -#[derive(Parser)] -pub struct Args { - #[command(subcommand)] - pub command: Option, -} - -#[derive(Subcommand)] -pub enum Command { - /// Run the daemon - Daemon, - /// Run the GUI - Gui, - Cli(CliArgs), -} diff --git a/lact/src/main.rs b/lact/src/main.rs index 74ca32d..20d8690 100644 --- a/lact/src/main.rs +++ b/lact/src/main.rs @@ -1,27 +1,25 @@ -mod args; - -use args::{Args, Command}; -use clap::Parser; +use lact_schema::args::{clap::Parser, Args, Command, GuiArgs}; fn main() -> anyhow::Result<()> { let args = Args::parse(); - let command = args.command.unwrap_or(Command::Gui); + let command = args + .command + .unwrap_or_else(|| Command::Gui(GuiArgs::default())); match command { Command::Daemon => lact_daemon::run(), - Command::Gui => run_gui(), + Command::Gui(gui_args) => run_gui(gui_args), Command::Cli(cli_args) => lact_cli::run(cli_args), } } #[cfg(feature = "lact-gui")] -fn run_gui() -> anyhow::Result<()> { - lact_gui::run() +fn run_gui(args: GuiArgs) -> anyhow::Result<()> { + lact_gui::run(args) } #[cfg(not(feature = "lact-gui"))] -fn run_gui() -> anyhow::Result<()> { +fn run_gui(_: GuiArgs) -> anyhow::Result<()> { use anyhow::anyhow; - Err(anyhow!("LACT was built without GUI support")) }