Skip to content

Commit

Permalink
feat: configuration (#67)
Browse files Browse the repository at this point in the history
* feat: initialise and view the config

Signed-off-by: Berend Sliedrecht <[email protected]>

* style: removed whitespace

Signed-off-by: Berend Sliedrecht <[email protected]>

* fix: resolved feedback

Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Feb 26, 2022
1 parent 3d6b844 commit 232d417
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
4 changes: 4 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub enum Error {
NoEndpointSupplied,
NoConfigKey,
UnqualAmountKeyValue,
ConfigExists,
UnreachableCode,
}

impl std::error::Error for Error {}
Expand All @@ -21,6 +23,8 @@ 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::ConfigExists => write!(f, "Configuration file already exists."),
Error::UnreachableCode => write!(f, "Unreachable code detected! Please report this issue with the command that caused it."),
}
}
}
65 changes: 61 additions & 4 deletions cli/src/modules/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,80 @@
use std::path::Path;
use std::{fs, fmt};

use clap::Args;

use crate::error;
use crate::error::Result;
use crate::utils::logger::Log;

#[derive(Args)]
pub struct ConfigurationOptions {
#[clap(short, long)]
#[clap(short, long, conflicts_with = "view")]
initialise: bool,

#[clap(short, long, conflicts_with = "initialise")]
view: bool,
}

struct ConfigurationEnvironment {
environment: String,
endpoint: String,
api_key: Option<String>,
}

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()))
}
}


// 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<()> {
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");
if options.initialise {
initialise()?
initialise(&default_config_path)?;
logger.log("Initialised the configuration!");
return Ok(());
}
if options.view {
view(&default_config_path, logger)?;
return Ok(());
}

Err(error::Error::UnreachableCode.into())
}

fn view(path: &Path, logger: Log) -> Result<()> {
let output = fs::read_to_string(path)?;
logger.log(output);
Ok(())

}

fn initialise() -> Result<()> {
fn initialise(path: &Path) -> Result<()> {
let config = ConfigurationEnvironment{
environment: "Default".to_string(),
endpoint: "https://agent.community.animo.id".to_string(),
api_key: None,
};

if path.exists() {
return Err(error::Error::ConfigExists.into());
}

// Get the directories
let prefix = path.parent().unwrap();

// create all the required directories
fs::create_dir_all(prefix)?;

// Create the configuration file
fs::File::create(&path)?;

// Write the default configuration to the file
fs::write(path, config.to_string())?;

Ok(())
}

0 comments on commit 232d417

Please sign in to comment.