lineFile

package module
v0.0.0-...-a3b26e9 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2017 License: MIT Imports: 14 Imported by: 0

README

Line file GoDoc Status

Line file is a file management assistant for line-based operations

Usage

package main

import (
	"bytes"
	"fmt"
)

func main() {
	var (
		f   *File
		err error
	)

	if f, err = New(Opts{
		Path: "./",
		Name: "test",
		Ext:  "txt",
	}); err != nil {
		t.Error(err)
		return
	}

	f.WriteLine([]byte("1"))
	f.WriteLine([]byte("2"))
	f.WriteLine([]byte("3"))
	f.Flush()

	f.prevLine()
	f.prevLine()
	f.prevLine()

	f.ReadLine(func(rdr *bytes.Buffer) {
		var a [16]byte
		n, err := rdr.Read(a[:])
		fmt.Println("Read!", n, err)
		fmt.Println(string(a[:n]))
	})

	f.ReadLine(func(rdr *bytes.Buffer) {
		var a [16]byte
		n, err := rdr.Read(a[:])
		fmt.Println("Read!", n, err)
		fmt.Println(string(a[:n]))
	})

	f.ReadLine(func(rdr *bytes.Buffer) {
		var a [16]byte
		n, err := rdr.Read(a[:])
		fmt.Println("Read!", n, err)
		fmt.Println(string(a[:n]))
	})

	f.ReadLine(func(rdr *bytes.Buffer) {
		var a [16]byte
		n, err := rdr.Read(a[:])
		fmt.Println("Read!", n, err)
		fmt.Println(string(a[:n]))
	})

	f.prevLine()
	f.prevLine()
	f.ReadLine(func(rdr *bytes.Buffer) {
		var a [16]byte
		n, err := rdr.Read(a[:])
		fmt.Println("Read!", n, err)
		fmt.Println(string(a[:n]))
	})
}

Documentation

Index

Constants

View Source
const (
	// SyncBackend is for synchronous backends
	SyncBackend uint8 = iota
	// AsyncBackend is for asychronous backends
	AsyncBackend
)
View Source
const (
	// ErrLineNotFound is returned when a line is not found while calling SeekNextLine
	ErrLineNotFound = errors.Error("line not found")

	// ErrIsClosed is returned when an action is attempted on a closed instance
	ErrIsClosed = errors.Error("cannot perform action on closed instance")

	// ErrIsOpen is returned when an instance is attempted to be re-opened when it's already active
	ErrIsOpen = errors.Error("cannot open an instance which is already open")

	// ErrInvalidOptions is returned when options are invalid
	ErrInvalidOptions = errors.Error("options are invalid")

	// ErrInvalidLineNumber is returned when an invalid line number is provided
	ErrInvalidLineNumber = errors.Error("invalid line number provided")

	// ErrInvalidBackend is returned when an invalid backend option is set in the options
	ErrInvalidBackend = errors.Error("invalid backend option provided")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CryptyMW

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

CryptyMW handles encryption

func NewCryptyMW

func NewCryptyMW(key, iv []byte) *CryptyMW

NewCryptyMW returns a new Crypty middleware

func (*CryptyMW) Name

func (c *CryptyMW) Name() string

Name returns the middleware name

func (*CryptyMW) Reader

func (c *CryptyMW) Reader(r io.Reader) (rc io.ReadCloser, err error)

Reader returns a new gzip reader

func (*CryptyMW) Writer

func (c *CryptyMW) Writer(w io.Writer) (io.WriteCloser, error)

Writer returns a new gzip writer

type File

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

File is a line-based file for easy management

func New

func New(o Opts) (f *File, err error)

New will return a pointer to a new instance of File

func (*File) Append

func (f *File) Append(nf *File) (err error)

Append will append target file (f) with a provided file (nf)

func (*File) Close

func (f *File) Close() (err error)

Close will close the File

func (*File) Flush

func (f *File) Flush() (err error)

Flush will flush the internal buffer and sync the file to disk

func (*File) Location

func (f *File) Location() (loc string)

Location will return the location to this file

func (*File) NextLine

func (f *File) NextLine() (err error)

NextLine will position the file at the next line

func (*File) Open

func (f *File) Open() (err error)

Open will open a closed File

func (*File) PrevLine

func (f *File) PrevLine() (err error)

PrevLine will position the file at the previous line

func (*File) ReadLine

func (f *File) ReadLine(fn func(*bytes.Buffer)) (err error)

ReadLine will return a line in the form of an a bytes.Buffer

func (*File) ReadLines

func (f *File) ReadLines(fn func(*bytes.Buffer) bool) (err error)

ReadLines will return all lines in the form of an a bytes.Buffer Provided function can return true to end iteration early

func (*File) SeekToEnd

func (f *File) SeekToEnd() (err error)

SeekToEnd will seek the file to the end

func (*File) SeekToLine

func (f *File) SeekToLine(n int) (err error)

SeekToLine will seek to line

func (*File) SeekToStart

func (f *File) SeekToStart() (err error)

SeekToStart will seek the file to the start

func (*File) WriteLine

func (f *File) WriteLine(b []byte) (err error)

WriteLine will write a line given a provided body

type GZipMW

type GZipMW struct {
}

GZipMW handles gzipping

func (GZipMW) Name

func (g GZipMW) Name() string

Name returns the middleware name

func (GZipMW) Reader

func (g GZipMW) Reader(r io.Reader) (rc io.ReadCloser, err error)

Reader returns a new gzip reader

func (GZipMW) Writer

func (g GZipMW) Writer(w io.Writer) (io.WriteCloser, error)

Writer returns a new gzip writer

type Middleware

type Middleware interface {
	Name() string
	Writer(w io.Writer) (io.WriteCloser, error)
	Reader(r io.Reader) (io.ReadCloser, error)
}

Middleware is the interface that defines an encoder/decoder chain.

type Opts

type Opts struct {
	Path string
	Name string
	Ext  string

	Backend uint8 // Will default to SyncBackend if empty

	NoSet bool // If set to true, will avoid setting file when calling New
}

Opts are used to initialize a file

Jump to

Keyboard shortcuts

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