Categorygithub.com/bobcatalyst/subflow
repositorypackage
0.0.2
Repository: https://github.com/bobcatalyst/subflow.git
Documentation: pkg.go.dev

# README

Subflow

Subflow is a Go library designed for managing and executing subprocesses. It provides an interface to define commands, manage input/output streams, and handle subprocess lifecycle events synchronously and asynchronously.


Installation

Install Subflow using go get:

go get github.com/bobcatalyst/subflow

Import it in your Go code:

import "github.com/bobcatalyst/subflow"

Quick Start

Define a Command

Create a basic command:

cmd := subflow.NewCommand("ls")

Add arguments and environment variables:

cmdArgsEnv := subflow.NewCommandArgsEnv("ls", []string{"-l", "-a"}, []string{"PATH=/usr/bin"})

Run a Command

Execute a command and get output:

ctx := context.Background()
output := subflow.Run(ctx, cmdArgsEnv, nil)
fmt.Printf("Stdout: %s\n", string(output.Stdout()))
fmt.Printf("Stderr: %s\n", string(output.Stderr()))

Manage Subprocesses

Asynchronous subprocess management:

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

subCmd, err := subflow.New(ctx, cmdArgsEnv)
if err != nil {
    log.Fatalf("failed to create command: %v", err)
}
defer subCmd.Close()

go func() {
    for msg := range subCmd.Listen(ctx) {
        fmt.Printf("Message: %v\n", msg)
    }
}()

subCmd.Start()
<-subCmd.Wait()

Streaming Input

Push input data to a running command:

subCmd.Push(subflow.NewInputln("example input"))