From 132e74056e6702a95389aaf06ab89d0fedea9517 Mon Sep 17 00:00:00 2001 From: Giacomo Bagnoli Date: Tue, 30 Aug 2022 21:14:07 +0100 Subject: [PATCH] Support configuration files using confy --- Cargo.lock | 80 +++++++++++++++++++++++++++++++++++++++++++++++++---- Cargo.toml | 7 +++-- README.md | 11 ++++++-- src/main.rs | 50 +++++++++++++++++++++++++-------- 4 files changed, 127 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17fc2cd..79a7c26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "libc", ] +[[package]] +name = "anyhow" +version = "1.0.62" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1485d4d2cc45e7b201ee3767015c96faa5904387c9d87c6efdd0fb511f12d305" + [[package]] name = "atty" version = "0.2.14" @@ -52,6 +58,12 @@ version = "3.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -134,6 +146,17 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "confy" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2913470204e9e8498a0f31f17f90a0de801ae92c8c5ac18c49af4819e6786697" +dependencies = [ + "directories", + "serde", + "toml", +] + [[package]] name = "core-foundation-sys" version = "0.8.3" @@ -162,16 +185,37 @@ dependencies = [ "memchr", ] +[[package]] +name = "directories" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" +dependencies = [ + "cfg-if 0.1.10", + "dirs-sys", +] + [[package]] name = "dirs-next" version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "dirs-sys-next", ] +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -195,7 +239,7 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.11.0+wasi-snapshot-preview1", ] @@ -278,7 +322,7 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -495,6 +539,20 @@ name = "serde" version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] [[package]] name = "siphasher" @@ -576,14 +634,26 @@ dependencies = [ "winapi", ] +[[package]] +name = "toml" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" +dependencies = [ + "serde", +] + [[package]] name = "tzbuddy" -version = "0.2.0" +version = "0.3.0" dependencies = [ + "anyhow", "chrono", "chrono-tz", "clap", + "confy", "prettytable-rs", + "serde", ] [[package]] @@ -631,7 +701,7 @@ version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7652e3f6c4706c8d9cd54832c4a4ccb9b5336e2c3bd154d5cccfbf1c1f5f7d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] diff --git a/Cargo.toml b/Cargo.toml index 5ae00a1..4a472a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tzbuddy" -version = "0.2.0" +version = "0.3.0" authors = ["Giacomo Bagnoli "] edition = "2018" homepage = "https://github.com/gbagnoli/tzbuddy.rs" @@ -13,7 +13,10 @@ categories = ["command-line-utilities"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -clap = {version= "3", features=["cargo", "derive"]} +anyhow = "^1" chrono = "^0.4" chrono-tz = "^0.6" +clap = {version= "3", features=["cargo", "derive"]} +confy = "^0.4" prettytable-rs = "^0.9" +serde = { version = "1.0", features = ["derive"] } diff --git a/README.md b/README.md index 930b73f..ef1f5f1 100644 --- a/README.md +++ b/README.md @@ -38,8 +38,15 @@ $ brew install tzbuddy Usage ======= -See `tzbuddy --help` for all available options. There is no configuration, so -you probably want to create an alias in your shell. +See `tzbuddy --help` for all available options. + +Prior to version `0.3`, `tzbuddy` did not support configuration files, and you +had to create an alias on the shell. + +Since version `0.3` there is support for configuration. Simply pass `--save` and +`tzbuddy` will remember flags when running without. Eventual options on the +commandline takes precedence. `--no-config` will skip loading the config. +Running `--save` without any other option will save an empty config. Development =========== diff --git a/src/main.rs b/src/main.rs index 6bd734d..4f77514 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ +use anyhow::Result; use chrono::{DateTime, Datelike, Duration, NaiveDateTime, Timelike, Utc}; use chrono_tz::Tz; use clap::Parser; use prettytable::{format, Cell, Row, Table}; +use serde::{Deserialize, Serialize}; use std::cmp::Ordering; use std::collections::HashMap; @@ -12,7 +14,7 @@ struct TimezoneHours { tz: Tz, } -#[derive(Parser, Debug)] +#[derive(Parser, Debug, Serialize, Deserialize, Default)] #[clap(name = clap::crate_name!())] #[clap(author = clap::crate_authors!("\n"))] #[clap(version=clap::crate_version!())] @@ -31,11 +33,17 @@ struct Cli { #[clap(short = 'H', long = "no-header")] noheader: bool, /// How many hours to span - #[clap(short, long, default_value_t = 12)] - span: i32, + #[clap(short, long)] + span: Option, /// Use 12h (am/pm) format #[clap(short, long = "am-pm")] ampm: bool, + /// Save config with current args + #[clap(long)] + save: bool, + /// Do not load config at startup + #[clap(long = "no-config")] + noconfig: bool, ///Calculate times from a specific date (YYYY-mm-dd HH:mm). If omitted, current time is used date: Option, } @@ -207,22 +215,40 @@ fn print_table(tz_hours: Vec, date: DateTime, no_header: boo table.printstd(); } -fn main() { +fn main() -> Result<()> { let cli = Cli::parse(); + if cli.save { + println!("Saving config"); + confy::store("tzbuddy", &cli)?; + } + let config: Cli = if cli.noconfig { + Cli::default() + } else { + confy::load("tzbuddy")? + }; let date = get_utc_date(cli.date); let mut sort_order = SortOrder::East; - if cli.noorder { + if cli.noorder || config.noorder { sort_order = SortOrder::None; - } else if cli.inverseorder { + } else if cli.inverseorder || config.noorder { sort_order = SortOrder::West; } - let tzhours = calculate_timezone_hours( - get_timezones(cli.timezones), + let timezones = match cli.timezones.len() { + 0 => config.timezones, + _ => cli.timezones, + }; + let span = match cli.span { + None => config.span.unwrap_or(12), + Some(span) => span, + }; + let ampm = cli.ampm || config.ampm; + let tzhours = calculate_timezone_hours(get_timezones(timezones), date, span, ampm, sort_order); + print_table( + tzhours, date, - cli.span, - cli.ampm, - sort_order, + cli.noheader || config.noheader, + cli.ampm || config.ampm, ); - print_table(tzhours, date, cli.noheader, cli.ampm); + Ok(()) }