Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/zio/zio-process

A simple ZIO library for interacting with external processes and command-line programs
https://github.com/zio/zio-process

scala zio

Last synced: 6 days ago
JSON representation

A simple ZIO library for interacting with external processes and command-line programs

Awesome Lists containing this project

README

        

[//]: # (This file was autogenerated using `zio-sbt-website` plugin via `sbt generateReadme` command.)
[//]: # (So please do not edit it manually. Instead, change "docs/index.md" file or sbt setting keys)
[//]: # (e.g. "readmeDocumentation" and "readmeSupport".)

# ZIO Process

[ZIO Process](https://zio.dev/zio-process) is a simple ZIO library for interacting with external processes and command-line programs.

[![Production Ready](https://img.shields.io/badge/Project%20Stage-Production%20Ready-brightgreen.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-process/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-process_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-process_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-process_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-process_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/zio-process-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/zio-process-docs_2.13) [![ZIO Process](https://img.shields.io/github/stars/zio/zio-process?style=social)](https://github.com/zio/zio-process)

## Introduction

ZIO Process is backed by ZIO Streams, enabling you to work with processes that output gigabytes of data without worrying about exceeding memory constraints.

ZIO Process provides a principled way to call out to external programs from within a ZIO application while leveraging ZIO's capabilities like interruptions and offloading blocking operations to a separate thread pool. We don't need to worry about avoiding these common pitfalls as we would if we were to use Java's `ProcessBuilder` or the `scala.sys.process` API since it is already taken care of for you.

Key features of the ZIO Process:
- **Deep ZIO Integration** — Leverages ZIO to handle interruption and offload blocking operations.
- **ZIO Streams** — ZIO Process is backed by ZIO Streams, which enables us to obtain the command output as streams of bytes or lines. So we can work with processes that output gigabytes of data without worrying about exceeding memory constraints.
- **Descriptive Errors** — In case of command failure, it has a descriptive category of errors.
- **Piping** — It has a simple DSL for piping the output of one command as the input of another.
- **Blocking Operations**

## Installation

In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-process" % "0.7.2"
```

## Native support
Some features might have a different behaviour when using Scala Native. Creating non-existent commands do not throw a corresponding error. `bash` command might be needed when executing a script. In some cases, using `ZStreams` as standard input might block the process. Instead, a Java `InputStream` can be used to write to the standard input of the process.

## Example

Here is a simple example of using ZIO Process:

```scala
import zio._
import zio.process.Command

import java.io.File

object ZIOProcessExample extends ZIOAppDefault {
val myApp = for {
fiber <- Command("dmesg", "--follow").linesStream
.foreach(Console.printLine(_))
.fork
cpuModel <- (Command("cat", "/proc/cpuinfo") |
Command("grep", "model name") |
Command("head", "-n", "1") |
Command("cut", "-d", ":", "-f", "2")).string
_ <- Console.printLine(s"CPU Model: $cpuModel")
_ <- (Command("pg_dump", "my_database") > new File("dump.sql")).exitCode
_ <- fiber.join
} yield ()

override def run = myApp
}
```

## Documentation

Learn more on the [ZIO Process homepage](https://zio.dev/zio-process/)!

## Contributing

For the general guidelines, see ZIO [contributor's guide](https://zio.dev/about/contributing).

## Code of Conduct

See the [Code of Conduct](https://zio.dev/about/code-of-conduct)

## Support

Come chat with us on [![Badge-Discord]][Link-Discord].

[Badge-Discord]: https://img.shields.io/discord/629491597070827530?logo=discord "chat on discord"
[Link-Discord]: https://discord.gg/2ccFBr4 "Discord"

## License

[License](LICENSE)