|
| 1 | +#' @importFrom checkmate assertCharacter assertString %??% |
| 2 | +#' @importFrom stringi stri_replace_all_fixed stri_flatten |
| 3 | +runOSCommand = function(sys.cmd, sys.args = character(0L), stdin = "", nodename = "localhost") { |
| 4 | + assertCharacter(sys.cmd, any.missing = FALSE, len = 1L) |
| 5 | + assertCharacter(sys.args, any.missing = FALSE) |
| 6 | + assertString(nodename, min.chars = 1L) |
| 7 | + |
| 8 | + if (!isLocalHost(nodename)) { |
| 9 | + command = sprintf("%s %s", sys.cmd, stri_flatten(sys.args, " ")) |
| 10 | + if (getRversion() < "4.0.0") { |
| 11 | + command = shQuote(command) |
| 12 | + } |
| 13 | + command = stri_replace_all_fixed(command, "\\$", "$") |
| 14 | + sys.args = c("-q", nodename, command) |
| 15 | + sys.cmd = "ssh" |
| 16 | + } |
| 17 | + |
| 18 | + "!DEBUG [runOSCommand]: cmd: `sys.cmd` `stri_flatten(sys.args, ' ')`" |
| 19 | + |
| 20 | + if (nzchar(Sys.which(sys.cmd))) { |
| 21 | + res = suppressWarnings(system2(command = sys.cmd, args = sys.args, stdin = stdin, stdout = TRUE, stderr = TRUE, wait = TRUE)) |
| 22 | + output = as.character(res) |
| 23 | + exit.code = attr(res, "status") %??% 0L |
| 24 | + } else { |
| 25 | + output = "command not found" |
| 26 | + exit.code = 127L |
| 27 | + } |
| 28 | + |
| 29 | + "!DEBUG [runOSCommand]: OS result (stdin '`stdin`', exit code `exit.code`):" |
| 30 | + "!DEBUG [runOSCommand]: `paste0(output, sep = '\n')`" |
| 31 | + |
| 32 | + return(list(sys.cmd = sys.cmd, sys.args = sys.args, exit.code = exit.code, output = output)) |
| 33 | +} |
| 34 | + |
| 35 | +isLocalHost = function(nodename) { |
| 36 | + is.null(nodename) || nodename %chin% c("localhost", "127.0.0.1", "::1") |
| 37 | +} |
0 commit comments