Tighten process package API contracts
This commit is contained in:
parent
e85abe1ee6
commit
c31f3faa2b
5 changed files with 15 additions and 13 deletions
14
exec/exec.go
14
exec/exec.go
|
|
@ -4,12 +4,12 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
goio "io"
|
||||
)
|
||||
|
||||
// ErrCommandContextRequired is returned when a command is created without a context.
|
||||
|
|
@ -19,9 +19,9 @@ var ErrCommandContextRequired = coreerr.E("", "exec: command context is required
|
|||
type Options struct {
|
||||
Dir string
|
||||
Env []string
|
||||
Stdin io.Reader
|
||||
Stdout io.Writer
|
||||
Stderr io.Writer
|
||||
Stdin goio.Reader
|
||||
Stdout goio.Writer
|
||||
Stderr goio.Writer
|
||||
// Background runs the command asynchronously and returns from Run immediately.
|
||||
Background bool
|
||||
}
|
||||
|
|
@ -74,7 +74,7 @@ func (c *Cmd) WithEnv(env []string) *Cmd {
|
|||
// Example:
|
||||
//
|
||||
// cmd.WithStdin(strings.NewReader("input"))
|
||||
func (c *Cmd) WithStdin(r io.Reader) *Cmd {
|
||||
func (c *Cmd) WithStdin(r goio.Reader) *Cmd {
|
||||
c.opts.Stdin = r
|
||||
return c
|
||||
}
|
||||
|
|
@ -84,7 +84,7 @@ func (c *Cmd) WithStdin(r io.Reader) *Cmd {
|
|||
// Example:
|
||||
//
|
||||
// cmd.WithStdout(os.Stdout)
|
||||
func (c *Cmd) WithStdout(w io.Writer) *Cmd {
|
||||
func (c *Cmd) WithStdout(w goio.Writer) *Cmd {
|
||||
c.opts.Stdout = w
|
||||
return c
|
||||
}
|
||||
|
|
@ -94,7 +94,7 @@ func (c *Cmd) WithStdout(w io.Writer) *Cmd {
|
|||
// Example:
|
||||
//
|
||||
// cmd.WithStderr(os.Stderr)
|
||||
func (c *Cmd) WithStderr(w io.Writer) *Cmd {
|
||||
func (c *Cmd) WithStderr(w goio.Writer) *Cmd {
|
||||
c.opts.Stderr = w
|
||||
return c
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,8 @@ func (NopLogger) Debug(string, ...any) {}
|
|||
// Error discards the message (no-op implementation).
|
||||
func (NopLogger) Error(string, ...any) {}
|
||||
|
||||
var _ Logger = NopLogger{}
|
||||
|
||||
var defaultLogger Logger = NopLogger{}
|
||||
|
||||
// SetDefaultLogger sets the package-level default logger.
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package process
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
|
|
@ -11,6 +10,7 @@ import (
|
|||
"time"
|
||||
|
||||
coreerr "dappco.re/go/core/log"
|
||||
goio "io"
|
||||
)
|
||||
|
||||
// ManagedProcess represents a managed external process.
|
||||
|
|
@ -33,7 +33,7 @@ type ManagedProcess struct {
|
|||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
output *RingBuffer
|
||||
stdin io.WriteCloser
|
||||
stdin goio.WriteCloser
|
||||
done chan struct{}
|
||||
mu sync.RWMutex
|
||||
gracePeriod time.Duration
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ var (
|
|||
)
|
||||
|
||||
// Default returns the global process service.
|
||||
// Returns nil if not initialized.
|
||||
// Returns nil if not initialised.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
|
|
@ -219,7 +219,7 @@ func Clear() {
|
|||
|
||||
// Errors
|
||||
var (
|
||||
// ErrServiceNotInitialized is returned when the service is not initialized.
|
||||
// ErrServiceNotInitialized is returned when the service is not initialised.
|
||||
ErrServiceNotInitialized = coreerr.E("", "process: service not initialized; call process.Init(core) first", nil)
|
||||
// ErrSetDefaultNil is returned when SetDefault is called with nil.
|
||||
ErrSetDefaultNil = coreerr.E("", "process: SetDefault called with nil service", nil)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"sort"
|
||||
"sync"
|
||||
|
|
@ -15,6 +14,7 @@ import (
|
|||
|
||||
"dappco.re/go/core"
|
||||
coreerr "dappco.re/go/core/log"
|
||||
goio "io"
|
||||
)
|
||||
|
||||
// Default buffer size for process output (1MB).
|
||||
|
|
@ -313,7 +313,7 @@ func (s *Service) StartWithOptions(ctx context.Context, opts RunOptions) (*Proce
|
|||
}
|
||||
|
||||
// streamOutput reads from a pipe and broadcasts lines via ACTION.
|
||||
func (s *Service) streamOutput(proc *Process, r io.Reader, stream Stream) {
|
||||
func (s *Service) streamOutput(proc *Process, r goio.Reader, stream Stream) {
|
||||
scanner := bufio.NewScanner(r)
|
||||
// Increase buffer for long lines
|
||||
scanner.Buffer(make([]byte, 64*1024), 1024*1024)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue