mu-file-utils: add run_command0

To ensure command ran and had exit-code=0 in one go.
This commit is contained in:
Dirk-Jan C. Binnema 2023-09-19 22:24:38 +03:00
parent ae87be6a48
commit 24add72126
2 changed files with 19 additions and 5 deletions

View File

@ -162,7 +162,7 @@ Mu::runtime_path(Mu::RuntimePath path, const std::string& muhome)
throw std::logic_error("unknown path");
}
}
/* LCOV_EXCL_START*/
static gpointer
cancel_wait(gpointer data)
{
@ -210,7 +210,7 @@ Mu::g_cancellable_new_with_timeout(guint timeout)
return cancel;
}
/* LCOV_EXCL_STOP*/
Result<std::string>
Mu::read_from_stdin()
@ -286,6 +286,18 @@ Mu::run_command(std::initializer_list<std::string> args, bool try_setsid)
to_string_gchar(std::move(std_err/*consumed*/))});
}
Result<Mu::CommandOutput>
Mu::run_command0(std::initializer_list<std::string> args, bool try_setsid)
{
if (auto&& res{run_command(args, try_setsid)}; !res)
return res;
else if (res->exit_code != 0)
return Err(Error::Code::File, "command ran with non-zero exit code");
else
return Ok(std::move(*res));
}
Mu::Option<std::string>
Mu::program_in_path(const std::string& name)
{

View File

@ -227,14 +227,14 @@ Result<std::string> make_temp_dir();
Result<void> remove_directory(const std::string& path);
/**
* Run some system command
* Run some system command.
*
* @param args a list of commmand line arguments (like argv)
* @param try_setsid whether to try setsid(2) (see its manpage for details) if this
* system supports it.
*
* @return Ok(exit code) or an error. Note that exit-code != 0 is _not_
* considered an error from the perspective of this function.
* @return Ok(exit code) or an error. Note that exit-code != 0 is _not_ considered an error from the
* perspective of run_command, but is for run_command0
*/
struct CommandOutput {
int exit_code;
@ -243,6 +243,8 @@ struct CommandOutput {
};
Result<CommandOutput> run_command(std::initializer_list<std::string> args,
bool try_setsid=false);
Result<CommandOutput> run_command0(std::initializer_list<std::string> args,
bool try_setsid=false);
/**
* Try to 'play' (ie., open with it's associated program) a file. On MacOS, the