-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add support for commands with multiple arguments in custom operations #2602
Add support for commands with multiple arguments in custom operations #2602
Conversation
e12852d
to
b194649
Compare
Codecov ReportAttention:
Additional details and impacted files
|
Robot Results
|
4cf2a0f
to
834aea9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
...ustom_operation/custom_operation_multiple_args/operation_multiple_arguments_in_command.robot
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Panic must be avoided. Others look fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocker: update any relevant documentation (example 1)
Besides the panic that Rina mentioned, I think it would be the best to address the documentation as part of this PR. We need to keep in mind what are the limitations of shell_words
:
The result is exactly the same as one obtained from Unix shell as long as those unsupported features are not present in input: no operators, no variable assignments, no tilde expansion, no parameter expansion, no command substitution, no arithmetic expansion, no pathname expansion.
In case those unsupported shell features are present, the syntax that introduce them is interpreted literally.
This can potentially confusing to users, as error message Operation execution failed: No such file or directory (os error 2). Command: ~/../etc/tedge/operations/command_1.sh. Operation name: c8y_Command
one might think that the file doesn't exist instead of the tilde character not getting expanded. Granted most users shouldn't do that kind of thing anyway, so I'd say error messages are enough, but this behaviour needs to be described in the documentation.
if !args.is_empty() { | ||
logged.args(&args); | ||
} | ||
|
||
logged.arg(&payload); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beware, that the behavior is not obvious and has to be documented: the smart rest payload is always added as the very last argument.
The command = "python /etc/tedge/operations/command.py"
actually leads to the execution of python /etc/tedge/operations/command.py $SMART_REST_PAYLOAD
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes can be simpler by moving the logic to parse the command line into the logged_command
crate.
Instead of introducing an args
method to mutate a LoggedCommand
that tries to rebuild the command line from where the arguments have been extracted, it would be far simpler to pass the command line to LoggedCommand::new()
and do the split there.
e961441
to
c6fc170
Compare
c6fc170
to
30d2e66
Compare
30d2e66
to
f3d7b8e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved
let mut args = shell_words::split(&command_line)?; | ||
|
||
let mut command = match args.len() { | ||
0 => return Err(shell_words::ParseError), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Display
impl for shell_words::ParseError
is "missing closing quote"
which is very much not the case here. We should use our own error with a correct message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also one more thing, need to add time
feature to logged_command/Cargo.toml
, because without it it's impossible to run tests for the package.
f3d7b8e
to
df6e1ab
Compare
Signed-off-by: Krzysztof Piotrowski <krzysztof.piotrowski@inetum.com>
Signed-off-by: Krzysztof Piotrowski <krzysztof.piotrowski@inetum.com>
Signed-off-by: Krzysztof Piotrowski <krzysztof.piotrowski@inetum.com>
Signed-off-by: Krzysztof Piotrowski <krzysztof.piotrowski@inetum.com>
df6e1ab
to
b821fdf
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved
@@ -148,23 +148,40 @@ impl std::fmt::Display for LoggedCommand { | |||
} | |||
|
|||
impl LoggedCommand { | |||
pub fn new(program: impl AsRef<OsStr>) -> LoggedCommand { | |||
pub fn new(program: impl AsRef<OsStr>) -> Result<LoggedCommand, std::io::Error> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very minor: I think try_new()
is a better name as the function now returns a Result
.
Proposed changes
This PR adds support for arguments/subcommands when running command in custom operation. Command accepts string with arguments separated by whitespaces, e.g:
More in #2568.
Types of changes
Paste Link to the issue
Checklist
cargo fmt
as mentioned in CODING_GUIDELINEScargo clippy
as mentioned in CODING_GUIDELINESFurther comments