local

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2020 License: MIT Imports: 14 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ListFiles

func ListFiles(pth string) ([]stat.Stats, error)

ListFiles will list all files in the provided pth directory. pth must be a directory.

Will not list recursively and does not return directories. Checksums are not returned.

func Stat added in v0.5.0

func Stat(pth string) (stat.Stats, error)

Stat returns a summary stats of a file or directory. It can be used to verify read permissions

Types

type Options

type Options struct {
	*buf.Options
}

func NewOptions

func NewOptions() *Options

type Reader

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

Reader

func NewReader

func NewReader(pth string) (*Reader, error)
Example
pth := "./test/test.txt"
createFile(pth)
r, err := NewReader(pth)
if r == nil {
	return
}

fmt.Println(err)                 // output: <nil>
fmt.Println(r.sts.Path != "")    // output: true
fmt.Println(r.sts.Size)          // output: 20
fmt.Println(r.sts.Created != "") // output: true
fmt.Println(r.f != nil)          // output: true
fmt.Println(r.rBuf != nil)       // output: true
fmt.Println(r.rGzip == nil)      // output: true
fmt.Println(r.rHshr != nil)      // output: true
fmt.Println(r.closed)            // output: false

// cleanup
os.Remove(pth)
os.Remove("./test")
Output:

<nil>
true
20
true
true
true
true
true
false

func (*Reader) Close

func (r *Reader) Close() (err error)
Example
pth := "./test/test.txt"
createFile(pth)
r, _ := NewReader(pth)
if r == nil {
	return
}

r.ReadLine()
r.ReadLine()
err := r.Close()

fmt.Println(err)            // output: <nil>
fmt.Println(r.sts.Checksum) // output: 54f30d75cf7374c7e524a4530dbc93c2
fmt.Println(r.closed)       // output: true

// cleanup
os.Remove(pth)
os.Remove("./test")
Output:

<nil>
54f30d75cf7374c7e524a4530dbc93c2
true

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)
Example
pth := "./test/test.txt"
createFile(pth)
r, _ := NewReader(pth)
if r == nil {
	return
}

b1 := make([]byte, 20)
b2 := make([]byte, 20)
n1, err1 := r.Read(b1)
n2, err2 := r.Read(b2)

fmt.Print(string(b1))      // output: test line, test line
fmt.Println(n1)            // output: 20
fmt.Println(err1)          // output: <nil>
fmt.Println(n2)            // output: 0
fmt.Println(err2)          // output: EOF
fmt.Println(r.sts.ByteCnt) // output: 20
fmt.Println(r.sts.LineCnt) // output: 0

// cleanup
os.Remove(pth)
os.Remove("./test")
Output:

test line
test line
20
<nil>
0
EOF
20
0

func (*Reader) ReadLine

func (r *Reader) ReadLine() (ln []byte, err error)
Example
pth := "./test/test.txt"
createFile(pth)
r, _ := NewReader(pth)
if r == nil {
	return
}

ln1, err1 := r.ReadLine()
ln2, err2 := r.ReadLine()
ln3, err3 := r.ReadLine()

fmt.Println(string(ln1))   // output: test line
fmt.Println(string(ln2))   // output: test line
fmt.Println(string(ln3))   // output:
fmt.Println(err1)          // output: <nil>
fmt.Println(err2)          // output: <nil>
fmt.Println(err3)          // output: EOF
fmt.Println(r.sts.ByteCnt) // output: 20
fmt.Println(r.sts.LineCnt) // output: 2

// cleanup
os.Remove(pth)
os.Remove("./test")
Output:

test line
test line

<nil>
<nil>
EOF
20
2

func (*Reader) Stats

func (r *Reader) Stats() stat.Stats
Example
pth := "./test/test.txt"
createFile(pth)
r, _ := NewReader(pth)
if r == nil {
	return
}

r.ReadLine()
r.ReadLine()
sts := r.Stats()

fmt.Println(sts.ByteCnt) // output: 20
fmt.Println(sts.LineCnt) // output: 2

// cleanup
os.Remove(pth)
os.Remove("./test")
Output:

20
2

type Writer

type Writer struct {
	// contains filtered or unexported fields
}
Example (CopyAndClean)
// memory buffer to file
w, _ := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
n, err := w.copyAndClean()

// read file
f, _ := os.Open(w.sts.Path)
if f == nil {
	return
}
b := make([]byte, 20)
rn, _ := f.Read(b)

// check actual size
fInfo, _ := os.Stat(w.sts.Path)
if fInfo == nil {
	return
}

fmt.Println(n)            // output: 20
fmt.Println(err)          // output: <nil>
fmt.Print(string(b))      // output: test line, test line
fmt.Println(rn)           // output: 20
fmt.Println(fInfo.Size()) // output: 20

// cleanup
os.Remove("./test")
Output:

20
<nil>
test line
test line
20
20
Example (CopyAndCleanTmpFile)
// file buffer to file
opt := NewOptions()
opt.UseFileBuf = true
opt.FileBufDir = "./test/tmp"
w, _ := NewWriter("./test/test.txt", opt)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
n, err := w.copyAndClean()

// read file
f, _ := os.Open(w.sts.Path)
if f == nil {
	return
}
b := make([]byte, 20)
rn, _ := f.Read(b)

// check actual size
fInfo, _ := os.Stat(w.sts.Path)
if fInfo == nil {
	return
}

// tmp file does not exist
_, tmpErr := os.Open(w.tmpPth)
if tmpErr == nil {
	return
}
removed := strings.Contains(tmpErr.Error(), "no such file or directory")

fmt.Println(n)            // output: 20
fmt.Println(err)          // output: <nil>
fmt.Print(string(b))      // output: test line, test line
fmt.Println(rn)           // output: 20
fmt.Println(fInfo.Size()) // output: 20
fmt.Println(removed)      // output: true

// cleanup
os.Remove("./test/tmp")
os.Remove("./test/test.txt")
os.Remove("./test")
Output:

20
<nil>
test line
test line
20
20
true
Example (CopyAndCleanTmpFileErr)
// file buffer to file
opt := NewOptions()
opt.UseFileBuf = true
opt.FileBufDir = "./test/tmp"
w, _ := NewWriter("./test/test.txt", opt)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
w.sts.Path = "/bad/file.txt"
n, err := w.copyAndClean()
if err == nil {
	return
}
notCopied := strings.Contains(err.Error(), "no such")

// tmp file does not exist
_, tmpErr := os.Open(w.tmpPth)
if tmpErr == nil {
	return
}
tmpRemoved := strings.Contains(tmpErr.Error(), "no such file or directory")

fmt.Println(n)          // output: 0
fmt.Println(notCopied)  // output: true
fmt.Println(tmpRemoved) // output: true

// cleanup
os.Remove("./test/tmp")
os.Remove("./test")
Output:

0
true
true
Example (CopyAndCleanTmpFileToDev)
// file buffer to device file
opt := NewOptions()
opt.UseFileBuf = true
opt.FileBufDir = "./test/tmp"
w, _ := NewWriter("/dev/stdout", opt)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
n, err := w.copyAndClean() // output: test line, test line

// tmp file does not exist
_, tmpErr := os.Open(w.tmpPth)
if tmpErr == nil {
	return
}
removed := strings.Contains(tmpErr.Error(), "no such file or directory")

fmt.Println(n)       // output: 20
fmt.Println(err)     // output: <nil>
fmt.Println(removed) // output: true

// cleanup
os.Remove("./test/tmp")
os.Remove("./test")
Output:

test line
test line
20
<nil>
true
Example (CopyAndCleanToDev)
// memory buffer to device file
w, _ := NewWriter("/dev/stdout", nil)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
n, err := w.copyAndClean() // output: test line, test line

fmt.Println(n)   // output: 20
fmt.Println(err) // output: <nil>
Output:

test line
test line
20
<nil>

func NewWriter

func NewWriter(pth string, opt *Options) (*Writer, error)

NewWriter will create a new local writer. - 'pth' is the full path (with filename) that will be written. If the final file extension is a supported compression format then the file will be compressed in that format. - 'append' indicates whether the write session will append the contents to an existing file or truncate and then write. The defaut is false which will truncate an existing file and then write. - 'lazy' set to 'true' will tell the writer to do a lazy write. This means that all write calls will write to memory first and then write to 'pth' when writing is complete with a final call to 'Close'. If Close is never called then the file will not be written.

When initializing a new writer, pth is checked for the correct write permissions. An error is returned if the writer will not have the correct permissions.

For lazy writing, the writer supports writing to memory or a temp file. The writer will use the temp file option if tmpDir and/or tmpPrefix is provided. The writer will remove a temp file with a call to Close.

Example
w, err := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}

fmt.Println(strings.HasSuffix(w.sts.Path, "/test/test.txt")) // output: true
fmt.Println(err)                                             // output: <nil>

os.Remove("./test") // cleanup test dir
Output:

true
<nil>

func (*Writer) Abort

func (w *Writer) Abort() error

Abort will: - clear and close buffer - prevent further writing

Example
w, _ := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}
err := w.Abort()

fmt.Println(err) // output: <nil>

os.Remove("./test") // cleanup test dir
Output:

<nil>

func (*Writer) Close

func (w *Writer) Close() error

Close will: - calculate final checksum - set file size - set file created date - copy (mv) buffer to pth file - clear and close buffer - report any errors

Calling Abort after Close will do nothing. Writing after Close will not write and will not return a nil-error.

Example
opt := NewOptions()
opt.UseFileBuf = true
opt.FileBufDir = "./test/tmp"
opt.FileBufPrefix = "prefix_"
w, _ := NewWriter("./test/test.txt", opt)
if w == nil {
	return
}
w.WriteLine([]byte("test line"))
w.WriteLine([]byte("test line"))
err := w.Close()
sts := w.Stats()

// check that tmp file was cleaned up
_, tmpErr := os.Open(w.tmpPth)
if tmpErr == nil {
	return
}
removed := strings.Contains(tmpErr.Error(), "no such file or directory")

fmt.Println(err)               // output: <nil>
fmt.Println(sts.Created != "") // output: true
fmt.Println(sts.Checksum)      // output: 54f30d75cf7374c7e524a4530dbc93c2
fmt.Println(removed)           // output: true

// cleanup
os.Remove("./test/tmp")
os.Remove("./test/test.txt")
os.Remove("./test")
Output:

<nil>
true
54f30d75cf7374c7e524a4530dbc93c2
true

func (*Writer) Stats

func (w *Writer) Stats() stat.Stats
Example
w, _ := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}
w.sts.Path = "test path"
w.sts.Created = "test created"

sts := w.Stats()
fmt.Println(sts.Path)    // output: test path
fmt.Println(sts.Created) // output: test created

os.Remove("./test") // cleanup test dir
Output:

test path
test created

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)
Example
w, _ := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}

n, err := w.Write([]byte("test line"))

fmt.Println(n)   // output: 9
fmt.Println(err) // output: <nil>

os.Remove("./test") // cleanup test dir
Output:

9
<nil>

func (*Writer) WriteLine

func (w *Writer) WriteLine(ln []byte) (err error)
Example
w, _ := NewWriter("./test/test.txt", nil)
if w == nil {
	return
}

err := w.WriteLine([]byte("test line"))

fmt.Println(err) // output: <nil>

os.Remove("./test") // cleanup test dir
Output:

<nil>

Jump to

Keyboard shortcuts

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