Documentation
¶
Overview ¶
Package textedit provides helpers for making changes to line-oriented text files. This is especially targeted at "shell rc" files like `~/.bashrc` or `~/.zshrc`.
Index ¶
- func Edit(in io.Reader, out io.Writer, editor Editor) error
- func EditFile(fileName string, editor Editor) (bool, error)
- func EditFileAsRoot(ctx context.Context, fileName string, editor Editor) (bool, error)
- func EditFileUnsafe(fileName string, editor Editor) (bool, error)
- func ReplaceLine(oldLine, newLine string) *replaceLineEditor
- type Editor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EditFileAsRoot ¶ added in v0.7.0
func EditFileUnsafe ¶ added in v0.15.1
EditFileUnsafe is like EditFile, except that it writes directly to the output file instead of to a temp file and renaming.
It does still compute the new output and avoid rewrites if no changes are necessary.
Only use this for scenarios where permissions prevent doing the temp file dance.
func ReplaceLine ¶ added in v0.7.0
func ReplaceLine(oldLine, newLine string) *replaceLineEditor
ReplaceLine makes an editor that will replace oldLine with newLine, with optional prior lines that must immediately precede old/new in order.
This editor is stateful and can only be used once.
Types ¶
type Editor ¶
type Editor interface {
// Next is called after each input line is read. If err is non-nil, the edit
// operation will fail. Otherwise any lines in output will be emitted. This
// must include the input line if it should be copied to the output.
Next(line string) (output iter.Seq[string], err error)
// EOF is called after all input lines have been processed through Next. Its
// return will be processed the same way as Next.
EOF() (output iter.Seq[string], err error)
}
func AppendLine ¶
AppendLine adds the given line to the end of the file if it is not already present. It will also insert it in place of any of the given oldLines (optional).
It will panic if the line or any of the oldLines are empty after trimming leading & trailing whitespace.
func SpliceRange ¶
Splice a range of lines marked by the start and end lines. Panics if len(lines) < 3, i.e. must have start & end markers and at least one "content" line, or if the start or end markers are empty after trimming leading & trailing whitespace.
Matching the start and end markers is done after trimming leading & trailing whitespace from both the sought & observed lines.