Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: print help if custom CLI is empty & replace unwrap where prossible #1068

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
17 changes: 12 additions & 5 deletions cmd/soroban-cli/src/commands/contract/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,19 @@ impl Cmd {
let mut cmd = clap::Command::new(self.contract_id.clone())
.no_binary_name(true)
.term_width(300)
// .help
willemneal marked this conversation as resolved.
Show resolved Hide resolved
.max_term_width(300);

for ScSpecFunctionV0 { name, .. } in spec.find_functions()? {
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 {
leighmcculloch marked this conversation as resolved.
Show resolved Hide resolved
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 +182,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 +210,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 +422,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 +435,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
Loading