file

package
v0.0.0-...-d459dd1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package file handles common operations in 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

Constants

View Source
const (
	R perm // read
	W      // write
	X      // execute
)

permissions

View Source
const PREFIX_TEMP = "test-" // Prefix to add to temporary files.

Variables

This section is empty.

Functions

func Append

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

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

func AppendString

func AppendString(filename, 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, 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, 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, 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, reLine []string) error

CommentOutM removes the comment character of lines that mach any regular expression in reLine, in the named file.

func Contain

func Contain(filename string, b []byte) (bool, error)

Contain returns whether the named file contains the byte slice b. The return value is a boolean.

func ContainString

func ContainString(filename, s string) (bool, error)

ContainString returns whether the named file contains the string s. The return value is a boolean.

func Copy

func Copy(source, dest string) (err error)

Copy copies file in source to file in dest preserving the mode attributes.

func CopytoTemp

func CopytoTemp(source, prefix string) (tmpFile string, err error)

CopytoTemp creates a temporary file from the source file into the default directory for temporary files (see os.TempDir), whose name begins with prefix. If prefix is the empty string, uses the default value PREFIX_TEMP. Returns the temporary file name.

func Create

func Create(filename string, b []byte) (err error)

Create creates a new file with b bytes.

func CreateString

func CreateString(filename, s string) error

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

func GroupHas

func GroupHas(name string, p ...perm) (bool, error)

GroupHas reports whether the named file has all given permissions for the group.

func IsDir

func IsDir(name string) (bool, error)

IsDir reports whether if the named file is a directory.

func IsFile

func IsFile(name string) (bool, error)

IsFile reports whether the named file is a regular file.

func NewEdit

func NewEdit(filename string) (*edit, error)

NewEdit opens a file to edit; it is created a backup.

func NewInfo

func NewInfo(name string) (*info, error)

NewInfo returns a info describing the named file.

func OthersHave

func OthersHave(name string, p ...perm) (bool, error)

OthersHave reports whether the named file have all given permissions for the others.

func Overwrite

func Overwrite(filename string, b []byte) (err error)

Overwrite truncates the named file to zero and writes len(b) bytes. It returns an error, if any.

func OverwriteString

func OverwriteString(filename, s string) error

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

func OwnerHas

func OwnerHas(name string, p ...perm) (bool, error)

OwnerHas reports whether the named file has all given permissions for the owner.

func Replace

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

Replace replaces all regular expressions mathed in r for the named file.

func ReplaceAtLine

func ReplaceAtLine(filename string, 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, 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, r []Replacer, n int) error

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

Types

type Replacer

type Replacer struct {
	Search, Replace string
}

type ReplacerAtLine

type ReplacerAtLine struct {
	Line, Search, Replace string
}

Jump to

Keyboard shortcuts

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