diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-08-19 00:19:38 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-08-19 00:19:38 -0400 |
commit | aa52e632b866b51d455787a96bcd2f5a63b7ac89 (patch) | |
tree | 7e947c62510404f6233be24c303d7fb6f455be0d /src/paths.rs | |
parent | 3539ceb20f5383a332a8ad1fcab816cf083f277e (diff) | |
download | ynab-api-aa52e632b866b51d455787a96bcd2f5a63b7ac89.tar.gz ynab-api-aa52e632b866b51d455787a96bcd2f5a63b7ac89.zip |
move ynab-api to its own repository
Diffstat (limited to 'src/paths.rs')
-rw-r--r-- | src/paths.rs | 56 |
1 files changed, 0 insertions, 56 deletions
diff --git a/src/paths.rs b/src/paths.rs deleted file mode 100644 index f53bef5..0000000 --- a/src/paths.rs +++ /dev/null @@ -1,56 +0,0 @@ -use snafu::{OptionExt, ResultExt}; -use std::io::Read; - -#[derive(Debug, snafu::Snafu)] -pub enum Error { - #[snafu(display("couldn't find config path for project {}", name))] - FindConfigDir { name: String }, - - #[snafu(display( - "couldn't open file {}: {}", - file.to_string_lossy(), - source - ))] - OpenFile { - file: std::path::PathBuf, - source: std::io::Error, - }, - - #[snafu(display( - "couldn't read file {}: {}", - file.to_string_lossy(), - source - ))] - ReadFile { - file: std::path::PathBuf, - source: std::io::Error, - }, -} - -pub type Result<T> = std::result::Result<T, Error>; - -const PROJECT_NAME: &str = "ynab"; - -pub fn api_key() -> Result<std::path::PathBuf> { - Ok(directories::ProjectDirs::from("", "", PROJECT_NAME) - .with_context(|| FindConfigDir { - name: PROJECT_NAME.to_string(), - })? - .config_dir() - .join("api-key")) -} - -pub fn read_api_key() -> Result<String> { - let mut key = String::new(); - let key_file = api_key()?; - std::fs::File::open(key_file.clone()) - .with_context(|| OpenFile { - file: key_file.clone(), - })? - .read_to_string(&mut key) - .with_context(|| ReadFile { - file: key_file.clone(), - })?; - let key = key.trim(); - Ok(key.to_string()) -} |