Skip to content

Commit 1dc6617

Browse files
monyarmPetarKirov
authored andcommitted
feat(mcl/utils/process): Add support to execute function, for supplying redirect; Improve execute function argument escaping, to not escape globs
1 parent 72ddc16 commit 1dc6617

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/mcl/src/src/mcl/utils/process.d

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,34 @@ import mcl.utils.test;
44

55
import mcl.utils.tui : bold;
66

7-
import std.process : ProcessPipes;
7+
import std.process : ProcessPipes, Redirect;
88
import std.string : split, strip;
99
import core.sys.posix.unistd : geteuid;
1010
import std.json : JSONValue, parseJSON;
1111

1212
bool isRoot() => geteuid() == 0;
1313

14-
T execute(T = string)(string args, bool printCommand = true, bool returnErr = false) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
14+
T execute(T = string)(string args, bool printCommand = true, bool returnErr = false, Redirect redirect = Redirect.all) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
1515
{
16-
return execute!T(args.split(" "), printCommand, returnErr);
16+
return execute!T(args.strip.split(" "), printCommand, returnErr, redirect);
1717
}
18-
T execute(T = string)(string[] args, bool printCommand = true, bool returnErr = false) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
18+
T execute(T = string)(string[] args, bool printCommand = true, bool returnErr = false, Redirect redirect = Redirect.all) if (is(T == string) || is(T == ProcessPipes) || is(T == JSONValue))
1919
{
2020
import std.exception : enforce;
2121
import std.format : format;
22-
import std.process : pipeShell, wait, Redirect, escapeShellCommand;
22+
import std.process : pipeShell, wait, escapeShellCommand;
2323
import std.logger : tracef, errorf, infof;
2424
import std.array : join;
25-
import std.algorithm : map;
25+
import std.algorithm : map, canFind;
2626
import std.conv : to;
2727

28-
auto cmd = args.map!escapeShellCommand.join(" ");
28+
auto cmd = args.map!(x => x.canFind("*") ? x : x.escapeShellCommand()).join(" ");
2929

3030
if (printCommand)
3131
{
3232
infof("\n$ `%s`", cmd.bold);
3333
}
34-
auto res = pipeShell(cmd, Redirect.all);
34+
auto res = pipeShell(cmd, redirect);
3535
static if (is(T == ProcessPipes))
3636
{
3737
return res;

0 commit comments

Comments
 (0)