1
0

Implemented quiet option for cmd_output (fixes #9) (#21)

This commit is contained in:
Patrick Gaskin
2020-05-13 11:38:05 -04:00
committed by GitHub
parent bf03f9e22b
commit 043dfb37e6
2 changed files with 12 additions and 2 deletions

View File

@@ -60,6 +60,7 @@
# cmd_spawn - the command line to pass to /bin/sh -c (started in /) # cmd_spawn - the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the toast with the process PID from being displayed. # It can be prefixed with "quiet:" to prevent the toast with the process PID from being displayed.
# cmd_output - the timeout in milliseconds (0 < t < 10000), a colon, then the command line to pass to /bin/sh -c (started in /) # cmd_output - the timeout in milliseconds (0 < t < 10000), a colon, then the command line to pass to /bin/sh -c (started in /)
# It can be prefixed with "quiet:" to prevent the message box with the output from being displayed (i.e. you'd use this where you'd normally use >/dev/null 2>&1).
# #
# chain:<action>:<arg> # chain:<action>:<arg>
# chain_failure:<action>:<arg> # chain_failure:<action>:<arg>

View File

@@ -405,14 +405,22 @@ NM_ACTION_(cmd_spawn) {
NM_ACTION_(cmd_output) { NM_ACTION_(cmd_output) {
#define NM_ERR_RET nullptr #define NM_ERR_RET nullptr
// split the timeout into timeout, put the command into cmd
char *tmp = strdup(arg); char *tmp = strdup(arg);
char *cmd = tmp; char *cmd = tmp;
char *tmp1 = strtrim(strsep(&cmd, ":")), *tmp2; char *tmp1 = strtrim(strsep(&cmd, ":")), *tmp2;
long timeout = strtol(tmp1, &tmp2, 10); long timeout = strtol(tmp1, &tmp2, 10);
cmd = strtrim(cmd); cmd = strtrim(cmd);
NM_ASSERT(*tmp1 && !*tmp2 && timeout > 0 && timeout < 10000, "invalid timeout '%s'", tmp1); NM_ASSERT(*tmp1 && !*tmp2 && timeout > 0 && timeout < 10000, "invalid timeout '%s'", tmp1);
// parse the quiet option and update cmd if it's specified
char *tmp3 = strdup(cmd);
char *tmp4 = tmp3;
char *tmp5 = strtrim(strsep(&tmp4, ":"));
bool quiet = tmp4 && !strcmp(tmp5, "quiet");
if (tmp4 && quiet)
cmd = strtrim(tmp4); // update cmd to exclude "quiet:"
QProcess proc; QProcess proc;
proc.setProcessChannelMode(QProcess::MergedChannels); proc.setProcessChannelMode(QProcess::MergedChannels);
proc.setWorkingDirectory(QStringLiteral("/")); proc.setWorkingDirectory(QStringLiteral("/"));
@@ -445,9 +453,10 @@ NM_ACTION_(cmd_output) {
if (out.length() > 500) if (out.length() > 500)
out = out.left(500) + "..."; out = out.left(500) + "...";
free(tmp3);
free(tmp); free(tmp);
NM_RETURN_OK(nm_action_result_msg("%s", qPrintable(out))); NM_RETURN_OK(quiet ? nm_action_result_silent() : nm_action_result_msg("%s", qPrintable(out)));
#undef NM_ERR_RET #undef NM_ERR_RET
} }