Improved error handling and verbosity regarding env vars.

This commit is contained in:
scoobybejesus 2020-10-13 00:27:32 -04:00
parent 059e2b2a90
commit f117b155cf
4 changed files with 38 additions and 56 deletions

View File

@ -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

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cryptools" name = "cryptools"
version = "0.9.0" version = "0.9.1"
authors = ["scoobybejesus <scoobybejesus@users.noreply.github.com>"] authors = ["scoobybejesus <scoobybejesus@users.noreply.github.com>"]
edition = "2018" edition = "2018"
description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'." description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'."

View File

@ -95,22 +95,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::from_args(); let args = Cli::from_args();
let cfg = setup::get_env()?;
println!( println!(
" "\
Hello, Hello!
This software will import your csv file's ledger of cryptocurrency transactions. 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. 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. 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 See .env.example for environment variables that may be set in a .env file in order to
change default program behavior. 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 (input_file_path, settings) = setup::run_setup(args, cfg)?;
let ( let (

View File

@ -1,7 +1,6 @@
// Copyright (c) 2017-2019, scoobybejesus // Copyright (c) 2017-2019, scoobybejesus
// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt
// use std::ffi::OsString;
use std::path::PathBuf; use std::path::PathBuf;
use std::error::Error; use std::error::Error;
use std::process; use std::process;
@ -20,13 +19,14 @@ use crate::wizard;
pub fn get_env() -> Result<super::Cfg, Box<dyn Error>> { pub fn get_env() -> Result<super::Cfg, Box<dyn Error>> {
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") { let iso_date: bool = match env::var("ISO_DATE") {
Ok(val) => { Ok(val) => {
let var_lower = val.to_lowercase(); if val == "1" || val.to_lowercase() == "true" {
let val_str = var_lower.as_str();
if val_str == "1" || val == "true" {
true true
} else { } else {
false false
@ -36,23 +36,37 @@ pub fn get_env() -> Result<super::Cfg, Box<dyn Error>> {
}; };
let date_separator: String = match env::var("DATE_SEPARATOR") { let date_separator: String = match env::var("DATE_SEPARATOR") {
Ok(val) => val.to_lowercase(), Ok(val) => {
Err(_e) => "h".to_string(), 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") { let home_currency = match env::var("HOME_CURRENCY") {
Ok(val) => val.to_uppercase(), Ok(val) => {
Err(_e) => "USD".to_string(), 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") { 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, Err(_e) => None,
}; };
let inv_costing_method = match env::var("INV_COSTING_METHOD") { let inv_costing_method = match env::var("INV_COSTING_METHOD") {
Ok(val) => val, Ok(val) => {
Err(_e) => "1".to_string(), 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 { 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 { let like_kind_cutoff_date = if like_kind_election {
NaiveDate::parse_from_str(&like_kind_cutoff_date_string, "%y-%m-%d") 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") .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() }; } else { NaiveDate::parse_from_str(&"1-1-1", "%y-%m-%d").unwrap() };
let settings = ImportProcessParameters { let settings = ImportProcessParameters {