output

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package output provides machine-readable output for pgtransfer.

By default pgtransfer prints human-friendly, coloured text. When the global --json flag is set (or PGTRANSFER_JSON=1 is present in the environment), the commands emit a single structured JSON object on stdout instead, so that scripts, CI pipelines and AI agents can parse results reliably without scraping prose.

The JSON contract is intentionally small and stable:

{
  "ok": true,
  "command": "export csv",
  "data": { ... command-specific fields ... },
  "error": null
}

On failure:

{
  "ok": false,
  "command": "export csv",
  "data": null,
  "error": {"code": "connection_failed", "message": "..."}
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Configure

func Configure(jsonFlag bool)

Configure applies explicit settings, typically called once from the root command. When both forceJSON and the environment agree, json wins.

func EnableMCPMode

func EnableMCPMode() *os.File

EnableMCPMode switches the process into MCP server mode.

When pgtransfer speaks the Model Context Protocol over stdio, stdout is reserved for protocol frames: a single stray byte (a progress message, an ANSI escape) corrupts the stream and the client fails to parse it. Third-party libraries, however, print progress messages straight to os.Stdout.

EnableMCPMode therefore redirects the process-level os.Stdout to stderr for the lifetime of the MCP server, so any such output becomes harmless diagnostics. The caller must keep a reference to the real stdout (returned here) and hand it to the MCP transport.

func IsInteractive

func IsInteractive() bool

IsInteractive reports whether writes to stdout are attached to a terminal. Agents and pipelines generally are not, which lets commands suppress spinners and colour automatically.

func IsJSON

func IsJSON() bool

IsJSON reports whether JSON output is currently enabled. It also honours the PGTRANSFER_JSON environment variable when the mode has not been set explicitly, so agents can simply export the variable.

func IsMCP

func IsMCP() bool

IsMCP reports whether the process is running as an MCP server.

func PrintErrorLine

func PrintErrorLine(err error)

PrintErrorLine writes a human-readable error to stderr. It is a no-op in JSON mode, where the error has already been emitted as part of the result object.

func Reset

func Reset()

Reset restores the default state. Intended for tests.

func SetJSON

func SetJSON(enabled bool)

SetJSON is a convenience wrapper around SetMode.

func SetMode

func SetMode(m Mode)

SetMode forces the output mode. Primarily used by tests and by the root command once flags have been parsed.

func SetWriters

func SetWriters(out, errW io.Writer)

SetWriters overrides the output destinations. Intended for tests.

func Success

func Success(command string, data interface{})

Success emits a successful result. In text mode, data is ignored because the caller has already printed human-friendly output; in JSON mode it is serialised to stdout.

func WriteJSON

func WriteJSON(v interface{}) error

WriteJSON serialises v to stdout. Used for commands whose entire output is inherently structured (e.g. profile list).

Types

type Error

type Error struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

Error is a structured, machine-readable error.

func Fail

func Fail(command, code, format string, args ...interface{}) *Error

Fail emits a failed result in JSON mode and returns the structured error so the caller can propagate it. Emission happens at most once per process: a command body that reports a failure prevents the top-level handler from emitting a second, less specific error.

func NewError

func NewError(code, format string, args ...interface{}) *Error

NewError builds a structured error.

func (*Error) Error

func (e *Error) Error() string

type Mode

type Mode int

Mode controls how command results are rendered.

const (
	// ModeText is the default, human-friendly output.
	ModeText Mode = iota
	// ModeJSON emits a single machine-readable JSON object.
	ModeJSON
)

type Result

type Result struct {
	OK      bool        `json:"ok"`
	Command string      `json:"command"`
	Data    interface{} `json:"data,omitempty"`
	Error   *Error      `json:"error"`
}

Result is the envelope emitted by every command in JSON mode.

Jump to

Keyboard shortcuts

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