From c7996c1aca47610678116242c980b58b14aa4a2f Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Mon, 28 Feb 2022 16:35:09 +0100 Subject: [PATCH 1/8] feat: WIP (untested) Add config path for windows OS Signed-off-by: morrieinmaas --- cli/src/modules/configuration.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index 6b619ac9..011a1f83 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -1,5 +1,6 @@ +use std::env; use std::path::Path; -use std::{fs, fmt}; +use std::{fmt, fs}; use clap::Args; @@ -24,15 +25,31 @@ struct ConfigurationEnvironment { impl fmt::Display for ConfigurationEnvironment { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[{}]\nendpoint={}{}", self.environment, self.endpoint, self.api_key.as_ref().map(|val| format!("\napi_key={val}")).unwrap_or_else(|| "".to_string())) + write!( + f, + "[{}]\nendpoint={}{}", + self.environment, + self.endpoint, + self.api_key + .as_ref() + .map(|val| format!("\napi_key={val}")) + .unwrap_or_else(|| "".to_string()) + ) } } - // TODO: we should implement `from` so we can use todo and have a cleaner api pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { - let home = env!("HOME"); - let default_config_path = Path::new(home).join(".config/aries-cli/config.ini"); + let default_config_path; + if cfg!(windows) { + let home = "C:\\Program Files\\Common Files\\".to_string(); + let default_config_path_win = Path::new(&home).join(".config\\aries-cli\\config.ini"); + default_config_path = default_config_path_win + } else { + let home = env!("HOME"); + let default_config_path_unix = Path::new(home).join(".config/aries-cli/config.ini"); + default_config_path = default_config_path_unix + } if options.initialise { initialise(&default_config_path)?; logger.log("Initialised the configuration!"); @@ -50,11 +67,10 @@ fn view(path: &Path, logger: Log) -> Result<()> { let output = fs::read_to_string(path)?; logger.log(output); Ok(()) - } fn initialise(path: &Path) -> Result<()> { - let config = ConfigurationEnvironment{ + let config = ConfigurationEnvironment { environment: "Default".to_string(), endpoint: "https://agent.community.animo.id".to_string(), api_key: None, @@ -72,7 +88,7 @@ fn initialise(path: &Path) -> Result<()> { // Create the configuration file fs::File::create(&path)?; - + // Write the default configuration to the file fs::write(path, config.to_string())?; From a8a3b1aa944b53cc49930a11b57835a9cfb2a89d Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Mon, 28 Feb 2022 17:45:48 +0100 Subject: [PATCH 2/8] chore: code review feedback Signed-off-by: morrieinmaas --- cli/src/modules/configuration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index 011a1f83..acff2074 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -42,8 +42,8 @@ impl fmt::Display for ConfigurationEnvironment { pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { let default_config_path; if cfg!(windows) { - let home = "C:\\Program Files\\Common Files\\".to_string(); - let default_config_path_win = Path::new(&home).join(".config\\aries-cli\\config.ini"); + let home = "%AppData%"; + let default_config_path_win = Path::new(home).join("aries-cli/config.ini"); default_config_path = default_config_path_win } else { let home = env!("HOME"); From 35ba804302b2f2ab015d34e292831d6e74d8d81e Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Tue, 1 Mar 2022 13:49:21 +0100 Subject: [PATCH 3/8] chore: code review feedback Signed-off-by: morrieinmaas --- cli/src/modules/configuration.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index acff2074..fc147e70 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -40,16 +40,13 @@ impl fmt::Display for ConfigurationEnvironment { // TODO: we should implement `from` so we can use todo and have a cleaner api pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { - let default_config_path; - if cfg!(windows) { + let default_config_path = if cfg!(windows) { let home = "%AppData%"; - let default_config_path_win = Path::new(home).join("aries-cli/config.ini"); - default_config_path = default_config_path_win + Path::new(home).join("aries-cli/config.ini") } else { let home = env!("HOME"); - let default_config_path_unix = Path::new(home).join(".config/aries-cli/config.ini"); - default_config_path = default_config_path_unix - } + Path::new(home).join(".config/aries-cli/config.ini") + }; if options.initialise { initialise(&default_config_path)?; logger.log("Initialised the configuration!"); From d857be101bac1c10470c35942d52d59b8e22a0bd Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Wed, 2 Mar 2022 22:02:43 +0100 Subject: [PATCH 4/8] feat: better check on attribute length Signed-off-by: morrieinmaas --- cli/src/modules/schema.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/modules/schema.rs b/cli/src/modules/schema.rs index 62c313ed..8eb4141f 100644 --- a/cli/src/modules/schema.rs +++ b/cli/src/modules/schema.rs @@ -62,7 +62,7 @@ pub async fn parse_schema_args( version: version.to_string(), attributes: attributes.to_vec(), }; - if options.attributes.len() == 0 { + if options.attributes.is_empty() { return Err(Error::RequiredAttributes.into()); } agent From 987dd63fb893ab9de2df81e14b0735b0d0a831ef Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Thu, 3 Mar 2022 09:32:12 +0100 Subject: [PATCH 5/8] feat: add windows config file logic This works with WSL and Powershell Signed-off-by: morrieinmaas --- cli/src/modules/configuration.rs | 34 ++++++++++++++++++++++---------- cli/src/register.rs | 14 ++++++++++--- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index 03454128..40fd9127 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -1,5 +1,5 @@ use std::path::Path; -use std::{fs, fmt}; +use std::{fmt, fs}; use clap::Args; @@ -24,20 +24,36 @@ struct ConfigurationEnvironment { impl fmt::Display for ConfigurationEnvironment { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "[{}]\nendpoint={}{}", self.environment, self.endpoint, self.api_key.as_ref().map(|val| format!("\napi_key={}", val)).unwrap_or_else(|| "".to_string())) + write!( + f, + "[{}]\nendpoint={}{}", + self.environment, + self.endpoint, + self.api_key + .as_ref() + .map(|val| format!("\napi_key={}", val)) + .unwrap_or_else(|| "".to_string()) + ) } } - // TODO: we should implement `from` so we can use todo and have a cleaner api pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { - let home = env!("HOME"); - let default_config_path = Path::new(home).join(".config/aries-cli/config.ini"); + let default_config_path = if cfg!(windows) { + let home = "C:\\Program Files\\Common Files"; + Path::new(home).join("aries-cli\\config.ini") + } else if cfg!(unix) { + let home = env!("HOME"); + Path::new(home).join(".config/aries-cli/config.ini") + } else { + let home = "."; + Path::new(home).join(".config/aries-cli/config.ini") + }; if options.initialize { initialise(&default_config_path)?; logger.log("Initialised the configuration!"); return Ok(()); - } + } if options.view { return view(&default_config_path, logger); } @@ -49,11 +65,10 @@ fn view(path: &Path, logger: Log) -> Result<()> { let output = fs::read_to_string(path)?; logger.log(output); Ok(()) - } fn initialise(path: &Path) -> Result<()> { - let config = ConfigurationEnvironment{ + let config = ConfigurationEnvironment { environment: "Default".to_string(), endpoint: "https://agent.community.animo.id".to_string(), api_key: None, @@ -71,10 +86,9 @@ fn initialise(path: &Path) -> Result<()> { // Create the configuration file fs::File::create(&path)?; - + // Write the default configuration to the file fs::write(path, config.to_string())?; Ok(()) } - diff --git a/cli/src/register.rs b/cli/src/register.rs index fe73fa80..cb686870 100644 --- a/cli/src/register.rs +++ b/cli/src/register.rs @@ -65,8 +65,16 @@ fn initialise_agent_from_cli( endpoint: Option, api_key: Option, ) -> Result { - let home = env!("HOME"); - let default_config_path = Path::new(home).join(".config/aries-cli/config.ini"); + let default_config_path = if cfg!(windows) { + let home = "C:\\Program Files\\Common Files"; + Path::new(home).join("aries-cli\\config.ini") + } else if cfg!(unix) { + let home = env!("HOME"); + Path::new(home).join(".config/aries-cli/config.ini") + } else { + let home = "."; + Path::new(home).join(".config/aries-cli/config.ini") + }; let config_path = config.unwrap_or(default_config_path); let environment = environment; @@ -77,7 +85,7 @@ fn initialise_agent_from_cli( let endpoint = match endpoint { Some(e) => e, - None => endpoint_from_config? + None => endpoint_from_config?, }; let api_key = api_key.or_else(|| api_key_from_config.ok()); From 0aff0bd0acefe971728c11c3d0c99158ff7bd5e0 Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Thu, 3 Mar 2022 09:38:19 +0100 Subject: [PATCH 6/8] format Signed-off-by: morrieinmaas --- cli/src/modules/configuration.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index ae0d83ca..1f15ae2d 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -25,7 +25,6 @@ struct ConfigurationEnvironment { impl fmt::Display for ConfigurationEnvironment { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { -<<<<<<< HEAD write!( f, "[{}]\nendpoint={}{}", @@ -36,9 +35,6 @@ impl fmt::Display for ConfigurationEnvironment { .map(|val| format!("\napi_key={val}")) .unwrap_or_else(|| "".to_string()) ) -======= - write!(f, "[{}]\nendpoint={}{}", self.environment, self.endpoint, self.api_key.as_ref().map(|val| format!("\napi_key={}", val)).unwrap_or_else(|| "".to_string())) ->>>>>>> main } } @@ -55,7 +51,7 @@ pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Lo initialise(&default_config_path)?; logger.log("Initialised the configuration!"); return Ok(()); - } + } if options.view { return view(&default_config_path, logger); } @@ -94,4 +90,3 @@ fn initialise(path: &Path) -> Result<()> { Ok(()) } - From 185ef891bd665b5a57fe7b26fdd274b2b058ee46 Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Thu, 3 Mar 2022 10:17:15 +0100 Subject: [PATCH 7/8] Code review feedback Only create path for unix and windows. Throw and error if neither OS type can be detected. Signed-off-by: morrieinmaas --- cli/src/error.rs | 2 ++ cli/src/modules/configuration.rs | 10 +++++----- cli/src/register.rs | 10 +++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cli/src/error.rs b/cli/src/error.rs index 3ae3d73b..04b816c7 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -8,6 +8,7 @@ pub enum Error { NoConfigKey, UnqualAmountKeyValue, ConfigExists, + OsUnknown, RequiredAttributes, NoSubcommandSupplied(String), NoFlagSupplied(String), @@ -26,6 +27,7 @@ impl Display for Error { Error::NoConfigKey => write!(f, "Required key does not exist in the configuration file."), Error::UnqualAmountKeyValue => write!(f, "Supplies keys and values are not equal in size."), Error::ConfigExists => write!(f, "Configuration file already exists."), + Error::OsUnknown => write!(f, "Unknown operating system. Failed to detect OS as windows or unix."), Error::NoSubcommandSupplied(subcommand) => write!(f, "No subcommand supplied for {}. Check `aries-cli {} --help for the available options.", subcommand, subcommand), Error::NoFlagSupplied(subcommand) => write!(f, "The subcommand {} requires atleast one flag. Check `aries-cli {} --help for the available options.", subcommand, subcommand), Error::RequiredAttributes => write!(f, "Creating a schema requires at least one attribute. Please supply them via the --attributes flag."), diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index 158c4109..d55caa12 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -40,15 +40,15 @@ impl fmt::Display for ConfigurationEnvironment { // TODO: we should implement `from` so we can use todo and have a cleaner api pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { - let default_config_path = if cfg!(windows) { + let default_config_path; + if cfg!(windows) { let home = "C:\\Program Files\\Common Files"; - Path::new(home).join("aries-cli\\config.ini") + default_config_path = Path::new(home).join("aries-cli\\config.ini") } else if cfg!(unix) { let home = env!("HOME"); - Path::new(home).join(".config/aries-cli/config.ini") + default_config_path = Path::new(home).join(".config/aries-cli/config.ini") } else { - let home = "."; - Path::new(home).join(".config/aries-cli/config.ini") + return Err(error::Error::OsUnknown.into()); }; if options.initialize { initialise(&default_config_path)?; diff --git a/cli/src/register.rs b/cli/src/register.rs index cb686870..3029f502 100644 --- a/cli/src/register.rs +++ b/cli/src/register.rs @@ -65,15 +65,15 @@ fn initialise_agent_from_cli( endpoint: Option, api_key: Option, ) -> Result { - let default_config_path = if cfg!(windows) { + let default_config_path; + if cfg!(windows) { let home = "C:\\Program Files\\Common Files"; - Path::new(home).join("aries-cli\\config.ini") + default_config_path = Path::new(home).join("aries-cli\\config.ini") } else if cfg!(unix) { let home = env!("HOME"); - Path::new(home).join(".config/aries-cli/config.ini") + default_config_path = Path::new(home).join(".config/aries-cli/config.ini") } else { - let home = "."; - Path::new(home).join(".config/aries-cli/config.ini") + return Err(error::Error::OsUnknown.into()); }; let config_path = config.unwrap_or(default_config_path); let environment = environment; From c882d7c370a545986b02ad5e67549260217afaa3 Mon Sep 17 00:00:00 2001 From: morrieinmaas Date: Thu, 3 Mar 2022 12:14:42 +0100 Subject: [PATCH 8/8] chore: code review feedback Signed-off-by: morrieinmaas --- cli/src/error.rs | 2 ++ cli/src/modules/configuration.rs | 30 ++++++++++++++++-------------- cli/src/register.rs | 23 +++++++---------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/cli/src/error.rs b/cli/src/error.rs index 04b816c7..816a3e90 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -7,6 +7,7 @@ pub enum Error { NoEndpointSupplied, NoConfigKey, UnqualAmountKeyValue, + HomeNotFoundError, ConfigExists, OsUnknown, RequiredAttributes, @@ -26,6 +27,7 @@ impl Display for Error { Error::NoEndpointSupplied => write!(f, "No endpoint supplied. Supply an endpoint either via `--endpoint` or via `--config`."), Error::NoConfigKey => write!(f, "Required key does not exist in the configuration file."), Error::UnqualAmountKeyValue => write!(f, "Supplies keys and values are not equal in size."), + Error::HomeNotFoundError => write!(f, "Unable to find home directory."), Error::ConfigExists => write!(f, "Configuration file already exists."), Error::OsUnknown => write!(f, "Unknown operating system. Failed to detect OS as windows or unix."), Error::NoSubcommandSupplied(subcommand) => write!(f, "No subcommand supplied for {}. Check `aries-cli {} --help for the available options.", subcommand, subcommand), diff --git a/cli/src/modules/configuration.rs b/cli/src/modules/configuration.rs index d55caa12..ebb484d1 100644 --- a/cli/src/modules/configuration.rs +++ b/cli/src/modules/configuration.rs @@ -1,5 +1,4 @@ -use std::env; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::{fmt, fs}; use clap::Args; @@ -40,23 +39,14 @@ impl fmt::Display for ConfigurationEnvironment { // TODO: we should implement `from` so we can use todo and have a cleaner api pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> { - let default_config_path; - if cfg!(windows) { - let home = "C:\\Program Files\\Common Files"; - default_config_path = Path::new(home).join("aries-cli\\config.ini") - } else if cfg!(unix) { - let home = env!("HOME"); - default_config_path = Path::new(home).join(".config/aries-cli/config.ini") - } else { - return Err(error::Error::OsUnknown.into()); - }; + let config_path = get_config_path()?; if options.initialize { - initialise(&default_config_path)?; + initialise(&config_path)?; logger.log("Initialised the configuration!"); return Ok(()); } if options.view { - return view(&default_config_path, logger); + return view(&config_path, logger); } Err(error::Error::NoFlagSupplied("configuration".to_string()).into()) @@ -93,3 +83,15 @@ fn initialise(path: &Path) -> Result<()> { Ok(()) } + +pub fn get_config_path() -> Result { + if cfg!(windows) { + let home = "C:\\Program Files\\Common Files"; + Ok(Path::new(home).join("aries-cli\\config.ini")) + } else if cfg!(unix) { + let home = option_env!("HOME").ok_or_else(|| error::Error::HomeNotFoundError); + Ok(Path::new(&home?).join(".config/aries-cli/config.ini")) + } else { + Err(error::Error::OsUnknown.into()) + } +} diff --git a/cli/src/register.rs b/cli/src/register.rs index 3029f502..50ce0062 100644 --- a/cli/src/register.rs +++ b/cli/src/register.rs @@ -1,8 +1,8 @@ -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use crate::cli::{Cli, Commands}; use crate::error::{self, Result}; -use crate::modules::configuration::parse_configuration_args; +use crate::modules::configuration::{get_config_path, parse_configuration_args}; use crate::modules::credential_definition::parse_credential_definition_args; use crate::modules::credentials::parse_credentials_args; use crate::modules::message::parse_message_args; @@ -65,22 +65,13 @@ fn initialise_agent_from_cli( endpoint: Option, api_key: Option, ) -> Result { - let default_config_path; - if cfg!(windows) { - let home = "C:\\Program Files\\Common Files"; - default_config_path = Path::new(home).join("aries-cli\\config.ini") - } else if cfg!(unix) { - let home = env!("HOME"); - default_config_path = Path::new(home).join(".config/aries-cli/config.ini") - } else { - return Err(error::Error::OsUnknown.into()); - }; - let config_path = config.unwrap_or(default_config_path); + let config_path = get_config_path()?; + let config_path = config.unwrap_or(config_path); let environment = environment; - // We cannot infer type of error here with `.into()` as we are async - let endpoint_from_config = get_value_from_config(&config_path, &environment, "endpoint") - .map_err(|_| Box::new(error::Error::NoEndpointSupplied) as Box); + let endpoint_from_config: Result = + get_value_from_config(&config_path, &environment, "endpoint") + .map_err(|_| error::Error::NoEndpointSupplied.into()); let api_key_from_config = get_value_from_config(&config_path, &environment, "api_key"); let endpoint = match endpoint {