xopen

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 12 Imported by: 0

README

![GoDoc] (https://godoc.org/github.com/brentp/xopen?status.png) Build Status Coverage Status

xopen

-- import "github.com/brentp/xopen"

xopen makes it easy to get buffered (possibly gzipped) readers and writers. and close all of the associated files. Ropen opens a file for reading. Wopen opens a file for writing. Both will use gzip when appropriate and will use buffered IO.

Usage

Here's how to get a buffered reader:

// gzipped
rdr, err := xopen.Ropen("some.gz")
// normal
rdr, err := xopen.Ropen("some.txt")
// stdin (possibly gzipped)
rdr, err := xopen.Ropen("-")
// https://
rdr, err := xopen.Ropen("http://example.com/some-file.txt")
// Cmd
rdr, err := xopen.Ropen("|ls -lh somefile.gz")
// User directory:
rdr, err := xopen.Ropen("~/brentp/somefile")

Get a buffered writer with xopen.Wopen.

func CheckBytes
func CheckBytes(b *bufio.Reader, buf []byte) (bool, error)

CheckBytes peeks at a buffered stream and checks if the first read bytes match.

func IsGzip
func IsGzip(b *bufio.Reader) (bool, error)

IsGzip returns true buffered Reader has the gzip magic.

func IsStdin
func IsStdin() bool

IsStdin checks if we are getting data from stdin.

func XReader
func XReader(f string) (io.Reader, error)

XReader returns a reader from a url string or a file.

type Reader
type Reader struct {
	*bufio.Reader
}

Reader is returned by Ropen

func Buf
func Buf(r io.Reader) *Reader

Return a buffered reader from an io.Reader If f == "-", then it will attempt to read from os.Stdin. If the file is gzipped, it will be read as such.

func Ropen
func Ropen(f string) (*Reader, error)

Ropen opens a buffered reader.

func (*Reader) Close
func (r *Reader) Close() error

Close the associated files.

type Writer
type Writer struct {
	*bufio.Writer
}

Writer is returned by Wopen

func Wopen
func Wopen(f string) (*Writer, error)

Wopen opens a buffered reader. If f == "-", then stdout will be used. If f endswith ".gz", then the output will be gzipped.

func (*Writer) Close
func (w *Writer) Close() error

Close the associated files.

func (*Writer) Flush
func (w *Writer) Flush()

Flush the writer.

Documentation

Overview

Package xopen makes it easy to get buffered readers and writers. Ropen opens a (possibly gzipped) file/process/http site for buffered reading. Wopen opens a (possibly gzipped) file for buffered writing. Both will use gzip when appropriate and will user buffered IO.

Index

Constants

This section is empty.

Variables

View Source
var ErrDirNotSupported = errors.New("xopen: input is a directory")

ErrDirNotSupported means the path is a directory.

View Source
var ErrNoContent = errors.New("xopen: no content")

ErrNoContent means nothing in the stream/file.

Functions

func CheckBytes

func CheckBytes(b *bufio.Reader, buf []byte) (bool, error)

CheckBytes peeks at a buffered stream and checks if the first read bytes match.

func Exists

func Exists(path string) bool

Exists checks if a local file exits

func ExpandUser

func ExpandUser(path string) (string, error)

ExpandUser expands ~/path and ~otheruser/path appropriately

func IsGzip

func IsGzip(b *bufio.Reader) (bool, error)

IsGzip returns true buffered Reader has the gzip magic.

func IsStdin

func IsStdin() bool

IsStdin checks if we are getting data from stdin.

func XReader

func XReader(f string) (io.Reader, error)

XReader returns a reader from a url string or a file.

Types

type Reader

type Reader struct {
	*bufio.Reader
	// contains filtered or unexported fields
}

Reader is returned by Ropen

func Buf

func Buf(r io.Reader) (*Reader, error)

Buf returns a buffered reader from an io.Reader If f == "-", then it will attempt to read from os.Stdin. If the file is gzipped, it will be read as such.

func Ropen

func Ropen(f string) (*Reader, error)

Ropen opens a buffered reader.

func (*Reader) Close

func (r *Reader) Close() error

Close the associated files.

type Writer

type Writer struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

Writer is returned by Wopen

func Wopen

func Wopen(f string) (*Writer, error)

Wopen opens a buffered reader. If f == "-", then stdout will be used. If f endswith ".gz", then the output will be gzipped.

func WopenFile

func WopenFile(f string, flag int, perm os.FileMode) (*Writer, error)

WopenFile opens a buffered reader. If f == "-", then stdout will be used. If f endswith ".gz", then the output will be gzipped.

func WopenGzip

func WopenGzip(f string) (*Writer, error)

WopenGzip opens a buffered gzipped reader. If f == "-", then stdout will be used.

func (*Writer) Close

func (w *Writer) Close() error

Close the associated files.

func (*Writer) Flush

func (w *Writer) Flush()

Flush the writer.

Jump to

Keyboard shortcuts

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