Skip to content

Commit

Permalink
Add basic command-line handling
Browse files Browse the repository at this point in the history
  • Loading branch information
aklajnert committed Oct 2, 2020
1 parent a175bd6 commit fba7976
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ idasen = { git = "https://github.com/aklajnert/idasen.git" }
toml = "0.5.6"
serde = { version = "1.0", features = ["derive"] }
shellexpand = "2.0.0"
failure = "0.1.8"
failure = "0.1.8"
clap = "3.0.0-beta.2"
4 changes: 2 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const CONFIG_FILE_NAME: &'static str = "desk.toml";

#[derive(Serialize, Deserialize, Debug, Default)]
pub struct ConfigData {
pub min_height: Option<i16>,
pub max_height: Option<i16>,
pub position_down: Option<i16>,
pub position_up: Option<i16>,
}

#[derive(Debug)]
Expand Down
38 changes: 31 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,43 @@
mod config;

use crate::config::Config;
use clap::{App, Arg};
use failure;
use idasen::Idasen;
use std::thread;
use std::time::Duration;
use std::{process, thread};

pub fn main() -> Result<(), failure::Error> {
let mut config = Config::new()?;
config.save();
println!("{:?}", config);
let desk = Idasen::new()?;
let matches = App::new("Desk")
.version("0.1.0")
.about("Control the IDASEN desk position via bluetooth.")
.subcommand(App::new("up").about("Move desk up"))
.subcommand(App::new("down").about("Move desk down"))
.subcommand(App::new("save-up").about("Save current position as up"))
.subcommand(App::new("save-down").about("Save current position as down"))
.subcommand(App::new("info").about("Display desk information"))
.get_matches();

println!("Desk addr: {}", desk.mac_addr);
println!("Position: {}", desk.position()?);
if let Some(subcommand) = matches.subcommand() {
let config = Config::new().expect("Failed to load configuration.");
let subcommand = subcommand.0;
if subcommand == "up" && config.data.position_up.is_none() {
eprintln!(
"Position `up` is not defined. \
Please set desk manually to desired position and run `save-up` command."
);
process::exit(1);
} else if subcommand == "down" && config.data.position_down.is_none() {
eprintln!(
"Position `down` is not defined. \
Please set desk manually to desired position and run `save-down` command."
);
process::exit(1);
}
} else {
eprintln!("Please select subcommand. Use `help` to see available subcommands.");
process::exit(1);
}

Ok(())
}

0 comments on commit fba7976

Please sign in to comment.