Documentation ¶
Overview ¶
Package edit allows edit files. It can create automatically a backup before of editing.
During tests, can be used the next tags:
-tags=debug
Show data before and after of edit at case of failure.
-tags=log
Run tests with logging.
Index ¶
- Variables
- func Append(filename string, src []byte, data string, opt *Options) error
- func Backup(fname string) (fbackup string, err error)
- func Comment(filename string, src []byte, reLine string, opt *Options) error
- func CommentM(filename string, src []byte, reLine []string, opt *Options) error
- func CommentOut(filename string, src []byte, reLine string, opt *Options) error
- func CommentOutM(filename string, src []byte, reLine []string, opt *Options) error
- func Delete(filename string, src []byte, begin, end int64, opt *Options) error
- func Insert(filename string, src []byte, data string, opt *Options) error
- func Replace(filename string, src []byte, r []Replacer) error
- func ReplaceAtLine(filename string, src []byte, r []ReplacerAtLine) error
- func ReplaceAtLineN(filename string, src []byte, r []ReplacerAtLine, n int) error
- func ReplaceN(filename string, src []byte, r []Replacer, n int) error
- type Editor
- func (ed *Editor) Append(b []byte) error
- func (ed *Editor) AppendString(s string) error
- func (ed *Editor) Close() error
- func (ed *Editor) Comment(reLine []string) error
- func (ed *Editor) CommentOut(reLine []string) error
- func (ed *Editor) Delete(begin, end int64) error
- func (ed *Editor) FileBackup() string
- func (ed *Editor) Insert(b []byte) error
- func (ed *Editor) InsertString(s string) error
- func (ed *Editor) Replace(r []Replacer) error
- func (ed *Editor) ReplaceAtLine(r []ReplacerAtLine) error
- func (ed *Editor) ReplaceAtLineN(r []ReplacerAtLine, n int) error
- func (ed *Editor) ReplaceN(r []Replacer, n int) error
- type Options
- type Replacer
- type ReplacerAtLine
Constants ¶
This section is empty.
Variables ¶
var Log *log.Logger
Log is the logger by default.
Functions ¶
func Append ¶
Append writes the content of `data` at the end of named file or `src`. If `Options` is null, uses the values by default. It returns an error, if any.
func Backup ¶
Backup creates a backup of the named file. Returns the backup file name.
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.
Uses `Log` to write a message.
func Comment ¶
Comment inserts the comment character in lines that mach the regular expression in reLine, at the named file. If `Options` is null, uses the values by default.
func CommentM ¶
CommentM inserts the comment character in lines that mach any regular expression in reLine, at the named file. If `Options` is null, uses the values by default.
func CommentOut ¶
CommentOut removes the comment character of lines that mach the regular expression in reLine, at the named file. If `Options` is null, uses the values by default.
func CommentOutM ¶
CommentOutM removes the comment character of lines that mach any regular expression in reLine, at the named file. If `Options` is null, uses the values by default.
func Delete ¶
Delete removes the text given at position 'begin:end'. If `Options` is null, uses the values by default.
func Insert ¶ added in v1.2.0
Insert writes the content of `data` at the start of the named file or `src`. If `Options` is null, uses the values by default. It returns an error, if any.
func Replace ¶
Replace replaces all regular expressions mathed in `r` for the named file or `src`.
It is equivalent to `ReplaceN` with a count of -1.
func ReplaceAtLine ¶
func ReplaceAtLine(filename string, src []byte, r []ReplacerAtLine) error
ReplaceAtLine replaces all regular expressions mathed in `r` for the named file or `src`, if the line is matched at the first.
It is equivalent to `ReplaceAtLineN` with a count of -1.
func ReplaceAtLineN ¶
func ReplaceAtLineN(filename string, src []byte, r []ReplacerAtLine, n int) error
ReplaceAtLineN replaces a number of regular expressions mathed in `r` for the named file or `src`, 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
Types ¶
type Editor ¶
type Editor struct { Opt *Options // contains filtered or unexported fields }
Editor represents the file to edit.
func NewEditor ¶
NewEditor prepares the 'Editor' for a file at `filename` or data at `src`. If `filename == ""`, uses `src`. If `Options` is null, uses the values by default.
Must use `Close()` to close the file.
func (*Editor) Append ¶
Append writes len(b) bytes at the end of the File. It returns an error, if any.
func (*Editor) AppendString ¶
AppendString is like Append, but writes the contents of string s rather than an array of bytes.
func (*Editor) Comment ¶
Comment inserts the comment character in lines that mach any regular expression in reLine.
func (*Editor) CommentOut ¶
CommentOut removes the comment character of lines that mach any regular expression in reLine.
func (*Editor) FileBackup ¶
FileBackup returns the backup file name, if it was done.
func (*Editor) Insert ¶ added in v1.1.0
Insert writes len(b) bytes at the start of the File. It returns an error, if any.
func (*Editor) InsertString ¶ added in v1.1.0
InsertString is like Insert, but writes the contents of string s rather than an array of bytes.
func (*Editor) Replace ¶
Replace replaces all regular expressions mathed in `r`. It is equivalent to `ReplaceN` with a count of -1.
func (*Editor) ReplaceAtLine ¶
func (ed *Editor) ReplaceAtLine(r []ReplacerAtLine) error
ReplaceAtLine replaces all regular expressions mathed in `r`, if the line is matched at the first. It is equivalent to `ReplaceAtLineN` with a count of -1.
func (*Editor) ReplaceAtLineN ¶
func (ed *Editor) 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
type Options ¶
type Options struct { Comment []byte // Character for comment. Backup bool // Do backup before of edit. }
Options are options to edit a text file.
func NewOptions ¶
func NewOptions() *Options
NewOptions returns the options used by default:
Comment: '#' Backup: true
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.