edit

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Edit

Package edit allows edit files.
It can create automatically a backup before of editing.

Documentation online

Author

Jonás Melián (https://bitbucket.org/ares)

License

The source files are distributed under the Apache License, version 2.0.

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

Constants

This section is empty.

Variables

Log is the logger by default.

Functions

func Append

func Append(filename string, src []byte, data string, opt *Options) error

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

func Backup(fname string) (fbackup string, err error)

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

func Comment(filename string, src []byte, reLine string, opt *Options) error

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

func CommentM(filename string, src []byte, reLine []string, opt *Options) error

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

func CommentOut(filename string, src []byte, reLine string, opt *Options) error

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

func CommentOutM(filename string, src []byte, reLine []string, opt *Options) error

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

func Delete(filename string, src []byte, begin, end int64, opt *Options) error

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

func Insert(filename string, src []byte, data string, opt *Options) error

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

func Replace(filename string, src []byte, r []Replacer) error

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

func ReplaceN

func ReplaceN(filename string, src []byte, r []Replacer, n int) error

ReplaceN replaces a number of regular expressions mathed in `r` for the named file or `src`.

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

func NewEditor(filename string, src []byte, opt *Options,
) (ed *Editor, err error)

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

func (ed *Editor) Append(b []byte) error

Append writes len(b) bytes at the end of the File. It returns an error, if any.

func (*Editor) AppendString

func (ed *Editor) AppendString(s string) error

AppendString is like Append, but writes the contents of string s rather than an array of bytes.

func (*Editor) Close

func (ed *Editor) Close() error

Close closes the file. Uses `Log` to write a message.

func (*Editor) Comment

func (ed *Editor) Comment(reLine []string) error

Comment inserts the comment character in lines that mach any regular expression in reLine.

func (*Editor) CommentOut

func (ed *Editor) CommentOut(reLine []string) error

CommentOut removes the comment character of lines that mach any regular expression in reLine.

func (*Editor) Delete

func (ed *Editor) Delete(begin, end int64) error

Delete removes the text given at position 'begin:end'.

func (*Editor) FileBackup

func (ed *Editor) FileBackup() string

FileBackup returns the backup file name, if it was done.

func (*Editor) Insert added in v1.1.0

func (ed *Editor) Insert(b []byte) error

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

func (ed *Editor) InsertString(s string) error

InsertString is like Insert, but writes the contents of string s rather than an array of bytes.

func (*Editor) Replace

func (ed *Editor) Replace(r []Replacer) error

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

func (*Editor) ReplaceN

func (ed *Editor) ReplaceN(r []Replacer, n int) error

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 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.

Jump to

Keyboard shortcuts

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