io/byte-pipeline.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
bc38d685824b6ad01ae93433e2b61069786deea2
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// Manual, host-dependent: a three-stage byte pipeline through std:io/commands.
// bin/protos --allow-execution protos/examples/io/byte-pipeline.protos
// It needs the explicit execution grant and the host programs /bin/echo, /usr/bin/tr and
// /bin/cat, so it is outside the authority-free example sweep. There is no shell: each
// stage is one executable with an exact argument Array.

Commands: import("std:io/commands")
ProcessStreams: import("std:io/process_streams")
out: ProcessStreams.stdoutWriter(process)

// Each child receives exactly the environment Map it is given; nothing is inherited.
locale: Map()
locale["LC_ALL"] = "C"

produce: Commands.command("/bin/echo", ["protos pipelines stream raw bytes"], Map(), null)
shout: Commands.command("/usr/bin/tr", ["a-z", "A-Z"], locale, null)
copy: Commands.command("/bin/cat", [], Map(), null)

// stdin is null (the first stage gets EOF), the final stdout is the raw Process stdout
// (borrowed, never closed), and each stage has its own stderr route: the Process stderr
// for the first and last stages, discarded for the middle one. The library runs every
// route itself; the application contains no read/write loop.
run: Commands.run(execution, [produce, shout, copy], null, process.stdout(),
    [process.stderr(), null, process.stderr()])
result: run.wait().value()

// The result is frozen data: per-stage start flags and statuses, ordered failure and
// cancellation records, the first Error, and whether the run was stopped.
if(result.success) {
    out.writeLine("pipeline succeeded").value()
} else {
    out.writeLine("pipeline did not succeed").value()
    for(failure in result.failures) {
        out.writeLine("failure in phase " + failure.phase).value()
    }
}