feat: standardise cli arguments, configure gui log level through cli args

This commit is contained in:
Ilya Zlobintsev 2023-06-01 19:19:25 +03:00
parent dd6b53f2fe
commit 7836ca76fe
11 changed files with 83 additions and 74 deletions

5
Cargo.lock generated
View File

@ -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",

View File

@ -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"] }

View File

@ -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<String>,
#[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<String> {
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()
}
}
}
}

View File

@ -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<String> {
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()
}
}
}

View File

@ -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() {

View File

@ -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"

42
lact-schema/src/args.rs Normal file
View File

@ -0,0 +1,42 @@
pub use clap;
use clap::{Parser, Subcommand};
#[derive(Parser)]
pub struct Args {
#[command(subcommand)]
pub command: Option<Command>,
}
#[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<String>,
}
#[derive(Parser)]
#[command(author, version, about)]
pub struct CliArgs {
#[arg(short, long)]
pub gpu_id: Option<String>,
#[command(subcommand)]
pub subcommand: CliCommand,
}
#[derive(Subcommand)]
pub enum CliCommand {
/// List GPUs
ListGpus,
/// Show GPU info
Info,
}

View File

@ -1,3 +1,5 @@
#[cfg(feature = "args")]
pub mod args;
pub mod request;
mod response;

View File

@ -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"] }

View File

@ -1,17 +0,0 @@
use clap::{Parser, Subcommand};
use lact_cli::args::CliArgs;
#[derive(Parser)]
pub struct Args {
#[command(subcommand)]
pub command: Option<Command>,
}
#[derive(Subcommand)]
pub enum Command {
/// Run the daemon
Daemon,
/// Run the GUI
Gui,
Cli(CliArgs),
}

View File

@ -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"))
}