io

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package io provide a library for reading and watching file, and reading from standard input.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfirmYesNo

func ConfirmYesNo(in io.Reader, msg string, defIsYes bool) bool

ConfirmYesNo display a question to standard output and read for answer from input Reader for simple yes "y" or no "n" answer. If input Reader is nil, it will set to standard input. If "defIsYes" is true and answer is empty (only new line), then it will return true.

func Copy added in v0.13.0

func Copy(out, in string) (err error)

Copy file from in to out. If the output file is already exist, it will be truncated. If the file is not exist, it will created with permission set to user's read-write only.

func IsBinary added in v0.7.0

func IsBinary(file string) bool

IsBinary will return true if content of file is binary. If file is not exist or there is an error when reading or closing the file, it will return false.

Example
fmt.Println(IsBinary("/bin/bash"))
fmt.Println(IsBinary("io.go"))
Output:

true
false

func IsDirEmpty

func IsDirEmpty(dir string) (ok bool)

IsDirEmpty will return true if directory is not exist or empty; otherwise it will return false.

func IsFileExist

func IsFileExist(parent, relpath string) bool

IsFileExist will return true if relative path is exist on parent directory; otherwise it will return false.

func RmdirEmptyAll

func RmdirEmptyAll(path string) error

RmdirEmptyAll remove directory in path if it's empty until one of the parent is not empty.

Types

type DirWatcher added in v0.6.0

type DirWatcher struct {
	// Path to directory that we want to watch.
	Path string

	// Includes contains list of regex to filter file names that we want
	// to be notified.
	Includes []string

	// Excludes contains list of regex to filter file names that we did
	// not want to be notified.
	Excludes []string

	// Delay define a duration when the new changes will be fetched from
	// system.
	// This field is optional, minimum is 100 milli second and default is
	// 5 seconds.
	Delay time.Duration

	// Callback define a function that will be called when change detected
	// on directory.
	Callback WatchCallback
	// contains filtered or unexported fields
}

DirWatcher is a naive implementation of directory change notification.

func (*DirWatcher) Start added in v0.6.0

func (dw *DirWatcher) Start() (err error)

Start create a new watcher that detect changes in directory and its content.

func (*DirWatcher) Stop added in v0.6.0

func (dw *DirWatcher) Stop()

Stop watching changes on directory.

type FileState added in v0.6.0

type FileState byte

FileState define the state of file. There are three state of file: created, modified, or deleted.

const (
	// FileStateCreated indicate that the file has been created.
	FileStateCreated FileState = iota
	// FileStateModified indicate that the file has been modified.
	FileStateModified
	// FileStateDeleted indicate that the file has been deleted.
	FileStateDeleted
)

type NodeState added in v0.6.0

type NodeState struct {
	// Node represent the file information.
	Node *memfs.Node
	// State of file, its either created, modified, or deleted.
	State FileState
}

NodeState contains the information about the file and its state.

type Reader

type Reader struct {
	X int    // X contains the current index of readed buffer.
	V []byte // V contains the buffer.
}

Reader represent a buffered reader that use an index to move through slice of bytes.

The following illustration show the uses of each fields,

+-+-+-+-+-+
| | | | | | <= r.V
+-+-+-+-+-+
   ^
   |
  r.X

func NewReader

func NewReader(path string) (*Reader, error)

NewReader open the file in path for reading.

func (*Reader) Current

func (r *Reader) Current() byte

Current byte at index position or 0 if EOF.

func (*Reader) Init

func (r *Reader) Init(src []byte)

Init initialize reader buffer from slice of byte.

func (*Reader) ReadLine

func (r *Reader) ReadLine() (line []byte)

ReadLine read one line including the line feed '\n' character.

func (*Reader) ReadUntil

func (r *Reader) ReadUntil(seps, terms []byte) (tok []byte, isTerm bool, c byte)

ReadUntil read the content of buffer until one of separator found, or until one of terminator character found, or until EOF. If terminator found, the returned isTerm value will be true, and c value will be the character that cause the termination.

func (*Reader) Rest

func (r *Reader) Rest() []byte

Rest return the rest of unreaded buffer.

func (*Reader) ScanInt64

func (r *Reader) ScanInt64() (n int64, c byte)

ScanInt64 convert textual representation of number into int64 and return it. Any spaces before actual reading of text will be ignored. The number may prefixed with '-' or '+', if its '-', the returned value must be negative.

On success, c is non digit character that terminate scan, if its 0, its mean EOF.

func (*Reader) SkipHorizontalSpace

func (r *Reader) SkipHorizontalSpace() (n int, c byte)

SkipHorizontalSpace read until no space, carriage return, or tab occurred on buffer. On EOF it will return 0.

func (*Reader) SkipLine

func (r *Reader) SkipLine()

SkipLine skip reading content until newline.

func (*Reader) SkipN

func (r *Reader) SkipN(n int) bool

SkipN skip reading n bytes from buffer and return true if EOF.

func (*Reader) SkipSpaces

func (r *Reader) SkipSpaces() (c byte)

SkipSpaces read until no white spaces found and return the first byte that is not white spaces. On EOF, it will return 0.

func (*Reader) SkipUntil

func (r *Reader) SkipUntil(seps []byte) (c byte)

SkipUntil skip reading content until one of separator found or EOF.

func (*Reader) String

func (r *Reader) String() string

String return all unreaded content as string.

func (*Reader) UnreadN

func (r *Reader) UnreadN(n int) byte

UnreadN unread the buffer N characters and return the character its pointed to. If N greater than buffer length, it will reset the pointer index back to zero.

type WatchCallback added in v0.6.0

type WatchCallback func(*NodeState)

WatchCallback is a function that will be called when Watcher or DirWatcher detect any changes on its file or directory. The watcher will pass the file information and its state.

type Watcher

type Watcher struct {
	// contains filtered or unexported fields
}

Watcher is a naive implementation of file event change notification.

func NewWatcher

func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err error)

NewWatcher return a new file watcher that will inspect the file for changes with period specified by duration `d` argument.

If duration is less or equal to 100 millisecond, it will be set to default duration (5 seconds).

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop watching the file.

Jump to

Keyboard shortcuts

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