fileutil

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: Apache-2.0 Imports: 10 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrFileExists is the error returned if a file exists.
	ErrFileExists = errors.New("file exists")

	// ErrIsDir is the error returned if the file is a directory.
	ErrIsDir = errors.New("file is a directory")

	// SnippetHeader is the header of a step generated snippet in a
	// configuration file.
	SnippetHeader = "# autogenerated by step"

	// SnippetFooter is the header of a step generated snippet in a
	// configuration file.
	SnippetFooter = "# end"
)

Functions

func AppendNewLine

func AppendNewLine(filename string, data []byte, perm os.FileMode) error

AppendNewLine appends the given data at the end of the file. If the last character of the file does not contain an LF it prepends it to the data.

func FileError

func FileError(err error, filename string) error

FileError is a wrapper for errors of the os package.

func PrependLine added in v0.7.0

func PrependLine(filename string, data []byte, perm os.FileMode) error

PrependLine prepends the given line into the given filename and removes other instances of the line in the file.

func RemoveLine added in v0.7.0

func RemoveLine(filename, substr string) error

RemoveLine removes a single line which contains the given substring from the given file.

func WriteFile

func WriteFile(filename string, data []byte, perm os.FileMode) error

WriteFile wraps ioutil.WriteFile with a prompt to overwrite a file if the file exists. It returns ErrFileExists if the user picks to not overwrite the file. If force is set to true, the prompt will not be presented and the file if exists will be overwritten.

func WriteSnippet

func WriteSnippet(filename string, data []byte, perm os.FileMode) error

WriteSnippet writes the given data into the given filename. It surrounds the data with a default header and footer, and it will replace the previous one.

Types

type File

type File struct {
	File *os.File
	// contains filtered or unexported fields
}

File represents a wrapper on os.File that supports read, write, seek and close methods, but they won't be called if an error occurred before.

func OpenFile

func OpenFile(name string, flag int, perm os.FileMode) (*File, error)

OpenFile calls os.OpenFile method and returns the os.File wrapped.

func (*File) Close

func (f *File) Close() error

Close wraps `func (*os.File) Close` it will always call Close but the error return will be the first error thrown if any.

func (*File) Read

func (f *File) Read(b []byte) (n int, err error)

Read wraps `func (*os.File) Read` but doesn't perform the operation if a previous error was thrown.

func (*File) ReadAt

func (f *File) ReadAt(b []byte, off int64) (n int, err error)

ReadAt wraps `func (*os.File) ReadAt` but doesn't perform the operation if a previous error was thrown.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (ret int64, err error)

Seek wraps `func (*os.File) Seek` but doesn't perform the operation if a previous error was thrown.

func (*File) Write

func (f *File) Write(b []byte) (n int, err error)

Write wraps `func (*os.File) Write` but doesn't perform the operation if a previous error was thrown.

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (n int, err error)

WriteAt wraps `func (*os.File) WriteAt` but doesn't perform the operation if a previous error was thrown.

func (*File) WriteString

func (f *File) WriteString(s string) (n int, err error)

WriteString wraps `func (*os.File) WriteString` but doesn't perform the operation if a previous error was thrown.

Jump to

Keyboard shortcuts

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