fileutil

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2020 License: BSD-2-Clause Imports: 10 Imported by: 0

README

fileutil

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

Overview

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

Installation

go get github.com/cuberat-go/fileutil

Index

Package files

fileutil.go

Variables

var (
    Err_UnknownSuffix error = errors.New("Unknown suffix")
)

func AddCompressionLayer

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

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

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

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

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

type CloseFunc func() error

type NameReadCloser

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
func NameReadCloserFromReadCloser(
    name string,
    rc io.ReadCloser,
) NameReadCloser

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

func NameReadCloserFromReader
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
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

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
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
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
func CreateFileSync(outfile string) (NameWriteCloser, error)

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

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

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

func NameWriteCloserFromWriter
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

Documentation

Overview

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

Installation

go get github.com/cuberat-go/fileutil

Index

Constants

This section is empty.

Variables

View Source
var (
	Err_UnknownSuffix error = errors.New("Unknown suffix")
)

Functions

func AddCompressionLayer

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

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

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

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

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`.

Types

type CloseFunc

type CloseFunc func() error

type NameReadCloser

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

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

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

func NameReadCloserFromReader

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

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

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

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

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

func CreateFileSync(outfile string) (NameWriteCloser, error)

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

func NameWriteCloserFromWriteCloser

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

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

func NameWriteCloserFromWriter

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`.

Jump to

Keyboard shortcuts

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