Skip to content

Commit

Permalink
fix: print help if custom CLI is empty & replace unwrap where prossib…
Browse files Browse the repository at this point in the history
…le (stellar#1068)

(cherry picked from commit efe5916)
  • Loading branch information
willemneal committed Nov 8, 2023
1 parent f77f299 commit 93971e3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
10 changes: 8 additions & 2 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,17 @@ pub enum Error {
Fetch(#[from] fetch::Error),
#[error(transparent)]
Spec(#[from] contract_spec::Error),
#[error(transparent)]
Wasm(#[from] wasm::Error),
#[error("Failed to get file name from path: {0:?}")]
FailedToGetFileName(PathBuf),
}

impl Cmd {
pub async fn run(&self) -> Result<(), Error> {
let spec = if let Some(wasm) = &self.wasm {
let wasm: wasm::Args = wasm.into();
wasm.parse().unwrap().spec
wasm.parse()?.spec
} else {
let fetch = contract::fetch::Cmd {
contract_id: self.contract_id.clone(),
Expand Down Expand Up @@ -100,7 +104,9 @@ impl Cmd {
.ok()
.unwrap_or_else(Network::futurenet);
let absolute_path = self.output_dir.canonicalize()?;
let file_name = absolute_path.file_name().unwrap();
let file_name = absolute_path
.file_name()
.ok_or_else(|| Error::FailedToGetFileName(absolute_path.clone()))?;
let contract_name = &file_name
.to_str()
.ok_or_else(|| Error::NotUtf8(file_name.to_os_string()))?;
Expand Down
8 changes: 3 additions & 5 deletions cmd/soroban-cli/src/commands/contract/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub enum Error {
Config(#[from] config::Error),
#[error(transparent)]
StrKey(#[from] stellar_strkey::DecodeError),
#[error(transparent)]
Infallible(#[from] std::convert::Infallible),
}

impl Cmd {
Expand Down Expand Up @@ -204,11 +206,7 @@ fn get_contract_id(
contract_id_preimage: ContractIdPreimage,
network_passphrase: &str,
) -> Result<Hash, Error> {
let network_id = Hash(
Sha256::digest(network_passphrase.as_bytes())
.try_into()
.unwrap(),
);
let network_id = Hash(Sha256::digest(network_passphrase.as_bytes()).try_into()?);
let preimage = HashIdPreimage::ContractId(HashIdPreimageContractId {
network_id,
contract_id_preimage,
Expand Down
16 changes: 11 additions & 5 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,12 @@ impl Cmd {
cmd = cmd.subcommand(build_custom_cmd(&name.to_string_lossy(), &spec)?);
}
cmd.build();
let long_help = cmd.render_long_help();
let mut matches_ = cmd.get_matches_from(&self.slop);
let (function, matches_) = &matches_.remove_subcommand().unwrap();
let Some((function, matches_)) = &matches_.remove_subcommand() else {
println!("{long_help}");
std::process::exit(1);
};

let func = spec.find_function(function)?;
// create parsed_args in same order as the inputs to func
Expand All @@ -177,7 +181,7 @@ impl Cmd {
.inputs
.iter()
.map(|i| {
let name = i.name.to_string().unwrap();
let name = i.name.to_string()?;
if let Some(mut val) = matches_.get_raw(&name) {
let mut s = val.next().unwrap().to_string_lossy().to_string();
if matches!(i.type_, ScSpecTypeDef::Address) {
Expand Down Expand Up @@ -205,7 +209,10 @@ impl Cmd {
&std::fs::read(arg_path)
.map_err(|_| Error::MissingFileArg(arg_path.clone()))?,
)
.unwrap())
.map_err(|()| Error::CannotParseArg {
arg: name.clone(),
error: soroban_spec_tools::Error::Unknown,
})?)
} else {
let file_contents = std::fs::read_to_string(arg_path)
.map_err(|_| Error::MissingFileArg(arg_path.clone()))?;
Expand Down Expand Up @@ -414,7 +421,6 @@ fn build_custom_cmd(name: &str, spec: &Spec) -> Result<clap::Command, Error> {
if kebab_name != name {
cmd = cmd.alias(kebab_name);
}
let func = spec.find_function(name).unwrap();
let doc: &'static str = Box::leak(func.doc.to_string_lossy().into_boxed_str());
let long_doc: &'static str = Box::leak(arg_file_help(doc).into_boxed_str());

Expand All @@ -428,7 +434,7 @@ fn build_custom_cmd(name: &str, spec: &Spec) -> Result<clap::Command, Error> {
.alias(name.to_kebab_case())
.num_args(1)
.value_parser(clap::builder::NonEmptyStringValueParser::new())
.long_help(spec.doc(name, type_).unwrap());
.long_help(spec.doc(name, type_)?);

file_arg = file_arg
.long(&file_arg_name)
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/src/commands/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum Error {
ExecutableNotFound(String, String),
#[error(transparent)]
Which(#[from] which::Error),
#[error(transparent)]
Regex(#[from] regex::Error),
}

const SUBCOMMAND_TOLERANCE: f64 = 0.75;
Expand Down Expand Up @@ -82,7 +84,7 @@ pub fn list() -> Result<Vec<String>, Error> {
} else {
r"^soroban-.*"
};
let re = regex::Regex::new(re_str).unwrap();
let re = regex::Regex::new(re_str)?;
Ok(which::which_re(re)?
.filter_map(|b| {
let s = b.file_name()?.to_str()?;
Expand Down
4 changes: 3 additions & 1 deletion cmd/soroban-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub mod parsing {
CannotParseAccountId { account_id: String },
#[error("cannot parse asset: {asset}")]
CannotParseAsset { asset: String },
#[error(transparent)]
Regex(#[from] regex::Error),
}

pub fn parse_asset(str: &str) -> Result<Asset, Error> {
Expand All @@ -145,7 +147,7 @@ pub mod parsing {
}
let code = split[0];
let issuer = split[1];
let re = Regex::new("^[[:alnum:]]{1,12}$").unwrap();
let re = Regex::new("^[[:alnum:]]{1,12}$")?;
if !re.is_match(code) {
return Err(Error::InvalidAssetCode {
asset: str.to_string(),
Expand Down

0 comments on commit 93971e3

Please sign in to comment.