gorun

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2025 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/cdvelop/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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KillAllByName added in v0.0.4

func KillAllByName(executableName string) 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 added in v0.0.7

type Config struct {
	ExecProgramPath string          // eg: "server/main.exe"
	RunArguments    func() []string // eg: []string{"dev"}
	ExitChan        chan bool
	Logger          io.Writer
	KillAllOnStop   bool   // If true, kills all instances of the executable when stopping
	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 added in v0.0.4

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 added in v0.0.4

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

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

type SafeBuffer added in v0.0.5

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

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

func NewSafeBuffer added in v0.0.5

func NewSafeBuffer() *SafeBuffer

NewSafeBuffer creates a new thread-safe buffer

func NewSafeBufferWithForward added in v0.0.5

func NewSafeBufferWithForward(forward io.Writer) *SafeBuffer

NewSafeBufferWithForward creates a new thread-safe buffer that forwards writes

func (*SafeBuffer) Len added in v0.0.5

func (sb *SafeBuffer) Len() int

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

func (*SafeBuffer) Reset added in v0.0.5

func (sb *SafeBuffer) Reset()

Reset resets the buffer in a thread-safe manner

func (*SafeBuffer) String added in v0.0.5

func (sb *SafeBuffer) String() string

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

func (*SafeBuffer) Write added in v0.0.5

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