Skip to content

Commit

Permalink
chore: code review feedback
Browse files Browse the repository at this point in the history
Signed-off-by: morrieinmaas <[email protected]>
  • Loading branch information
morrieinmaas committed Mar 3, 2022
1 parent 185ef89 commit ba140b3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
28 changes: 16 additions & 12 deletions cli/src/modules/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::path::Path;
use std::path::PathBuf;
use std::{fmt, fs};

use clap::Args;
Expand Down Expand Up @@ -40,23 +41,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())
Expand Down Expand Up @@ -93,3 +85,15 @@ fn initialise(path: &Path) -> Result<()> {

Ok(())
}

pub fn get_config_path() -> Result<PathBuf> {
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 = env!("HOME");
Ok(Path::new(home).join(".config/aries-cli/config.ini"))
} else {
Err(error::Error::OsUnknown.into())
}
}
23 changes: 7 additions & 16 deletions cli/src/register.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -65,22 +65,13 @@ fn initialise_agent_from_cli(
endpoint: Option<String>,
api_key: Option<String>,
) -> Result<CloudAgentPython> {
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<dyn std::error::Error>);
let endpoint_from_config: Result<String> =
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 {
Expand Down

0 comments on commit ba140b3

Please sign in to comment.