exteditor

package
v0.27.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 15 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 exteditor lets a terminal user interface hand a text to the user's own editor in the manner of sudoedit: the text goes to a copy in a private directory, the terminal goes to the editor through tea.ExecProcess, and when the editor has ended the copy is checked, read and removed, and the program gets an UnchangedMsg, a ChangedMsg with the new text or a FailedMsg. Writing the text back to its place, and the rights that takes, stay with the application.

Resolve finds the editor without a shell: the preference, then VISUAL, then EDITOR, and without any of them the first of micro, nano and vi on PATH. Editor.NoTraces tells the application before the start whether the editor writes no backup, swap, undo or history files; for any editor the package does not know it is false and the application decides whether to go on.

Always enforced

Edit refuses an invalid Request with ErrInvalidRequest: the editor path and the private directory must be absolute, MaxSize must be positive, Name must carry no path separator and keep an extension of at most 32 bytes that is valid UTF-8 without control characters, and a Target must name a user and group other than root and an absolute home directory. Resolve splits a command line at spaces only, looks its first part up on PATH and requires an absolute path; a source that is set but cannot be found is an error (ErrNoEditor), not a reason to try the next one. For vim and Neovim, micro and nano Resolve adds the options that switch off backup, swap, undo and history files; nano then skips the nanorc files, since it has no other way to switch off a backup set there.

The private directory is opened without following a symbolic link and checked on the open descriptor: a directory with mode 0700 that belongs to the user the editor runs as (ErrUnsafeDir). The copy is created through that descriptor with O_EXCL, without following a link and with mode 0600; its name is random and keeps only the extension of the original. It is read and removed through the same descriptor, so renaming or replacing the path after the check redirects nothing.

The text is accepted in either direction only up to MaxSize (ErrTooLarge), as valid UTF-8 (ErrNotUTF8) and without NUL bytes (ErrBinary). After the editor, only a regular file with a single link that belongs to the user the editor ran as is read (ErrUnsafeCopy), since the editor may have replaced the copy. The copy is removed in every case, also when the editor fails or is killed, and an error never quotes the text. When Edit itself returns an error, nothing is left in the directory.

With effective user ID 0, Edit refuses (ErrRoot) unless the application names a Target that is not root, and without root a Target is refused. The copy then belongs to the target, and the editor runs with its user and group ID, without supplementary groups, in the private directory and with this environment only: HOME of the target, PATH=/usr/local/bin:/usr/bin:/bin, and TERM, COLORTERM, LANG, LC_ALL, LC_CTYPE and LC_MESSAGES of the process when set. Without root, the editor inherits the environment of the process.

Boundary: the copy exists while the editor runs, so the private directory should be on memory-backed storage such as XDG_RUNTIME_DIR. If the process dies before the editor ends, or the returned command never runs, the copy stays. An editor or its plugins may still write elsewhere. Edit works on Linux and macOS; on other platforms it returns an error that wraps errors.ErrUnsupported.

Options

The package has no options.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupported is returned by Edit on a platform without support.
	ErrUnsupported = fmt.Errorf("the external editor is unsupported on this platform: %w", errors.ErrUnsupported)
	// ErrNoEditor reports that no editor could be resolved.
	ErrNoEditor = errors.New("no editor found")
	// ErrInvalidRequest reports a request that is incomplete or contradicts itself.
	ErrInvalidRequest = errors.New("invalid edit request")
	// ErrRoot reports a process with effective user ID 0 and no target user.
	ErrRoot = errors.New("refusing to run the editor as root; a target user is required")
	// ErrUnsafeDir reports a private directory that is no directory, a symbolic link, belongs to another
	// user or is open to others.
	ErrUnsafeDir = errors.New("unsafe private directory")
	// ErrUnsafeCopy reports a copy the editor left as something other than a private regular file.
	ErrUnsafeCopy = errors.New("unsafe copy")
	// ErrTooLarge reports a text above the size limit.
	ErrTooLarge = errors.New("text too large")
	// ErrBinary reports a text that holds a NUL byte.
	ErrBinary = errors.New("binary content")
	// ErrNotUTF8 reports a text that is not valid UTF-8.
	ErrNotUTF8 = errors.New("text is not valid UTF-8")
	// ErrEditorFailed reports an editor that could not start or did not exit with status 0.
	ErrEditorFailed = errors.New("the editor failed")
)

Functions

func Edit

func Edit(r Request) (tea.Cmd, error)

Edit checks the request, makes the private copy and returns a command that hands the terminal to the editor through tea.ExecProcess. When the editor has ended, the copy is checked, read and removed in every case, and the command yields an UnchangedMsg, a ChangedMsg or a FailedMsg. When Edit returns an error, nothing is left in Dir. The application runs the returned command exactly once; until then the copy stays in Dir.

Types

type ChangedMsg

type ChangedMsg struct {
	Text []byte
}

ChangedMsg carries the changed text. It passed the size limit, is valid UTF-8 and holds no NUL byte.

type Editor

type Editor struct {
	// Path is the absolute path of the program.
	Path string
	// Args come before the file name; for a known editor they start with the options that switch off its
	// backup, swap, undo and history files.
	Args []string
	// NoTraces reports that Args switch off the files a known editor writes beside or about the text.
	// It is false for an editor this package does not know; the application decides whether to go on.
	NoTraces bool
}

Editor is a resolved editor command line.

func Resolve

func Resolve(preference string, getenv func(string) string) (Editor, error)

Resolve finds the editor: the preference, then VISUAL, then EDITOR, read through getenv (os.Getenv when nil). The first of them that is set decides; one that cannot be found is an error, not a reason to try the next. When none is set, the first of micro, nano and vi on the PATH of the process is used. A command line is split at spaces only, without a shell; its first part is looked up on PATH and must resolve to an absolute path.

type FailedMsg

type FailedMsg struct {
	Err error
}

FailedMsg reports a failed edit; any change is discarded. Err wraps ErrEditorFailed, ErrUnsafeCopy, ErrTooLarge, ErrBinary or ErrNotUTF8, or reports that the copy could not be read or removed.

type Request

type Request struct {
	Editor Editor
	// Dir is the absolute private directory the copy is made in, such as XDG_RUNTIME_DIR. It must belong
	// to the user the editor runs as, have mode 0700 and not be a symbolic link.
	Dir string
	// Name is the name of the original file; only its extension is kept, so the editor recognizes the
	// format. It must not contain a path separator.
	Name string
	// Text is the content to edit; it must be valid UTF-8 without NUL bytes and at most MaxSize bytes.
	Text []byte
	// MaxSize is the limit in bytes for the text in both directions; it must be positive.
	MaxSize int64
	// Target is required when the process has effective user ID 0 and refused otherwise.
	Target *Target
	// contains filtered or unexported fields
}

Request describes one edit.

type Target

type Target struct {
	UID, GID uint32
	// Home is the absolute home directory of the user; the editor gets it as HOME.
	Home string
}

Target is the user the editor runs as when the process has effective user ID 0. Neither ID may be 0.

type UnchangedMsg

type UnchangedMsg struct{}

UnchangedMsg reports that the editor ended with status 0 and the text unchanged.

Jump to

Keyboard shortcuts

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