diff options
author | Lars Wirzenius <liw@liw.fi> | 2022-12-15 11:47:54 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2022-12-15 11:47:54 +0200 |
commit | 4d4053b01c0a7cdc5a1c4155524a2479ab41488f (patch) | |
tree | 71fdd2324ba2743e2e97148e1bfb1dd8030ecb9f | |
parent | 3391d1b7c3c960c1373d0fe5a9164ea3aacaf7dd (diff) | |
download | sshca-4d4053b01c0a7cdc5a1c4155524a2479ab41488f.tar.gz |
refactor: move timestamp formatting into a utility function
Sponsored-by: author
-rw-r--r-- | src/cmd/host.rs | 9 | ||||
-rw-r--r-- | src/util.rs | 8 |
2 files changed, 11 insertions, 6 deletions
diff --git a/src/cmd/host.rs b/src/cmd/host.rs index fdaff19..2896fe3 100644 --- a/src/cmd/host.rs +++ b/src/cmd/host.rs @@ -7,7 +7,7 @@ use crate::error::CAError; use crate::info::{self, Info, SafeHost}; use crate::key::{KeyKind, KeyPair, PublicKey, SshKey}; use crate::store::{KeyStore, KeyStoreError}; -use crate::util::read_as_string; +use crate::util::{format_timestamp, read_as_string}; use clap::{ArgAction, Parser}; use std::fs::File; @@ -322,8 +322,7 @@ impl Runnable for Generate { if self.temporary { let now = std::time::SystemTime::now(); if let Some(valid_until) = time::OffsetDateTime::from(now).checked_add(SHORT_TIME) { - let format = format_description!("[year]:[month]:[day]T[hour]:[minute]:[second]"); - let valid_until = valid_until.format(&format).map_err(CAError::TimeFormat)?; + let valid_until = format_timestamp(valid_until)?; host.set_valid_until(valid_until); } else { return Err(CAError::ShortTime); @@ -361,9 +360,7 @@ impl Runnable for Regenerate { if self.temporary { let now = std::time::SystemTime::now(); if let Some(valid_until) = time::OffsetDateTime::from(now).checked_add(SHORT_TIME) { - let format = - format_description!("[year]:[month]:[day]T[hour]:[minute]:[second]"); - let valid_until = valid_until.format(&format).map_err(CAError::TimeFormat)?; + let valid_until = format_timestamp(valid_until)?; host.set_valid_until(valid_until); } else { return Err(CAError::ShortTime); diff --git a/src/util.rs b/src/util.rs index a772ad0..3d50240 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,6 +5,8 @@ use std::fs::{File, Permissions}; use std::io::Write; use std::os::unix::fs::PermissionsExt; use std::path::Path; +use time::OffsetDateTime; +use time::macros::format_description; pub(crate) fn read<P: AsRef<Path>>(filename: P) -> Result<Vec<u8>, CAError> { let filename = filename.as_ref(); @@ -33,3 +35,9 @@ pub fn write_string<P: AsRef<Path>>(filename: P, s: &str) -> Result<(), CAError> pub(crate) fn prompt_token() { eprintln!("You may need to touch the security token button now"); } + +pub(crate) fn format_timestamp(valid_until: OffsetDateTime) -> Result<String, CAError> { + let format = format_description!("[year]:[month]:[day]T[hour]:[minute]:[second]"); + let valid_until = valid_until.format(&format).map_err(CAError::TimeFormat)?; + Ok(valid_until) +} |