Skip to content

Commit

Permalink
Add Config that is parseable from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
triarius committed Aug 31, 2024
1 parent 7bb9c5c commit 6dba94d
Show file tree
Hide file tree
Showing 5 changed files with 396 additions and 176 deletions.
151 changes: 150 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ edition = "2021"
build = "build.rs"

[dependencies]
clap = "4.5.13"
clap = { version = "4.5.13", features = ["derive", "env"] }
clap-serde-derive = "0.2.1"
color-eyre = "0.6.3"
env_logger = "0.11.5"
log = "0.4.22"
nom = "7.1.3"
paste = "1.0.15"
serde = "1.0.209"
thiserror = "1.0.63"
toml = "0.8.19"
urlencoding = "2.1.3"
users = "0.11.0"

Expand Down
38 changes: 38 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use clap_serde_derive::ClapSerde;
use color_eyre::Result;
use serde::{Deserialize, Serialize};
use std::{fs, path::PathBuf, time::Duration};

#[allow(clippy::module_name_repetitions)]
#[derive(ClapSerde, Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct Config {
/// Timeout in seconds for requests that show dialogs to the user.
/// E.g. GETPIN, CONFIRM, etc.
#[arg(short, long, value_name = "TIMEOUT_IN_SECONDS", value_parser = parse_duration, default_value = "300")]
pub timeout_in_seconds: Option<Duration>,

/// The command to run when a user input is required.
/// It must print the input to stdout.
#[arg(
short,
long,
value_name = "COMMAND",
value_delimiter = ' ',
num_args = 1..,
default_value = "walker --password",
)]
pub command: Vec<String>,
}

fn parse_duration(s: &str) -> Result<Duration> {
Ok(Duration::from_secs(s.parse::<u64>()?))
}

impl TryFrom<&PathBuf> for Config {
type Error = color_eyre::Report;

fn try_from(path: &PathBuf) -> Result<Self> {
let data = fs::read_to_string(path)?;
toml::from_str(&data).map_err(Into::into)
}
}
Loading

0 comments on commit 6dba94d

Please sign in to comment.