refactor: use Cmd class for hook command execution#1674
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors hook command execution in libs/utils/src/linglong/utils/hooks.cpp by replacing std::system and manual shell escaping with a custom Cmd class wrapper. This simplifies environment variable handling and command execution. Feedback on this change highlights that switching to Cmd::exec() captures standard output into the result, which is currently discarded. It is recommended to log this output when successful to prevent losing debugging and monitoring information.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| auto result = cmd.exec({ "-c", command }); | ||
| if (!result.has_value()) { | ||
| return LINGLONG_ERR( | ||
| fmt::format("Hook command '{}' failed: {}.", command, result.error())); | ||
| } |
There was a problem hiding this comment.
With the switch from std::system() to Cmd::exec(), the standard output (stdout) of the hook commands is now captured into result.value() instead of being printed directly to the standard output of the parent process. Since result.value() is currently discarded, any output printed by successful hook commands will be completely lost, which can make debugging and monitoring difficult.
Consider logging the stdout of the hook command if it is not empty.
auto result = cmd.exec({ "-c", command });
if (!result.has_value()) {
return LINGLONG_ERR(
fmt::format("Hook command '{}' failed: {}.", command, result.error()));
}
if (!result.value().empty()) {
LogD("Hook command '{}' output: {}", command, result.value());
}
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
1. Replace std::system() calls with Cmd class for better process control 2. Remove escapeAndWrapCommandForShell function, now using Cmd.exec() with argument list 3. Replace EnvironmentVariableGuard with Cmd.setEnv() for environment handling 4. Simplify error handling by leveraging Cmd class result types 5. Update copyright year to 2025-2026 6. Remove unused includes (linglong/utils/env.h, cstdlib) Influence: 1. Test hook commands execution with valid commands 2. Verify hook commands with environment variables are properly set 3. Test error handling when hook commands fail 4. Verify pre-install, post-install, and post-uninstall hooks work correctly 5. Test commands with special characters and quotes 6. Verify proper error messages are returned for failed commands
Influence: