gorun

package module
v0.0.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: MIT Imports: 11 Imported by: 0

README

gorun

Project Badges

gorun is a Go package that provides a simple interface to run, monitor, and stop external programs, capturing their output and handling graceful shutdowns.

Usage

Installation

gorun

Small helper to run/monitor/stop external programs from Go.

Usage (essential)

Install:

go get github.com/tinywasm/gorun

Minimal example (use WorkingDir when child needs a specific CWD):

cfg := &gorun.Config{
    ExecProgramPath: "./my-server",
    WorkingDir:      "/abs/path/to/project/pwa", // optional
    ExitChan:        make(chan bool),
}
r := gorun.New(cfg)
_ = r.RunProgram()
// ... stop when needed
_ = r.StopProgram()
// By default gorun captures output internally. For programmatic access in
// tests prefer not to rely on exported getters; pass a `Logger` (io.Writer)
// to receive forwarded output, or inspect the internal buffer from tests.

Notes

  • WorkingDir: optional; if empty child inherits parent's CWD. Prefer absolute paths.
  • Logger: optional io.Writer. gorun captures output internally; use GetOutput() in tests or when you need programmatic access.

Tests

cd gorun
go test ./... -v
go test ./... -race -v  # run with race detector

That's it — small, focused, non-redundant docs. If you want, I can add one short goserver example showing how to set WorkingDir from an AutoConfig.

These tests exercise WorkingDir handling and cleanup behaviors.

Contributing

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KillAllByName

func KillAllByName(executableName string, disableGlobal bool) error

KillAllByName kills all running processes that match the given executable name This is useful for cleanup when multiple instances might be running

Types

type Config

type Config struct {
	ExecProgramPath      string          // eg: "server/main.exe"
	RunArguments         func() []string // eg: []string{"dev"}
	ExitChan             chan bool
	Logger               func(message ...any)
	KillAllOnStop        bool   // If true, kills all instances of the executable when stopping
	DisableGlobalCleanup bool   // If true, disables global cleanup (pgrep -f) even if KillAllOnStop is true
	WorkingDir           string // eg: "/path/to/working/dir"
}

type GoRun

type GoRun struct {
	*Config
	Cmd *exec.Cmd
	// contains filtered or unexported fields
}

func New

func New(c *Config) *GoRun

func (*GoRun) GetPID

func (h *GoRun) GetPID() int

GetPID returns the process ID if the program is running, otherwise returns 0

func (*GoRun) IsRunning

func (h *GoRun) IsRunning() bool

IsRunning returns whether the program is currently running

func (*GoRun) RunProgram

func (h *GoRun) RunProgram() error

func (*GoRun) StopProgram

func (h *GoRun) StopProgram() error

func (*GoRun) StopProgramAndCleanup

func (h *GoRun) StopProgramAndCleanup(killAll bool) error

StopProgramAndCleanup stops the current program and optionally kills all instances of the same executable name

type SafeBuffer

type SafeBuffer struct {
	// contains filtered or unexported fields
}

SafeBuffer provides thread-safe operations on a bytes.Buffer and optionally forwards writes to a function logger

func NewSafeBuffer

func NewSafeBuffer() *SafeBuffer

NewSafeBuffer creates a new thread-safe buffer

func NewSafeBufferWithForward

func NewSafeBufferWithForward(forward func(message ...any)) *SafeBuffer

NewSafeBufferWithForward creates a new thread-safe buffer that forwards writes

func (*SafeBuffer) Len

func (sb *SafeBuffer) Len() int

Len returns the length of the buffer in a thread-safe manner

func (*SafeBuffer) Reset

func (sb *SafeBuffer) Reset()

Reset resets the buffer in a thread-safe manner

func (*SafeBuffer) String

func (sb *SafeBuffer) String() string

String returns the contents of the buffer as a string in a thread-safe manner

func (*SafeBuffer) Write

func (sb *SafeBuffer) Write(p []byte) (n int, err error)

Write writes data to the buffer and optionally forwards to another writer

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL