Skip to content

Commit

Permalink
fix: Remove init (from/init)seed_file flags
Browse files Browse the repository at this point in the history
  • Loading branch information
blelump committed Nov 29, 2024
1 parent c14576e commit b5479e8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use crate::{

#[derive(Error, Debug)]
pub enum CliError {
#[error("Error: No input provided. Use the {0} option or pipe data via stdin.")]
OptionOrStdinError(String),
#[error(transparent)]
ConfigUnparsable(#[from] ConfigFileError),
#[error("Keys derivation error")]
Expand Down
22 changes: 19 additions & 3 deletions src/subcommands/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ pub enum DataCommand {
},
/// Presents CESR data in a human-readable format
Expand {
/// A CESR string, such as one produced by the issue or sign command
cesr: String,
/// JSON-based data with CESR attachments to be transformed into a readable format
#[arg(short, long)]
message: Option<String>,
},
/// Issue credential
Issue {
Expand Down Expand Up @@ -113,7 +114,22 @@ pub async fn process_data_command(command: DataCommand) -> Result<(), CliError>
}
}
}
DataCommand::Expand { cesr } => expand::expand(&cesr),
DataCommand::Expand { message } => match message {
Some(message) => expand::expand(&message),
None => {
if io::stdin().is_terminal() {
return Err(CliError::OptionOrStdinError("-m".to_string()));
}

let mut buffer = String::new();
io::stdin().read_to_string(&mut buffer).map_err(|e| {
CliError::Verification(VerificationStatus::Error {
description: format!("Failed to read from stdin: {}", e),
})
})?;
expand::expand(&buffer)
}
},
DataCommand::Issue {
alias,
message: credential_json,
Expand Down
48 changes: 2 additions & 46 deletions src/subcommands/identifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io::{self, IsTerminal, Read};
use std::{
fs::{self, File},
io::Write,
fs::{self},
path::PathBuf,
};

Expand All @@ -25,9 +24,6 @@ pub enum IdentifierCommand {
/// Alias of the identifier used by the tool for internal purposes
#[arg(short, long)]
alias: Option<String>,
/// File with seed of the keys: current and next
#[arg(long)]
from_seed_file: Option<PathBuf>,
/// The URL of the witness
#[arg(long)]
witness_url: Vec<Url>,
Expand All @@ -37,9 +33,6 @@ pub enum IdentifierCommand {
/// Natural number specifying the minimum witnesses needed to confirm a KEL event
#[arg(long)]
witness_threshold: Option<u64>,
/// Generates json file with current and next keys seeds in provided path
#[arg(long)]
init_seed_file: Option<PathBuf>,
},
/// Show the identifier details of a specified alias
Info { alias: String },
Expand Down Expand Up @@ -124,29 +117,10 @@ pub async fn process_identifier_command(
match command {
IdentifierCommand::Init {
alias,
from_seed_file: init_seed_file,
witness_url: witness,
watcher_url: watcher,
witness_threshold,
init_seed_file: seed_file,
} => {
match (&init_seed_file, &seed_file) {
(None, Some(path)) => {
let kc = KeysConfig::default();
let mut file = File::create(path)?;
file.write_all(&serde_json::to_vec(&kc).unwrap())?;
println!("Seed generated and saved in {}", &path.to_str().unwrap());
return Ok(());
}
(_, None) => (),
(Some(_), Some(_)) => {
return Err(IdentifierSubcommandError::ArgumentsError(
"You can specify only one of 'init_seed_file' or 'seed_file', but not both"
.to_string(),
))
}
};

let alias = if let Some(alias) = alias {
alias
} else {
Expand All @@ -170,27 +144,9 @@ pub async fn process_identifier_command(

let watchers_oobis = find_oobis_for_urls(watcher).await?;

let seed_conf = init_seed_file.map(|seed_path| {
let contents = fs::read_to_string(&seed_path)
.map_err(|_e| {
IdentifierSubcommandError::ArgumentsError(format!(
"File {} doesn't exist",
seed_path.to_str().unwrap()
))
})
.unwrap();
serde_json::from_str(&contents)
.map_err(|_e| {
IdentifierSubcommandError::ArgumentsError(
"Wrong format of file with seeds".to_string(),
)
})
.unwrap()
});

handle_init(
alias,
seed_conf,
Some(KeysConfig::default()),
witnesses_oobis,
watchers_oobis,
witness_threshold,
Expand Down

0 comments on commit b5479e8

Please sign in to comment.