Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions io/shared/src/main/scala/fs2/io/process/ProcessBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ sealed abstract class ProcessBuilder private {
*/
def workingDirectory: Option[Path]

/** Configures how stdout and stderr should be handled. */
def outputMode: ProcessOutputMode

/** @see [[command]] */
def withCommand(command: String): ProcessBuilder

Expand All @@ -67,17 +70,29 @@ sealed abstract class ProcessBuilder private {
/** @see [[workingDirectory]] */
def withCurrentWorkingDirectory: ProcessBuilder

/** @see [[outputMode]] */
def withOutputMode(outputMode: ProcessOutputMode): ProcessBuilder

/** Starts the process and returns a handle for interacting with it.
* Closing the resource will kill the process if it has not already terminated.
*/
final def spawn[F[_]: Processes]: Resource[F, Process[F]] =
Processes[F].spawn(this)
}

sealed trait ProcessOutputMode
object ProcessOutputMode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like the right set of options! We need to support stdin, stdout, and stderr to be configured separately.

case object Separate extends ProcessOutputMode // stdout and stderr are separate
case object Merged extends ProcessOutputMode // stderr is redirected to stdout
case class FileOutput(path: Path) extends ProcessOutputMode // Output to file
case object Inherit extends ProcessOutputMode // Inherit parent process's streams
case object Ignore extends ProcessOutputMode // Discard output
}

object ProcessBuilder {

def apply(command: String, args: List[String]): ProcessBuilder =
ProcessBuilderImpl(command, args, true, Map.empty, None)
ProcessBuilderImpl(command, args, true, Map.empty, None, ProcessOutputMode.Separate)

def apply(command: String, args: String*): ProcessBuilder =
apply(command, args.toList)
Expand All @@ -87,7 +102,8 @@ object ProcessBuilder {
args: List[String],
inheritEnv: Boolean,
extraEnv: Map[String, String],
workingDirectory: Option[Path]
workingDirectory: Option[Path],
outputMode: ProcessOutputMode
) extends ProcessBuilder {

def withCommand(command: String): ProcessBuilder = copy(command = command)
Expand All @@ -100,7 +116,9 @@ object ProcessBuilder {

def withWorkingDirectory(workingDirectory: Path): ProcessBuilder =
copy(workingDirectory = Some(workingDirectory))

def withCurrentWorkingDirectory: ProcessBuilder = copy(workingDirectory = None)
}

def withOutputMode(outputMode: ProcessOutputMode): ProcessBuilder = copy(outputMode = outputMode)
}
}
Loading