forked from sigil-03/power
code cleanup + add CLI bin
This commit is contained in:
parent
c5866eeccc
commit
230034c7f0
5 changed files with 64 additions and 7 deletions
53
src/bin/cli.rs
Normal file
53
src/bin/cli.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
use clap::{Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct PowerCommand {
|
||||
index: usize,
|
||||
#[command(subcommand)]
|
||||
state: power::types::PowerState,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
Monitor,
|
||||
Set(PowerCommand),
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
pub async fn execute(self, config_file: &str) {
|
||||
let s = power::system::System::new_from_file(config_file).unwrap();
|
||||
|
||||
let handle = match self {
|
||||
Self::Monitor => tokio::spawn(async move {
|
||||
s.try_get_power().await.unwrap();
|
||||
}),
|
||||
Self::Set(command) => {
|
||||
// let c = Controller::new_from_file(config_file).unwrap();
|
||||
tokio::spawn(async move {
|
||||
s.try_set_power(command.index, command.state).await.unwrap();
|
||||
})
|
||||
}
|
||||
};
|
||||
handle.await.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct Cli {
|
||||
#[arg(short, long)]
|
||||
config_file: String,
|
||||
#[command(subcommand)]
|
||||
command: Commands,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub async fn execute(self) {
|
||||
self.command.execute(&self.config_file).await;
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
cli.execute().await;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue