Skip to content

cuberat-go/fileutil

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fileutil

import "github.com/cuberat-go/fileutil"

The fileutil package provides utilities for working with files and io streams in Go.

Installation

go get github.com/cuberat-go/fileutil

fileutil.go

var (
    Err_UnknownSuffix error = errors.New("Unknown suffix")
)
func AddCompressionLayer(
    w io.WriteCloser,
    suffix string,
) (
    io.WriteCloser,
    error,
)

Adds compression to output written to writer w, if the suffix is supported.

Supported compression:

gzip  (gz)
bzip2 (bz2) -- calls external program
xz    (xz)  -- calls external program

Call the Close() method on the returned io.WriteCloser to properly shutdown the compression layer.

func AddDecompressionLayer(
    r io.Reader,
    suffix string,
) (io.ReadCloser, error)

Adds decompression to input read from reader r, if the suffix is supported.

Supported decompression:

gzip  (gz)
bzip2 (bz2)
xz    (xz) -- calls external program
func OpenPipesToWriter(final_writer io.Writer,
    progs [][]string) (io.WriteCloser, error)

Runs the list of commands, piping the output of each one to the next. The output of the last command is sent to the final_writer passed in. Each command is represented as a slice of strings. The first element of the slice should be the full path to the program to run. The remaining elements of the slice should be the arguments to the program.

The writer returned writes to the standard input of the first program in the list. Close() should be called when writing has been completed.

func ReadCloserFromReader(r io.Reader, close_func CloseFunc) io.ReadCloser

Given an io.Reader, return an io.ReadCloser with the provided Close() function.

func WriteCloserFromWriter(
    writer io.Writer,
    close_func CloseFunc,
) io.WriteCloser

Given an io.Writer, returns an io.WriteCloser that calls the specified Close() function when Close() is called on the io.WriteCloser.

type CloseFunc func() error
type NameReadCloser interface {
    io.ReadCloser
    Name() string
}

An interface for an io.ReadCloser that also has a name, similar to a *os.FILE.

func NameReadCloserFromReadCloser(
    name string,
    rc io.ReadCloser,
) NameReadCloser

Given an io.ReadCloser, return a NameReadCloser with the provided name.

func NameReadCloserFromReader(
    name string,
    r io.Reader,
    close_func CloseFunc,
) NameReadCloser

Given an io.Reader, return a NameReadCloser with the provided name and Close() function.

func OpenFile(infile string) (NameReadCloser, error)

Opens a file in read-only mode. If the file name ends in a supported compression suffix, input will be decompressed.

Supported decompression:

gzip  (.gz)
bzip2 (.bz2)
xz    (.xz) -- calls external program

Call Close() on the returned NameReadCloser to avoid leaking filehandles and to properly shut down any compression layers.

type NameWriteCloser interface {
    Name() string
    Write(p []byte) (int, error)
    Close() error
}

An interface for an io.WriteCloser that also has a name, similar to a *os.FILE.

func CreateFile(outfile string) (NameWriteCloser, error)

Shortcut for calling CreateFileBuffered() with the default buffer size. Equivalent to CreateFileBuffered() with 0 as the size parameter.

func CreateFileBuffered(outfile string, size int) (NameWriteCloser, error)

Opens a file for writing (buffered). The size argument indicates that the underlying buffer should be at least size bytes. If size < 0, open the file with no buffering. If size == 0, a size of 16K is used. If the file name ends in a supported compression suffix, output will be compressed in that format.

Supported compression:

gzip  (.gz)
bzip2 (.bz2) -- calls external program
xz    (.xz)  -- calls external program

Be sure to call Close() explicitly to flush any buffers and properly shut down any compression layers.

func CreateFileSync(outfile string) (NameWriteCloser, error)

Non-buffered version of CreateFile() and CreateFileBuffered(). Equivalent to CreateFileBuffered() with -1 as the size parameter.

func NameWriteCloserFromWriteCloser(
    name string,
    wc io.WriteCloser,
) NameWriteCloser

Given an io.WriteCloser, return a NameWriteCloser with the provided name.

func NameWriteCloserFromWriter(
    name string,
    writer io.Writer,
    close_func CloseFunc,
) NameWriteCloser

Returns a NameWriteCloser with the provided name and Close() function for the provided io.Writer.


Generated by godoc2md

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages