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

Functions

func Append

func Append(filename string, mode ModeEdit, b []byte) error

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

func AppendString

func AppendString(filename string, mode ModeEdit, s string) error

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

func Backup

func Backup(filename string) error

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

func Delete(filename string, begin, end int64) error

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

type ConfEditer struct {
	Comment []byte
	Mode    ModeEdit
}

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

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

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

func (*Editer) AppendString

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

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

func (*Editer) Close

func (ed *Editer) Close() error

Close closes the file.

func (*Editer) Comment

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

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

func (*Editer) CommentOut

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

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

func (*Editer) Delete

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

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

func (*Editer) Replace

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

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

func (ed *Editer) 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 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

func NewFinder(filename, comment string, mode ModeFind) (*Finder, error)

NewFinder prepares the Finder.

func (*Finder) Contains

func (fn *Finder) Contains(b []byte) (found bool, err error)

Contains reports whether the file contains 'b'.

func (*Finder) Filename

func (fn *Finder) Filename() string

Filename returns the file name.

func (*Finder) HasPrefix

func (fn *Finder) HasPrefix(b []byte) (found bool, err error)

HasPrefix reports whether the file has a line that begins with 'b'.

func (*Finder) HasSuffix

func (fn *Finder) HasSuffix(b []byte) (found bool, err error)

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 replace.go util.go

Version
v1.3.0
Published
Aug 17, 2021
Platform
js/wasm
Imports
10 packages
Last checked
1 day ago

Tools for package owners.