refactor parsing of --expires-in

This commit is contained in:
Alex Auvolat
2025-04-17 12:14:51 +02:00
parent abcef7a3fd
commit 52437e4298
3 changed files with 16 additions and 27 deletions
+3 -14
View File
@@ -1,6 +1,6 @@
use format_table::format_table;
use chrono::{Local, Utc};
use chrono::Local;
use garage_util::error::*;
@@ -78,16 +78,10 @@ impl Cli {
}
pub async fn cmd_create_admin_token(&self, opt: AdminTokenCreateOp) -> Result<(), Error> {
// TODO
let res = self
.api_request(CreateAdminTokenRequest(UpdateAdminTokenRequestBody {
name: opt.name,
expiration: opt
.expires_in
.map(|x| parse_duration::parse::parse(&x))
.transpose()
.ok_or_message("Invalid duration passed for --expires-in parameter")?
.map(|dur| Utc::now() + dur),
expiration: parse_expires_in(&opt.expires_in)?,
never_expires: false,
scope: opt.scope.map(|s| {
s.split(",")
@@ -146,12 +140,7 @@ impl Cli {
id: token.id.unwrap(),
body: UpdateAdminTokenRequestBody {
name: None,
expiration: opt
.expires_in
.map(|x| parse_duration::parse::parse(&x))
.transpose()
.ok_or_message("Invalid duration passed for --expires-in parameter")?
.map(|dur| Utc::now() + dur),
expiration: parse_expires_in(&opt.expires_in)?,
never_expires: opt.never_expires,
scope: opt.scope.map({
let mut new_scope = token.scope;
+3 -13
View File
@@ -1,6 +1,6 @@
use format_table::format_table;
use chrono::{Local, Utc};
use chrono::Local;
use garage_util::error::*;
@@ -72,12 +72,7 @@ impl Cli {
let key = self
.api_request(CreateKeyRequest(UpdateKeyRequestBody {
name: Some(opt.name),
expiration: opt
.expires_in
.map(|x| parse_duration::parse::parse(&x))
.transpose()
.ok_or_message("Invalid duration passed for --expires-in parameter")?
.map(|dur| Utc::now() + dur),
expiration: parse_expires_in(&opt.expires_in)?,
never_expires: false,
allow: None,
deny: None,
@@ -130,12 +125,7 @@ impl Cli {
id: key.access_key_id,
body: UpdateKeyRequestBody {
name: None,
expiration: opt
.expires_in
.map(|x| parse_duration::parse::parse(&x))
.transpose()
.ok_or_message("Invalid duration passed for --expires-in parameter")?
.map(|dur| Utc::now() + dur),
expiration: parse_expires_in(&opt.expires_in)?,
never_expires: opt.never_expires,
allow: None,
deny: None,
+10
View File
@@ -12,6 +12,8 @@ use std::convert::TryFrom;
use std::sync::Arc;
use std::time::Duration;
use chrono::{DateTime, Utc};
use garage_util::error::*;
use garage_rpc::*;
@@ -162,3 +164,11 @@ pub fn table_list_abbr<T: IntoIterator<Item = S>, S: AsRef<str>>(values: T) -> S
None => String::new(),
}
}
pub fn parse_expires_in(expires_in: &Option<String>) -> Result<Option<DateTime<Utc>>, Error> {
expires_in
.as_ref()
.map(|x| parse_duration::parse::parse(&x).map(|dur| Utc::now() + dur))
.transpose()
.ok_or_message("Invalid duration passed for --expires-in parameter")
}