Documentation
¶
Overview ¶
Package valueinput resolves the value a write command takes: from stdin (--value-stdin), from a positional argument, or from $EDITOR when the session is interactive. The direct create/update commands and the stage add/edit commands share it, so a non-interactive session never waits on an editor.
Index ¶
Constants ¶
const FlagValueStdin = "value-stdin"
FlagValueStdin is the name of the flag that reads a create/update value from stdin instead of a positional argument, keeping the secret out of argv (and therefore out of ps/proc/cmdline and shell history).
Variables ¶
var ErrValueRequired = errors.New( "value is required: pass it as an argument, pipe it in with --" + FlagValueStdin + ", or run interactively to edit it in $EDITOR", )
ErrValueRequired is returned when no value was provided and the editor fallback is unavailable because the session is non-interactive (a pipe, CI, or any non-TTY stdin). It never hangs waiting on an editor.
var ErrValueStdinNeedsYes = errors.New( "--" + FlagValueStdin + " consumes stdin, so the confirmation prompt cannot be read; " + "re-run with --yes to acknowledge the update", )
ErrValueStdinNeedsYes is returned when the value was read from stdin via --value-stdin but a confirmation prompt would still run afterwards. The prompt reads from the same stdin, which has already been consumed, so it would immediately hit EOF. Rather than fail cryptically, we detect this up front and tell the user to re-run with --yes. We intentionally neither imply --yes nor read the confirmation from /dev/tty, so the acknowledgement stays explicit.
Functions ¶
func CheckEditorFallback ¶
func CheckEditorFallback(src ValueSource) error
CheckEditorFallback returns ErrValueRequired when src names no value (no --value-stdin, no argument) and the $EDITOR fallback cannot run. The real $EDITOR is a blocking, interactive program: launching it under a pipe or in CI would hang forever, so it runs only when stdin is a TTY. An injected OpenEditor (tests) is exempt. Callers that must do slow work before ResolveValue (e.g. fetch the value to edit) call it first to fail early.
func ResolveValue ¶
ResolveValue determines the value for a create/update or stage add/edit command. Precedence:
- --value-stdin: read the whole of stdin (one trailing newline trimmed).
- the positional value argument, when supplied.
- $EDITOR fallback: open EditorInitial (usually empty) and use whatever is saved. A non-interactive stdin fails with ErrValueRequired instead.
proceed reports whether the command should continue. It is false only when the editor fallback returns an empty value, which is treated as a cancellation (matching the staging add/edit UX). --value-stdin and the positional argument always proceed, even with an empty value, because those are explicit.
When ConfirmRequired is set alongside --value-stdin, ResolveValue returns ErrValueStdinNeedsYes instead of reading stdin, because the later confirmation prompt would find stdin already consumed.
func ValueStdin ¶
ValueStdin returns the command's configured reader, falling back to os.Stdin when none is set. The production app leaves Reader unset, so this preserves the real stdin there while letting tests inject a reader through cmd.Root().Reader.
func ValueStdinFlag ¶
ValueStdinFlag returns the shared --value-stdin flag used by the direct create/update and stage add/edit commands across every provider.
Types ¶
type ValueSource ¶
type ValueSource struct {
// FromStdin is true when --value-stdin was given.
FromStdin bool
// HasArg is true when a positional value argument was supplied.
HasArg bool
// Arg is the positional value argument (only meaningful when HasArg).
Arg string
// Stdin is the reader used when FromStdin is true (nil -> os.Stdin).
Stdin io.Reader
// OpenEditor is the editor seam used for the fallback path (nil -> editor.Open).
OpenEditor editor.OpenFunc
// EditorInitial is the text the editor fallback opens with (e.g. the staged
// draft or the current value); empty opens a blank buffer.
EditorInitial string
// ConfirmRequired is true when the command would prompt for confirmation on
// the same stdin after resolving the value (i.e. an update without --yes).
// Combined with FromStdin this is the double-consume case, so ResolveValue
// fails with ErrValueStdinNeedsYes instead of letting the later prompt hit
// EOF.
ConfirmRequired bool
}
ValueSource describes where a create/update value may come from. Exactly one of the non-editor sources is selected by ResolveValue's precedence rules.