execgo

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

exec-go

The fleet's one typed wrapper for running external commands — so no tool scatters raw os/exec.Command with ad-hoc output and error handling.

What

A Command built with functional options (WithArgs/WithEnv/WithDir/ WithStdin/WithTimeout), a Run(ctx) that captures a typed Result (stdout/stderr/exit/duration), and RunJSON[T] that decodes stdout into a typed value. A non-zero exit, a failed start, a timeout, and bad JSON each become a typed, code-carrying error via errors-go (exec_nonzero / exec_start / exec_timeout / exec_json).

Why

Wrapping external CLIs (kubectl, helm, git, …) recurs across tools. One typed builder means uniform output capture, uniform timeout handling, and uniform exit-code → errs.Exit mapping — never a hand-rolled exec.Command

  • buffer + if err != nil again.

Install

go get github.com/pleme-io/exec-go

Usage

res, err := execgo.New("kubectl", execgo.WithArgs("get", "ns", "-o", "json"),
    execgo.WithTimeout(10*time.Second)).Run(ctx)
if err != nil { return errs.Exit(err) } // exit_* code → process exit code

list, err := execgo.RunJSON[NamespaceList](ctx,
    execgo.New("kubectl", execgo.WithArgs("get", "ns", "-o", "json")))

Configuration

None — a pure library. Callers that read the command/args/env from config use shikumi-go and pass them via the options.

Release

Pull-model (Go modules): an annotated vX.Y.Z tag is the release; pkg.go.dev indexes it. See the GSDS module delivery FSM.

Documentation

Overview

Package execgo is the fleet's one typed wrapper for running external commands — so no tool scatters raw os/exec.Command with ad-hoc output handling. A Command is built with functional options, Run captures a typed Result (stdout/stderr/exit/duration), and a non-zero exit becomes a typed, severity- and exit-code-carrying error via errors-go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunJSON

func RunJSON[T any](ctx context.Context, c *Command) (T, error)

RunJSON runs the command and decodes its stdout JSON into T.

Types

type Command

type Command struct {
	Name    string
	Args    []string
	Env     []string // appended to os.Environ() unless EnvOnly is set
	EnvOnly bool
	Dir     string
	Stdin   []byte
	Timeout time.Duration // 0 = no timeout (ctx still governs)
}

Command is a typed, options-built external command.

func New

func New(name string, opts ...Option) *Command

New builds a Command.

func (*Command) Run

func (c *Command) Run(ctx context.Context) (Result, error)

Run executes the command, capturing output. A non-zero exit yields a typed *errs error (Severity Error, code "exec_nonzero", carrying the Result via CodeOf-friendly context); ctx cancellation / timeout yields code "exec_timeout".

type Option

type Option func(*Command)

Option configures a Command.

func WithArgs

func WithArgs(args ...string) Option

WithArgs sets the command arguments.

func WithDir

func WithDir(dir string) Option

WithDir sets the working directory.

func WithEnv

func WithEnv(kv ...string) Option

WithEnv appends KEY=VALUE entries to the inherited environment.

func WithEnvOnly

func WithEnvOnly(kv ...string) Option

WithEnvOnly replaces (does not append to) the environment.

func WithStdin

func WithStdin(b []byte) Option

WithStdin supplies stdin bytes.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout bounds the run (in addition to the caller's context).

type Result

type Result struct {
	Stdout   []byte
	Stderr   []byte
	ExitCode int
	Duration time.Duration
}

Result is the typed outcome of a run.

Jump to

Keyboard shortcuts

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