Skip to content

Commit

Permalink
[wallet bugfix] - Wallet interactive mode does not accept whitespaces…
Browse files Browse the repository at this point in the history
… in arguments (MystenLabs#1795)

* changed transfer -> transfer-coin

* add unit test
  • Loading branch information
patrickkuo authored May 5, 2022
1 parent d5958b6 commit 48f7a69
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ once_cell = "1.10.0"
jsonrpsee = { git = "https://github.com/paritytech/jsonrpsee.git", rev = "661870a0ab4159b2bb104ace25a2cb817a097e03", features = ["full"] }
jsonrpsee-proc-macros = "0.11.0"
schemars = "0.8.8"
shell-words = "1.1.0"

[dev-dependencies]
tracing-test = "0.2.1"
Expand Down
23 changes: 13 additions & 10 deletions sui/src/shell.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use anyhow::anyhow;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::env;
Expand Down Expand Up @@ -86,7 +87,7 @@ impl<P: Display, S: Send, H: AsyncHandler<S>> Shell<P, S, H> {
let line = substitute_env_variables(line);

// Runs the line
match Self::split_and_unescape(line.trim()) {
match split_and_unescape(line.trim()) {
Ok(line) => {
if let Some(s) = line.first() {
// These are shell only commands.
Expand Down Expand Up @@ -132,21 +133,23 @@ impl<P: Display, S: Send, H: AsyncHandler<S>> Shell<P, S, H> {
break;
};
}
Err(e) => writeln!(err, "{}", e.red())?,
Err(e) => writeln!(err, "{}", e.to_string().red())?,
}
}
Ok(())
}
}

fn split_and_unescape(line: &str) -> Result<Vec<String>, String> {
let mut commands = Vec::new();
for word in line.split_whitespace() {
let command =
unescape(word).ok_or_else(|| format!("Error: Unhandled escape sequence {word}"))?;
commands.push(command);
}
Ok(commands)
fn split_and_unescape(line: &str) -> Result<Vec<String>, anyhow::Error> {
let mut commands = Vec::new();
let split: Vec<String> = shell_words::split(line)?;

for word in split {
let command =
unescape(&word).ok_or_else(|| anyhow!("Error: Unhandled escape sequence {word}"))?;
commands.push(command);
}
Ok(commands)
}

fn substitute_env_variables(s: String) -> String {
Expand Down
17 changes: 17 additions & 0 deletions sui/src/unit_tests/shell_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustyline::Context;

use sui_types::base_types::ObjectID;

use crate::shell::split_and_unescape;
use crate::shell::{
substitute_env_variables, CacheKey, CommandStructure, CompletionCache, ShellHelper,
};
Expand Down Expand Up @@ -216,3 +217,19 @@ fn test_completer_with_cache() {
.collect::<Vec<_>>();
assert_eq!(vec!["--gas"], candidates);
}

#[test]
fn test_split_line() {
let test = "create-example-nft --name \"test 1\" --description \"t e s t 2\"";
let result = split_and_unescape(test).unwrap();
assert_eq!(
vec![
"create-example-nft",
"--name",
"test 1",
"--description",
"t e s t 2"
],
result
);
}

0 comments on commit 48f7a69

Please sign in to comment.