package edi
import "github.com/tredoe/osutil/edi"
Package edi allows edit files.
The editing of files is very important in the shell scripting to working with the configuration files. There are a great number of functions related to it, avoiding to have to use an external command to get the same result and with the advantage of that it creates automatically a backup before of editing.
NewEdit creates a new struct, edit, which has a variable, CommentChar, with a value by default, '#'. That value is the character used in comments.
Index ¶
- Variables
- func Append(filename string, mode ModeEdit, b []byte) error
- func AppendString(filename string, mode ModeEdit, s string) error
- func Backup(filename string) error
- func Comment(filename string, conf *ConfEditer, reLine string) error
- func CommentM(filename string, conf *ConfEditer, reLine []string) error
- func CommentOut(filename string, conf *ConfEditer, reLine string) error
- func CommentOutM(filename string, conf *ConfEditer, reLine []string) error
- func Delete(filename string, begin, end int64) error
- func Replace(filename string, conf *ConfEditer, r []Replacer) error
- func ReplaceAtLine(filename string, conf *ConfEditer, r []ReplacerAtLine) error
- func ReplaceAtLineN(filename string, conf *ConfEditer, r []ReplacerAtLine, n int) error
- func ReplaceN(filename string, conf *ConfEditer, r []Replacer, n int) error
- type ConfEditer
- type Editer
- func NewEdit(filename string, conf *ConfEditer) (*Editer, error)
- func (ed *Editer) Append(b []byte) error
- func (ed *Editer) AppendString(s string) error
- func (ed *Editer) Close() error
- func (ed *Editer) Comment(reLine []string) error
- func (ed *Editer) CommentOut(reLine []string) error
- func (ed *Editer) Delete(begin, end int64) error
- func (ed *Editer) Replace(r []Replacer) error
- func (ed *Editer) ReplaceAtLine(r []ReplacerAtLine) error
- func (ed *Editer) ReplaceAtLineN(r []ReplacerAtLine, n int) error
- func (ed *Editer) ReplaceN(r []Replacer, n int) error
- type Finder
- func NewFinder(filename, comment string, mode ModeFind) (*Finder, error)
- func (fn *Finder) Contains(b []byte) (found bool, err error)
- func (fn *Finder) Filename() string
- func (fn *Finder) HasPrefix(b []byte) (found bool, err error)
- func (fn *Finder) HasSuffix(b []byte) (found bool, err error)
- type ModeEdit
- type ModeFind
- type Replacer
- type ReplacerAtLine
Variables ¶
Logger is the global logger. By default, it does not write logs.
Functions ¶
func Append ¶
Append writes len(b) bytes at the end of the named file. It returns an error, if any.
func AppendString ¶
AppendString is like Append, but writes the contents of string s rather than an array of bytes.
func Backup ¶
Backup creates a backup of the named file.
The schema used for the new name is: {name}\+[1-9]~
name: The original file name. + : Character used to separate the file name from rest. number: A number from 1 to 9, using rotation. ~ : To indicate that it is a backup, just like it is used in Unix systems.
func Comment ¶
func Comment(filename string, conf *ConfEditer, reLine string) error
Comment inserts the comment character in lines that mach the regular expression in reLine, in the named file.
func CommentM ¶
func CommentM(filename string, conf *ConfEditer, reLine []string) error
CommentM inserts the comment character in lines that mach any regular expression in reLine, in the named file.
func CommentOut ¶
func CommentOut(filename string, conf *ConfEditer, reLine string) error
CommentOut removes the comment character of lines that mach the regular expression in reLine, in the named file.
func CommentOutM ¶
func CommentOutM(filename string, conf *ConfEditer, reLine []string) error
CommentOutM removes the comment character of lines that mach any regular expression in reLine, in the named file.
func Delete ¶
Delete removes the text given at position 'begin:end'.
func Replace ¶
func Replace(filename string, conf *ConfEditer, r []Replacer) error
Replace replaces all regular expressions mathed in r for the named file.
func ReplaceAtLine ¶
func ReplaceAtLine(filename string, conf *ConfEditer, r []ReplacerAtLine) error
ReplaceAtLine replaces all regular expressions mathed in r for the named file, if the line is matched at the first.
func ReplaceAtLineN ¶
func ReplaceAtLineN(filename string, conf *ConfEditer, r []ReplacerAtLine, n int) error
ReplaceAtLineN replaces a number of regular expressions mathed in r for the named file, if the line is matched at the first.
func ReplaceN ¶
func ReplaceN(filename string, conf *ConfEditer, r []Replacer, n int) error
ReplaceN replaces a number of regular expressions mathed in r for the named file.
Types ¶
type ConfEditer ¶
ConfEditer represents the editer configuration.
type Editer ¶
type Editer struct {
// contains filtered or unexported fields
}
Editer represents the file to edit.
func NewEdit ¶
func NewEdit(filename string, conf *ConfEditer) (*Editer, error)
NewEdit prepares a file to edit. You must use 'Close()' to close the file.
func (*Editer) Append ¶
Append writes len(b) bytes at the end of the File. It returns an error, if any.
func (*Editer) AppendString ¶
AppendString is like Append, but writes the contents of string s rather than an array of bytes.
func (*Editer) Close ¶
Close closes the file.
func (*Editer) Comment ¶
Comment inserts the comment character in lines that mach any regular expression in reLine.
func (*Editer) CommentOut ¶
CommentOut removes the comment character of lines that mach any regular expression in reLine.
func (*Editer) Delete ¶
Delete removes the text given at position 'begin:end'.
func (*Editer) Replace ¶
Replace replaces all regular expressions mathed in r.
func (*Editer) ReplaceAtLine ¶
func (ed *Editer) ReplaceAtLine(r []ReplacerAtLine) error
ReplaceAtLine replaces all regular expressions mathed in r, if the line is matched at the first.
func (*Editer) ReplaceAtLineN ¶
func (ed *Editer) ReplaceAtLineN(r []ReplacerAtLine, n int) error
ReplaceAtLineN replaces regular expressions mathed in r, if the line is matched at the first. The count determines the number to match:
n > 0: at most n matches n == 0: the result is none n < 0: all matches
func (*Editer) ReplaceN ¶
ReplaceN replaces regular expressions mathed in r. The count determines the number to match:
n > 0: at most n matches n == 0: the result is none n < 0: all matches
type Finder ¶
type Finder struct { Begin int64 // Line begin position where the string was found (if any). End int64 // Line end position where the string was found (if any). // contains filtered or unexported fields }
Finder represents the file where find a string.
func NewFinder ¶
NewFinder prepares the Finder.
func (*Finder) Contains ¶
Contains reports whether the file contains 'b'.
func (*Finder) Filename ¶
Filename returns the file name.
func (*Finder) HasPrefix ¶
HasPrefix reports whether the file has a line that begins with 'b'.
func (*Finder) HasSuffix ¶
HasSuffix reports whether the file has a line that ends with 'b'.
type ModeEdit ¶
type ModeEdit uint
A ModeEdit value is a set of flags (or 0) to control behavior at edit a file.
const ( ModBackup ModeEdit // Do backup before of edit. )
Modes used at edit a file.
type ModeFind ¶
type ModeFind uint
A ModeFind value is a set of flags (or 0) to control behavior at find into a file.
const ( ModTrimSpace ModeFind // Removes all leading and trailing white spaces. ModSkipComment // Skip skip lines that start with the comment string. )
Modes used at find into a file.
type Replacer ¶
type Replacer struct { Search, Replace string }
Replacer represents the text to be replaced.
type ReplacerAtLine ¶
type ReplacerAtLine struct { Line, Search, Replace string }
ReplacerAtLine represents the text to be replaced into a line.
Source Files ¶
backup.go doc.go edit.go find.go log.go replace.go util.go
- Version
- v1.1.3
- Published
- Aug 2, 2021
- Platform
- js/wasm
- Imports
- 9 packages
- Last checked
- 1 day ago –
Tools for package owners.