integrate cli into the main executable

This commit is contained in:
Ilya Zlobintsev
2023-01-30 20:23:56 +02:00
parent 1b57bfefc1
commit 688fccb582
6 changed files with 16 additions and 13 deletions

1
Cargo.lock generated
View File

@@ -723,6 +723,7 @@ version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"lact-cli",
"lact-daemon",
"lact-gui",
]

View File

@@ -3,21 +3,21 @@ use lact_client::DaemonClient;
#[derive(Parser)]
#[command(author, version, about)]
pub struct Args {
pub struct CliArgs {
pub gpu_id: Option<String>,
#[command(subcommand)]
pub subcommand: Command,
pub subcommand: CliCommand,
}
#[derive(Subcommand)]
pub enum Command {
pub enum CliCommand {
/// List GPUs
ListGpus,
/// Show GPU info
Info,
}
impl Args {
impl CliArgs {
pub fn gpu_ids(&self, client: &DaemonClient) -> Vec<String> {
match self.gpu_id {
Some(ref id) => vec![id.clone()],

View File

@@ -1,22 +1,20 @@
mod args;
pub mod args;
use anyhow::{Context, Result};
use args::{Args, Command};
use clap::Parser;
use args::{CliArgs, CliCommand};
use lact_client::DaemonClient;
fn main() -> Result<()> {
let args = Args::parse();
pub fn run(args: CliArgs) -> Result<()> {
let client = DaemonClient::connect()?;
let f = match args.subcommand {
Command::ListGpus => list_gpus,
Command::Info => info,
CliCommand::ListGpus => list_gpus,
CliCommand::Info => info,
};
f(&args, &client)
}
fn list_gpus(_: &Args, client: &DaemonClient) -> Result<()> {
fn list_gpus(_: &CliArgs, client: &DaemonClient) -> Result<()> {
let buffer = client.list_devices()?;
for entry in buffer.inner()? {
let id = entry.id;
@@ -29,7 +27,7 @@ fn list_gpus(_: &Args, client: &DaemonClient) -> Result<()> {
Ok(())
}
fn info(args: &Args, client: &DaemonClient) -> Result<()> {
fn info(args: &CliArgs, client: &DaemonClient) -> Result<()> {
for id in args.gpu_ids(client) {
let info_buffer = client.get_device_info(&id)?;
let info = info_buffer.inner()?;

View File

@@ -8,6 +8,7 @@ default = ["lact-gui"]
[dependencies]
lact-daemon = { path = "../lact-daemon" }
lact-cli = { path = "../lact-cli" }
lact-gui = { path = "../lact-gui", optional = true }
anyhow = "1.0.68"
clap = { version = "4.1.4", features = ["derive"] }

View File

@@ -1,4 +1,5 @@
use clap::{Parser, Subcommand};
use lact_cli::args::CliArgs;
#[derive(Parser)]
pub struct Args {
@@ -12,4 +13,5 @@ pub enum Command {
Daemon,
/// Run the GUI
Gui,
Cli(CliArgs),
}

View File

@@ -10,6 +10,7 @@ fn main() -> anyhow::Result<()> {
match command {
Command::Daemon => lact_daemon::run(),
Command::Gui => run_gui(),
Command::Cli(cli_args) => lact_cli::run(cli_args),
}
}