From f117b155cfa2dde8dca9979286c0ef79836f963d Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Tue, 13 Oct 2020 00:27:32 -0400 Subject: [PATCH] Improved error handling and verbosity regarding env vars. --- .env.example | 32 -------------------------------- Cargo.toml | 2 +- src/main.rs | 20 ++++++++++---------- src/setup.rs | 40 +++++++++++++++++++++++++++------------- 4 files changed, 38 insertions(+), 56 deletions(-) delete mode 100644 .env.example diff --git a/.env.example b/.env.example deleted file mode 100644 index 68c7215..0000000 --- a/.env.example +++ /dev/null @@ -1,32 +0,0 @@ -## CONFIGURATION -## -## If the defaults below are not suitable, copy this .env.example into a new .env file, -## uncomment the respective enviroment variable, and set the value according to your needs. - -# Setting to `TRUE` or `1` will cause the program to expect the `txDate` field in the `file_to_import` to use -# the format YYYY-MM-dd or YY-MM-dd (or YYYY/MM/dd or YY/MM/dd, depending on the date-separator option) -# instead of the default US-style MM-dd-YYYY or MM-dd-YY (or MM/dd/YYYY or MM/dd/YY, depending on the -# date separator option). -# (bool; default is FALSE/0) -#ISO_DATE=0 - -# Choose "h", "s", or "p" for hyphen, slash, or period (i.e., "-", "/", or ".") to indicate the separator -# character used in the `file_to_import` `txDate` column (i.e. 2017/12/31, 2017-12-31, or 2017.12.31). -# (String; default is 'h') -#DATE_SEPARATOR=h - -# Home currency (currency in which all resulting reports are denominated). -# (String; default is 'USD') -#HOME_CURRENCY=USD - -# Cutoff date through which like-kind exchange treatment should be applied. -# Please use %y-%m-%d (or %Y-%m-%d) format for like-kind cutoff date entry. -# (Optional; default is not set) -#LK_CUTOFF_DATE=YYYY-mm-DD - -#1. LIFO according to the order the lot was created. -#2. LIFO according to the basis date of the lot. -#3. FIFO according to the order the lot was created. -#4. FIFO according to the basis date of the lot. -# (String: default is '1') -#INV_COSTING_METHOD=1 \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index cb0ddf6..80f7712 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptools" -version = "0.9.0" +version = "0.9.1" authors = ["scoobybejesus "] edition = "2018" description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'." diff --git a/src/main.rs b/src/main.rs index 10c81aa..3a7b382 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,22 +95,22 @@ fn main() -> Result<(), Box> { let args = Cli::from_args(); - let cfg = setup::get_env()?; - println!( - " - Hello, + "\ +Hello! - This software will import your csv file's ledger of cryptocurrency transactions. - It will then process it by creating 'lots' and posting 'movements' to those lots. - Along the way, it will keep track of income, expenses, gains, and losses. +This software will import your csv file's ledger of cryptocurrency transactions. +It will then process it by creating 'lots' and posting 'movements' to those lots. +Along the way, it will keep track of income, expenses, gains, and losses. - See .env.example for environment variables that may be set in a .env file in order to - change default program behavior. +See .env.example for environment variables that may be set in a .env file in order to +change default program behavior. - Note: The software is designed to import a full history. Gains and losses may be incorrect otherwise. + Note: The software is designed to import a full history. Gains and losses may be incorrect otherwise. "); + let cfg = setup::get_env()?; + let (input_file_path, settings) = setup::run_setup(args, cfg)?; let ( diff --git a/src/setup.rs b/src/setup.rs index 9ade040..52e619e 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -1,7 +1,6 @@ // Copyright (c) 2017-2019, scoobybejesus // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt -// use std::ffi::OsString; use std::path::PathBuf; use std::error::Error; use std::process; @@ -20,13 +19,14 @@ use crate::wizard; pub fn get_env() -> Result> { - dotenv::dotenv().expect("Failed to read .env file"); + match dotenv::dotenv() { + Ok(_path) => {println!("Setting environment variables from .env file.")}, + Err(_e) => println!("Did not find .env file.") + } let iso_date: bool = match env::var("ISO_DATE") { Ok(val) => { - let var_lower = val.to_lowercase(); - let val_str = var_lower.as_str(); - if val_str == "1" || val == "true" { + if val == "1" || val.to_lowercase() == "true" { true } else { false @@ -36,23 +36,37 @@ pub fn get_env() -> Result> { }; let date_separator: String = match env::var("DATE_SEPARATOR") { - Ok(val) => val.to_lowercase(), - Err(_e) => "h".to_string(), + Ok(val) => { + println!(" Found DATE_SEPARATOR env var: {}", val); + val.to_lowercase()}, + Err(_e) => { + println!(" Using default date separator (hyphen)."); + "h".to_string()}, }; let home_currency = match env::var("HOME_CURRENCY") { - Ok(val) => val.to_uppercase(), - Err(_e) => "USD".to_string(), + Ok(val) => { + println!(" Found HOME_CURRENCY env var: {}", val); + val.to_uppercase()}, + Err(_e) => { + println!(" Using default home currency (USD)."); + "USD".to_string()}, }; let lk_cutoff_date = match env::var("LK_CUTOFF_DATE") { - Ok(val) => Some(val), + Ok(val) => { + println!(" Found LK_CUTOFF_DATE env var: {}", val); + Some(val)}, Err(_e) => None, }; let inv_costing_method = match env::var("INV_COSTING_METHOD") { - Ok(val) => val, - Err(_e) => "1".to_string(), + Ok(val) => { + println!(" Found INV_COSTING_METHOD env var: {}", val); + val}, + Err(_e) => { + println!(" Using default inventory costing method (LIFO by lot creation date)."); + "1".to_string()}, }; let cfg = super::Cfg { @@ -106,7 +120,7 @@ pub (crate) fn run_setup(cmd_args: super::Cli, cfg: super::Cfg) -> Result<(PathB let like_kind_cutoff_date = if like_kind_election { NaiveDate::parse_from_str(&like_kind_cutoff_date_string, "%y-%m-%d") .unwrap_or_else(|_| NaiveDate::parse_from_str(&like_kind_cutoff_date_string, "%Y-%m-%d") - .expect("Command line date (like-kind cutoff option) has an incorrect format. Program must abort.")) + .expect("Environment variable for LK_CUTOFF_DATE has an incorrect format. Program must abort. See .env.example.")) } else { NaiveDate::parse_from_str(&"1-1-1", "%y-%m-%d").unwrap() }; let settings = ImportProcessParameters {