Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verbosity levels #102

Merged
merged 6 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions agent/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ impl Display for Error {
match self {
Error::AuthorizationFailed => write!(f, "Failed to authorize. Either the wrong or no api-key is provided."),
Error::UnableToParseResponse => write!(f, "Unable to parse the response from the server. Is the cloudagent the correct version?"),
Error::UrlDoesNotExist => write!(f, "Path does not exist on endpoint. This can happen when querying by id and the id is not valid."),
Error::UnknownResponseStatusCode(msg) => write!(f, "Received unknown status code from the server. Endpoint is likely incorrect. If the endpoint is correct, please report this error at https://github.com/animo/aries-cli/issues/new \nAdditional info: {}", msg),
Error::UrlDoesNotExist => write!(f, "Path does not exist on agent URL. This can happen when querying by id and the id is not valid."),
Error::UnknownResponseStatusCode(msg) => write!(f, "Received unknown status code from the server. Agent URL is likely incorrect. If the agent URL is correct, please report this error at https://github.com/animo/aries-cli/issues/new \nAdditional info: {}", msg),
Error::InternalServerError(status) => write!(f, "Internal Server Error (status code: {})!", status),
Error::UnreachableUrl => write!(f, "Provided url is unreachable. Is the provided endpoint valid?"),
Error::UnreachableUrl => write!(f, "Provided url is unreachable. Is the provided agent URL valid?"),
Error::HttpServiceUnavailable => write!(f, "Cloudagent is currently unavailable. Are you sure the agent is online?")

}
Expand Down
12 changes: 6 additions & 6 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ use crate::modules::{
#[clap(name = "aries-cli", author, version, about = HelpStrings::Cli)]
#[clap(arg_required_else_help = true)]
pub struct Cli {
#[clap(long, short, help = HelpStrings::Endpoint)]
pub endpoint: Option<String>,
#[clap(long, short='u', help = HelpStrings::AgentURL)]
pub agent_url: Option<String>,

#[clap(long, short, help = HelpStrings::ApiKey)]
pub api_key: Option<String>,

#[clap(long, short, help = HelpStrings::Copy)]
pub copy: bool,

#[clap(long, short, help = HelpStrings::Quiet, conflicts_with = "raw")]
#[clap(long, short, help = HelpStrings::Quiet, conflicts_with = "verbose")]
pub quiet: bool,

#[clap(long, short = 'o', help = HelpStrings::Config)]
pub config: Option<PathBuf>,

#[clap(long, short = 'v', default_value = "default", help = HelpStrings::Environment)]
#[clap(long, short = 'e', default_value = "default", help = HelpStrings::Environment)]
Copy link
Contributor Author

@jl-animo jl-animo Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically short = 'e' is not needed

pub environment: String,

#[clap(long, short, help = HelpStrings::Verbose, conflicts_with = "quiet")]
pub raw: bool,
#[clap(long, short='v', help = HelpStrings::Verbose, parse(from_occurrences), conflicts_with = "quiet")]
pub verbose: usize,

#[clap(subcommand)]
pub commands: Commands,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub enum Error {
InvalidConfigurationPath,
InvalidConfigurationStructure,
InvalidEnvironment,
NoEndpointSupplied,
NoAgentURLSupplied,
UnqualAmountKeyValue,
HomeNotFound,
ConfigExists,
Expand All @@ -25,7 +25,7 @@ impl Display for Error {
Error::CannotReadConfigurationFile => write!(f, "Cannot not read configuration file. Try initializing first using: `aries-cli configuration initialize`."),
Error::InvalidConfigurationPath => write!(f, "Invalid configuration path."),
Error::InvalidEnvironment => write!(f, "Invalid environment."),
Error::NoEndpointSupplied => write!(f, "No endpoint supplied. Supply an endpoint either via `--endpoint` or via `--config`."),
Error::NoAgentURLSupplied => write!(f, "No agent URL supplied. Supply an agent URL either via `--agent-url` or see `aries-cli configuration --help` to learn about environments."),
Error::UnqualAmountKeyValue => write!(f, "Supplies keys and values are not equal in size."),
Error::HomeNotFound => write!(f, "Unable to find home directory."),
Error::ConfigExists => write!(f, "Configuration file already exists."),
Expand Down
32 changes: 19 additions & 13 deletions cli/src/help_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use std::convert::From;

pub enum HelpStrings {
Cli,
Endpoint,
AgentURL,
ApiKey,
Copy,
Quiet,
Verbose,
Config,
Environment,
ConfigurationInitialize,
ConfigurationView,
}

impl From<HelpStrings> for Option<&str> {
Expand All @@ -23,37 +25,41 @@ impl HelpStrings {
fn as_str(&self) -> &'static str {
match self {
HelpStrings::Cli => HELP_STRING_CLI,
HelpStrings::Endpoint => "The Aries agent endpoint requests are sent to",
HelpStrings::ApiKey => "This API key will be passed to the agent.",
HelpStrings::Copy => "Copy output to your clipboard.",
HelpStrings::Quiet => "Suppresses most output.",
HelpStrings::Verbose => "Print debug logs.",
HelpStrings::Config => "Path to your configuration file.",
HelpStrings::Environment => "Specify your current environment.",
HelpStrings::AgentURL => "The Aries agent URL that requests will be sent to",
HelpStrings::ApiKey => "This API key will be passed to the agent",
HelpStrings::Copy => "Copy output to your clipboard",
HelpStrings::Quiet => "Suppresses most output",
HelpStrings::Verbose => "Print debug logs",
HelpStrings::Config => "Path to your configuration file",
HelpStrings::Environment => "Specify your current environment",
HelpStrings::ConfigurationInitialize => {
"Initialize a new configuration file with a default environment"
}
HelpStrings::ConfigurationView => "Print your current configuration file",
// TODO: Add docs for all subcommands, e.g., ConnectionsAll.
}
}
}

const HELP_STRING_CLI: &str = "
--- Aries cli ---
--- Aries cli ---

To begin working with the aries-cli, run the following command:

$ aries-cli configuration initialize

This command will initialize the configuration file and makes sure
that you do not have to pass the --endpoint argument with every call.
that you do not have to pass the --agent-url argument with every call.

Some example commands are the following:

$ aries-cli connections
$ aries-cli connections
- fetches all the connections (jq compatible)
$ aries-cli connections invite -qr
- create an invitation (as a qr code)
$ aries-cli features
$ aries-cli features
- Fetches all the features of the cloudagent
$ aries-cli schema create --name FOO -a BAR -a BAZ
$ aries-cli schema create --name FOO -a BAR -a BAZ
- Create a new schema with the name as 'FOO' and the attributes as 'BAR' and 'BAZ'

-----------------
Expand Down
3 changes: 3 additions & 0 deletions cli/src/modules/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error;
use crate::error::Result;
use crate::help_strings::HelpStrings;
use crate::utils::config::{get_config_path, Configurations};
use clap::{Args, Subcommand};
use colored::*;
Expand All @@ -15,7 +16,9 @@ pub struct ConfigurationOptions {

#[derive(Subcommand, Debug)]
pub enum ConfigurationSubcommands {
#[clap(about = HelpStrings::ConfigurationInitialize)]
Initialize,
#[clap(about = HelpStrings::ConfigurationView)]
View,
}

Expand Down
35 changes: 19 additions & 16 deletions cli/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ pub async fn register() -> Result<()> {
let cli = Cli::parse();
let level = if cli.quiet {
LevelFilter::Error
} else if cli.raw {
LevelFilter::Debug
} else {
LevelFilter::Info
match cli.verbose {
1 => LevelFilter::Debug,
2 => LevelFilter::Trace,
2.. => LevelFilter::max(),
_ => LevelFilter::Info,
}
};
logger::init(level, cli.copy);

Expand All @@ -34,33 +37,33 @@ pub async fn register() -> Result<()> {
Commands::Configuration(options) => parse_configuration_args(options).await,
Commands::Schemas(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_schema_args(options, agent).await
}
Commands::Features(_) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_features_args(agent).await
}
Commands::Message(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_message_args(options, agent).await
}
Commands::CredentialDefinitions(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_credential_definition_args(options, agent).await
}
Commands::Connections(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
// TODO: refactor cli.copy
parse_connection_args(options, agent).await
}
Commands::Credentials(options) => {
let agent =
initialize_agent_from_cli(cli.config, cli.environment, cli.endpoint, cli.api_key)?;
initialize_agent_from_cli(cli.config, cli.environment, cli.agent_url, cli.api_key)?;
parse_credentials_args(&options.commands, agent).await
}
}?;
Expand All @@ -72,7 +75,7 @@ pub async fn register() -> Result<()> {
fn initialize_agent_from_cli(
config: Option<PathBuf>,
environment: String,
endpoint: Option<String>,
agent_url: Option<String>,
api_key: Option<String>,
) -> Result<CloudAgentPython> {
let config_path = match config {
Expand All @@ -92,23 +95,23 @@ fn initialize_agent_from_cli(
}
};

let (endpoint, api_key) = match config_path {
let (agent_url, api_key) = match config_path {
Some(cp) => {
let configurations = get_config_from_path(cp)?;
let configuration = configurations
.configurations
.get_key_value(&environment)
.ok_or(Error::InvalidEnvironment)?;
let endpoint = endpoint.unwrap_or_else(|| configuration.1.endpoint.to_owned());
let agent_url = agent_url.unwrap_or_else(|| configuration.1.endpoint.to_owned());
let api_key = api_key.or_else(|| configuration.1.api_key.to_owned());
(endpoint, api_key)
(agent_url, api_key)
}
None => {
let endpoint = endpoint.ok_or(Error::NoEndpointSupplied)?;
(endpoint, api_key)
let agent_url = agent_url.ok_or(Error::NoAgentURLSupplied)?;
(agent_url, api_key)
}
};

let version = CloudAgentPythonVersion::ZeroSixZero;
CloudAgentPython::new(endpoint, api_key, version)
CloudAgentPython::new(agent_url, api_key, version)
}
11 changes: 4 additions & 7 deletions cloudagent-python/src/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cloud_agent::CloudAgent;
use agent::error::{Error, Result};
use log::debug;
use log::trace;
use reqwest::{Client, RequestBuilder, Url};
use serde::de::DeserializeOwned;
use serde_json::Value;
Expand Down Expand Up @@ -45,13 +45,11 @@ impl CloudAgent {
None => client,
};

// TODO: change this to trace! when we support multiple log levels
debug!("About to send request:\n{:#?}", client);
trace!("About to send request:\n{:#?}", client);
match client.send().await {
Ok(res) => {
let status_code = res.status().as_u16();
// TODO: change this to trace! when we support multiple log levels
debug!("Got {} response:\n{:#?}", status_code, res);
trace!("Got {} response:\n{:#?}", status_code, res);
match status_code {
200..=299 => res
.json()
Expand All @@ -69,8 +67,7 @@ impl CloudAgent {
}
}
Err(e) => {
// TODO: change this to trace! when we support multiple log levels
debug!("Request failed {}", e);
trace!("Request failed {}", e);
Err(Error::UnreachableUrl.into())
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Configuration file and environments

Providing an endpoint and api-key with every call can be rather annoying. So in order to create a clean api we have decided to add a configuration file to the tool. The default location of the configuration file on \*NIX systems is `~/.config/aries-cli/config.ini` and on Windows `TODO`.
Providing an agent URL and an api-key with every call can be rather annoying. So in order to create a clean api we have decided to add a configuration file to the tool. The default location of the configuration file on \*NIX systems is `~/.config/aries-cli/config.ini` and on Windows `TODO`.

## No Configuration file

Without a configuration file the `--endpoint` is required and the `--api-key` is only required if the cloudagent requires this.
Without a configuration file the `--agent-url` is required and the `--api-key` is only required if the cloudagent requires this.

**example**

Expand Down Expand Up @@ -47,7 +47,7 @@ The configuration is initialized with the following structure:
endpoint = https://agent.community.animo.id
```

If a custom default endpoint is required the `--endpoint=https://cloudagent.example.com` can be supplied. Like so:
If a custom default agent URL is required the `--agent-url=https://cloudagent.example.com` can be supplied. Like so:

```sh
aries-cli config -i -e=https://cloudagent.example.com
Expand All @@ -64,7 +64,7 @@ This is the same as the two following commands:
```sh
aries-cli --config=~/.config/aries-cli/config.ini invite --qr

aries-cli --endpoint=https://agent.community.animo.id invite --qr
aries-cli --agent-url=https://agent.community.animo.id invite --qr
```

The default configuration can be expanded with your own default cloud agent and other environments.
Expand Down