Path.execute

Execute the file pointed by path

  1. auto execute(string[] args, string[string] env, Nullable!Path workDir, std.process.Config config, size_t maxOutput)
  2. auto execute(string[] args, string[string] env, Path workDir, std.process.Config config, size_t maxOutput)
    struct Path
    @safe const
    execute
    (
    in string[] args
    ,
    in string[string] env
    ,,
    in std.process.Config config = std.process.Config.none
    ,
    in size_t maxOutput = size_t.max
    )

Parameters

args string[]

arguments to be passed to program

env string[string]

associative array that represent environment variables to be passed to program pointed by path

workDir Path

Working directory for new process.

config std.process.Config

Parameters for process creation. See See std.process.Config

maxOutput size_t

Max bytes of output to be captured

Return Value

Type: auto

An std.typecons.Tuple!(int, "status", string, "output").

Examples

Example of running execute to run simple script

import dshould;
import std.conv: octal;
Path root = createTempPath();
scope(exit) root.remove();

// Create simple test script that will print its arguments
root.join("test-script").writeFile(
    "#!/usr/bin/env bash\necho \"$@\";");

// Add permission to run this script
root.join("test-script").setAttributes(octal!755);

// Run test script without args
auto status1 = root.join("test-script").execute;
status1.status.should.be(0);
status1.output.should.equal("\n");

auto status2 = root.join("test-script").execute(["hello", "world"]);
status2.status.should.be(0);
status2.output.should.equal("hello world\n");

auto status3 = root.join("test-script").execute(["hello", "world\nplus"]);
status3.status.should.be(0);
status3.output.should.equal("hello world\nplus\n");

auto status4 = root.join("test-script").execute(
        ["hello", "world"],
        null,
        root.nullable);
status4.status.should.be(0);
status4.output.should.equal("hello world\n");

Example of running execute to run script that will print current working directory

import dshould;
import std.conv: octal;

const Path current_dir = Path.current;
scope(exit) current_dir.chdir;

Path root = createTempPath();
scope(exit) root.remove();

// Create simple test script that will print its arguments
root.join("test-script").writeFile(
    "#!/usr/bin/env bash\npwd;");

// Add permission to run this script
root.join("test-script").setAttributes(octal!755);

// Change current working directory to our root;
root.chdir;

// Do not pass current working directory
// (script have to print current working directory)
auto status0 = root.join("test-script").execute(["hello", "world"]);
status0.status.should.be(0);
status0.output.should.equal(root.toString ~ "\n");

// Create some other directory
auto my_dir = root.join("my-dir");
my_dir.mkdir();

// Passs my-dir as workding directory for script
auto status1 = root.join("test-script").execute(
        ["hello", "world"],
        null,
        my_dir.nullable);
status1.status.should.be(0);
status1.output.should.equal(my_dir.toString ~ "\n");

// Passs null path as workding direcotry for script
auto status2 = root.join("test-script").execute(
        ["hello", "world"],
        null,
        Nullable!Path.init);
status2.status.should.be(0);
status2.output.should.equal(root.toString ~ "\n");

// Passs my-dir as workding directory for script (without nullable)
auto status3 = root.join("test-script").execute(
        ["hello", "world"],
        null,
        my_dir);
status3.status.should.be(0);
status3.output.should.equal(my_dir.toString ~ "\n");

Meta