Skip to content

Commit

Permalink
feat(rust): add plain output to project ticket
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Oct 28, 2024
1 parent b1c3433 commit 16aadfe
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
34 changes: 23 additions & 11 deletions implementations/rust/ockam/ockam_command/src/project/ticket.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::cmp::min;
use std::collections::BTreeMap;
use std::str::FromStr;
use std::time::Duration;
Expand All @@ -10,19 +9,20 @@ use miette::miette;
use tracing::debug;

use crate::shared_args::{IdentityOpts, RetryOpts, TrustOpts};
use crate::util::parsers::duration_parser;
use crate::util::parsers::{duration_parser, duration_to_human_format};
use crate::{docs, Command, CommandGlobalOpts, Error, Result};
use ockam::Context;
use ockam_api::authenticator::direct::{
OCKAM_ROLE_ATTRIBUTE_ENROLLER_VALUE, OCKAM_ROLE_ATTRIBUTE_KEY, OCKAM_TLS_ATTRIBUTE_KEY,
};
use ockam_api::authenticator::enrollment_tokens::{
TokenIssuer, MAX_RECOMMENDED_TOKEN_DURATION, MAX_RECOMMENDED_TOKEN_USAGE_COUNT,
TokenIssuer, DEFAULT_TOKEN_DURATION, DEFAULT_TOKEN_USAGE_COUNT, MAX_RECOMMENDED_TOKEN_DURATION,
MAX_RECOMMENDED_TOKEN_USAGE_COUNT,
};
use ockam_api::cli_state::{ExportedEnrollmentTicket, ProjectRoute};
use ockam_api::colors::color_primary;
use ockam_api::nodes::InMemoryNode;
use ockam_api::{fmt_log, fmt_ok, fmt_warn};
use ockam_api::{fmt_info, fmt_log, fmt_ok, fmt_warn};
use ockam_multiaddr::MultiAddr;

const LONG_ABOUT: &str = include_str!("./static/ticket/long_about.txt");
Expand Down Expand Up @@ -144,15 +144,27 @@ impl Command for TicketCommand {
.await?
.export_legacy()?;

opts.terminal
.write_line(fmt_ok!("Created enrollment ticket\n"))?;
opts.terminal.write_line(fmt_log!(
"You can use it to enroll another machine using: {}",
color_primary("ockam project enroll")
))?;
let usage_count = cmd.usage_count.unwrap_or(DEFAULT_TOKEN_USAGE_COUNT);
let plain = fmt_ok!("Created enrollment ticket\n\n")
+ &fmt_info!(
"It will expire in {} and it can be used {}\n",
color_primary(duration_to_human_format(
&cmd.expires_in.unwrap_or(DEFAULT_TOKEN_DURATION)
)),
if usage_count == 1 {
color_primary("once").to_string()
} else {
format!("up to {} times", color_primary(usage_count))
}
)
+ &fmt_log!(
"You can use it to enroll another machine using: {}",
color_primary("ockam project enroll")
);

opts.terminal
.stdout()
.plain(plain)
.machine(ticket.hex_encoded()?.to_string())
.json_obj(ticket)?
.write_line()?;
Expand All @@ -162,7 +174,7 @@ impl Command for TicketCommand {
}

impl TicketCommand {
async fn parse_args(mut self, opts: &CommandGlobalOpts) -> Result<Self> {
async fn parse_args(self, opts: &CommandGlobalOpts) -> Result<Self> {
// Handle expires_in and usage_count limits
if let (Some(expires_in), Some(usage_count)) = (self.expires_in, self.usage_count) {
if expires_in >= MAX_RECOMMENDED_TOKEN_DURATION
Expand Down
22 changes: 22 additions & 0 deletions implementations/rust/ockam/ockam_command/src/util/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,25 @@ pub(crate) fn project_name_parser(s: &str) -> Result<String> {
pub(crate) fn duration_parser(arg: &str) -> std::result::Result<Duration, clap::Error> {
parse_duration(arg).map_err(|_| Error::raw(ErrorKind::InvalidValue, "Invalid duration."))
}

pub(crate) fn duration_to_human_format(duration: &Duration) -> String {
let mut parts = vec![];
let secs = duration.as_secs();
let days = secs / 86400;
if days > 0 {
parts.push(format!("{}d", days));
}
let hours = (secs % 86400) / 3600;
if hours > 0 {
parts.push(format!("{}h", hours));
}
let minutes = (secs % 3600) / 60;
if minutes > 0 {
parts.push(format!("{}m", minutes));
}
let seconds = secs % 60;
if seconds > 0 {
parts.push(format!("{}s", seconds));
}
parts.join(" ")
}

0 comments on commit 16aadfe

Please sign in to comment.