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

Add support for commands with multiple arguments in custom operations #2602

Merged
merged 4 commits into from
Jan 29, 2024

Conversation

Ruadhri17
Copy link
Contributor

@Ruadhri17 Ruadhri17 commented Jan 18, 2024

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:

command = "python /etc/tedge/operations/command.py"

More in #2568.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

Copy link

codecov bot commented Jan 18, 2024

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (fb82765) 75.8% compared to head (df6e1ab) 75.9%.
Report is 8 commits behind head on main.

❗ Current head df6e1ab differs from pull request most recent head b821fdf. Consider uploading reports for the commit b821fdf to get more accurate results

Additional details and impacted files
Files Coverage Δ
crates/core/plugin_sm/src/plugin.rs 11.8% <0.0%> (-0.1%) ⬇️
crates/extensions/c8y_mapper_ext/src/converter.rs 81.6% <16.6%> (-0.3%) ⬇️
crates/common/logged_command/src/logged_command.rs 79.2% <52.9%> (-2.1%) ⬇️

... and 9 files with indirect coverage changes

Copy link
Contributor

github-actions bot commented Jan 18, 2024

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
382 0 3 382 100 52m38.941s

Copy link
Contributor

@albinsuresh albinsuresh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@rina23q rina23q left a 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.

crates/common/logged_command/src/logged_command.rs Outdated Show resolved Hide resolved
crates/extensions/c8y_mapper_ext/src/converter.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Bravo555 Bravo555 left a 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.

Comment on lines 721 to 803
if !args.is_empty() {
logged.args(&args);
}

logged.arg(&payload);
Copy link
Contributor

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.

Copy link
Contributor

@didier-wenzek didier-wenzek left a 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.

@Ruadhri17 Ruadhri17 force-pushed the custom-operation-subcommands branch 3 times, most recently from e961441 to c6fc170 Compare January 25, 2024 22:24
Copy link
Contributor

@didier-wenzek didier-wenzek left a 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),
Copy link
Contributor

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.

Copy link
Contributor

@Bravo555 Bravo555 Jan 26, 2024

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.

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>
Copy link
Member

@rina23q rina23q left a 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> {
Copy link
Member

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.

@didier-wenzek didier-wenzek added this pull request to the merge queue Jan 29, 2024
Merged via the queue into thin-edge:main with commit 952f0d1 Jan 29, 2024
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants