Skip to content

Commit

Permalink
feat: improved logging (#83)
Browse files Browse the repository at this point in the history
Co-authored-by: Jean-Louis Leysens <[email protected]>
  • Loading branch information
jl-animo and jloleysens authored Mar 5, 2022
1 parent ab13dae commit 6557e47
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 149 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ serde = {version = "1.0.130", features = ["derive"]}
serde_json = "1.0.68"
serde_yaml = "0.8.23"
tokio = {version = "1", features = ["full"]}
simplelog = "0.11.2"
log = "0.4.14"
2 changes: 1 addition & 1 deletion cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Display for Error {
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."),
Error::InvalidConfigurationStructure => write!(f, "Invalid configuration structure. Please make sure you have a valid configuration file."),

}
}
}
10 changes: 8 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
extern crate log;

mod cli;
mod error;
mod modules;
mod register;
mod utils;
mod help_strings;

use log::error;
use register::register;
use utils::logger::Log;
use colored::*;

#[tokio::main]
async fn main() {
match register().await {
Ok(_) => (),
Err(e) => Log::default().error(e),
Err(e) => {
error!("{} {}", "Error:".red(), e);
std::process::exit(1);
}
}
}
15 changes: 8 additions & 7 deletions cli/src/modules/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::path::Path;
use std::fs;
use std::path::Path;

use clap::Args;
use log::info;

use crate::error;
use crate::error::Result;
use crate::utils::config::{get_config_path, Configuration};
use crate::utils::logger::Log;
use colored::*;

#[derive(Args)]
pub struct ConfigurationOptions {
Expand All @@ -17,23 +18,23 @@ pub struct ConfigurationOptions {
view: bool,
}

pub async fn parse_configuration_args(options: &ConfigurationOptions, logger: Log) -> Result<()> {
pub async fn parse_configuration_args(options: &ConfigurationOptions) -> Result<()> {
let config_path = get_config_path()?;
if options.initialize {
initialise(&config_path)?;
logger.log("Initialised the configuration!");
info!("{} the configuration", "Initialised".cyan());
return Ok(());
}
if options.view {
return view(&config_path, logger);
return view(&config_path);
}

Err(error::Error::NoFlagSupplied("configuration".to_string()).into())
}

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

Expand Down
21 changes: 14 additions & 7 deletions cli/src/modules/connections.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use agent_controller::modules::connections::{ConnectionCreateInvitationOptions, ConnectionModule};
use clap::{Args, Subcommand};
use colored::*;
use log::info;

use crate::error::{Error, Result};
use crate::utils::{logger::Log, qr::print_qr_code};
use crate::utils::{
logger::{copy_to_clipboard, pretty_print_obj},
qr::print_qr_code,
};

#[derive(Args)]
pub struct ConnectionOptions {
Expand Down Expand Up @@ -37,19 +41,19 @@ pub enum ConnectionSubcommands {
pub async fn parse_connection_args(
options: &ConnectionOptions,
agent: impl ConnectionModule,
logger: Log,
copy: bool,
) -> Result<()> {
if let Some(id) = &options.id {
return agent
.get_connection_by_id(id.to_string())
.await
.map(|connection| logger.log_pretty(connection));
.map(|connection| pretty_print_obj(connection));
}
if options.all {
return agent
.get_connections()
.await
.map(|connections| logger.log_pretty(connections.results));
.map(|connections| pretty_print_obj(connections.results));
}
match &options
.commands
Expand All @@ -72,11 +76,14 @@ pub async fn parse_connection_args(
};
agent.create_invitation(options).await.map(|response| {
if *qr {
logger.log(format!("{}: {}", "Connection id".green(), response.0));
info!("{}", format!("{}: {}", "Connection id".green(), response.0));
print_qr_code(response.1).unwrap();
} else {
logger.log(format!("{}: {}", "Connection id".green(), response.0));
logger.log(response.1);
info!("{}", format!("{}: {}", "Connection id".green(), response.0));
info!("{}", response.1);
if copy {
copy_to_clipboard(response.1);
}
}
})
}
Expand Down
46 changes: 26 additions & 20 deletions cli/src/modules/credential_definition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use agent_controller::modules::credential_definition::CredentialDefinitionModule;
use clap::{Args, Subcommand};
use serde_json::json;
use log::{debug, info};

use crate::{error::{Result, Error}, utils::logger::Log};
use crate::{
error::{Error, Result},
utils::logger::{pretty_stringify_obj},
};

#[derive(Args)]
pub struct CredentialDefinitionOptions {
Expand All @@ -13,7 +17,7 @@ pub struct CredentialDefinitionOptions {
pub id: Option<String>,

#[clap(long, short)]
pub all: bool,
pub all: bool,
}

#[derive(Subcommand, Debug)]
Expand All @@ -27,33 +31,35 @@ pub enum CredentialDefinitionSubcommands {
pub async fn parse_credential_definition_args(
options: &CredentialDefinitionOptions,
agent: impl CredentialDefinitionModule,
logger: Log,
) -> Result<()> {
if let Some(id) = &options.id {
return agent.get_by_id(id.to_string()).await.map(|cred_def| {
if logger.debug {
logger.log_pretty(cred_def)
} else {
let loggable = json!({
"id": cred_def.credential_definition.id,
"schema_id": cred_def.credential_definition.schema_id,
"type": cred_def.credential_definition.type_field,
"tag": cred_def.credential_definition.tag,
"ver": cred_def.credential_definition.ver,
});
logger.log_pretty(loggable);
}
let loggable = json!({
"id": cred_def.credential_definition.id,
"schema_id": cred_def.credential_definition.schema_id,
"type": cred_def.credential_definition.type_field,
"tag": cred_def.credential_definition.tag,
"ver": cred_def.credential_definition.ver,
});
debug!("{}", pretty_stringify_obj(loggable));
});
}
if options.all {
return agent.get_all()
.await
.map(|cred_defs| logger.log_list(cred_defs.credential_definition_ids));
return agent.get_all().await.map(|cred_defs| {
cred_defs
.credential_definition_ids
.iter()
.for_each(|x| info!("{}", x))
});
}
match options.commands.as_ref().ok_or_else(|| Error::NoSubcommandSupplied("credential-definition".to_string()))? {
match options
.commands
.as_ref()
.ok_or_else(|| Error::NoSubcommandSupplied("credential-definition".to_string()))?
{
CredentialDefinitionSubcommands::Create { schema_id } => agent
.create(schema_id.to_string())
.await
.map(|cred_def| logger.log(cred_def.sent.credential_definition_id)),
.map(|cred_def| info!("{}", cred_def.sent.credential_definition_id)),
}
}
12 changes: 5 additions & 7 deletions cli/src/modules/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use agent_controller::modules::credentials::{CredentialsModule, CredentialsOfferOptions};
use clap::{Args, Subcommand};
use log::{debug, info};

use crate::error::{Error, Result};
use crate::utils::logger::Log;
use crate::utils::logger::pretty_stringify_obj;
use colored::*;

#[derive(Args)]
pub struct CredentialOptions {
Expand Down Expand Up @@ -31,7 +33,6 @@ pub enum CredentialSubcommands {
pub async fn parse_credentials_args(
commands: &CredentialSubcommands,
agent: impl CredentialsModule,
logger: Log,
) -> Result<()> {
match commands {
CredentialSubcommands::Offer {
Expand All @@ -51,11 +52,8 @@ pub async fn parse_credentials_args(
values: value.iter().map(|v| v.to_string()).collect(),
};
agent.send_offer(options).await.map(|res| {
if logger.debug {
logger.log_pretty(res)
} else {
logger.log("Successfully offered a credential!");
}
debug!("{}", pretty_stringify_obj(res));
info!("{} offered a credential", "Sucessefully".green());
})
}
CredentialSubcommands::Propose { id: _id } => todo!(),
Expand Down
19 changes: 12 additions & 7 deletions cli/src/modules/features.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use agent_controller::modules::features::FeaturesModule;
use clap::Args;
use log::{debug, info};

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

#[derive(Args)]
pub struct FeaturesOptions {}

pub async fn parse_features_args(agent: impl FeaturesModule, logger: Log) -> Result<()> {
pub async fn parse_features_args(agent: impl FeaturesModule) -> Result<()> {
agent.discover_features().await.map(|features| {
if logger.debug {
logger.log_pretty(features)
} else {
logger.log_list(features.results.keys().collect());
}
debug!("{}", pretty_stringify_obj(&features));
features
.results
.keys()
.collect::<Vec<_>>()
.iter()
.for_each(|x| {
info!("{}", x);
});
})
}
Loading

0 comments on commit 6557e47

Please sign in to comment.