engine

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 17 Imported by: 0 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.

Documentation

Overview

Package engine applies a project's own formatters to a single file, safely.

The design rule that everything else follows from: a third-party tool is never given the user's file. Content is piped in on stdin and read back on stdout, the chain is evaluated as a pure function, and onwrite performs one atomic write at the end only if every formatter in the chain succeeded.

That single decision removes most of the ways a format-on-write tool can destroy data:

  • A truncated, half-written file does not parse, so the formatter exits non-zero, so nothing is written and the original survives untouched.
  • A formatter that crashes or is killed mid-run cannot leave a partial file behind, because it never held a file handle on one.
  • A chain that fails at step three does not leave step two's output on disk, because nothing is on disk until the chain finishes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTempName

func IsTempName(base string) bool

IsTempName reports whether a basename is one of onwrite's own scratch files.

Types

type Config

type Config struct {
	// MaxSize is the largest file onwrite will format. Beyond this the cost
	// stops being invisible and the file is almost certainly generated.
	MaxSize int64
	// Timeout bounds a single tool invocation.
	Timeout time.Duration
	// DryRun computes the formatted result but never writes.
	DryRun bool
	// Log receives progress.
	Log Logger
}

Config tunes the engine.

type Engine

type Engine struct {
	Root string
	Proj *detect.Project
	Ign  *scan.Ignorer
	// contains filtered or unexported fields
}

Engine formats files for one project root.

func New

func New(root string, proj *detect.Project, ign *scan.Ignorer, cfg Config) *Engine

New builds an Engine.

func (*Engine) Forget

func (e *Engine) Forget(path string)

Forget exposes forget to other packages in the module.

func (*Engine) Format

func (e *Engine) Format(ctx context.Context, path string) Result

Format runs the project's formatters over one file and, if the result differs, replaces the file atomically.

func (*Engine) ToolsFor

func (e *Engine) ToolsFor(path string) []detect.Resolved

ToolsFor returns the tools that would run on a path, in order. Used by doctor and by callers deciding whether a file is worth watching at all.

type Logger

type Logger interface {
	Debugf(format string, args ...any)
	Infof(format string, args ...any)
	Errorf(format string, args ...any)
}

Logger receives human-readable progress. A nil Logger discards everything.

type Result

type Result struct {
	Path     string
	Changed  bool
	Skip     Skip
	Tools    []string
	Err      error
	Duration time.Duration
}

Result reports what happened to one file.

func (Result) OK

func (r Result) OK() bool

OK reports whether the file was handled without a tool failure.

type Skip

type Skip string

Skip explains why a file was not formatted. A skip is never an error; it is the normal outcome for most files onwrite is asked about.

const (
	SkipNone      Skip = ""
	SkipIgnored   Skip = "ignored"
	SkipNoTool    Skip = "no formatter for this file type"
	SkipTooLarge  Skip = "file too large"
	SkipBinary    Skip = "binary file"
	SkipSelfWrite Skip = "onwrite wrote this content"
	SkipNotFile   Skip = "not a regular file"
	SkipSymlink   Skip = "symlink"
	SkipEmpty     Skip = "empty file"
	SkipUnstable  Skip = "still being written"
)

type TimeoutError

type TimeoutError struct {
	Tool  string
	After time.Duration
}

TimeoutError is returned when a tool exceeds the per-invocation budget.

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Jump to

Keyboard shortcuts

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