filesystem

package
v0.0.38 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: LGPL-3.0 Imports: 25 Imported by: 0

Documentation

Overview

Package filesystem handles the opening and reading of text files.

Index

Examples

Constants

View Source
const (

	// Linefeed is a Linux/macOS line break.
	Linefeed rune = 10
	// CarriageReturn is a partial line break for Windows/DOS.
	CarriageReturn rune = 13
	// NewLine EBCDIC control.
	NewLine rune = 21
	// NextLine EBCDIC control in UTF-8 documents.
	NextLine rune = 133
)

Variables

View Source
var (
	ErrNotFound = errors.New("cannot find the file or sample file")
	ErrStdErr   = errors.New("failed to print to stderr")
)

ErrStdErr could not print to stderr.

View Source
var ErrLB = errors.New("linebreak runes cannot be empty")

Functions

func AddZip added in v0.0.38

func AddZip(name string, z *zip.Writer) error

func Clean

func Clean(name string)

Clean removes the named file or directory.

Example
package main

import (
	"log"

	"github.com/bengarrett/retrotxtgo/lib/filesystem"
)

func main() {
	path, err := filesystem.SaveTemp("examplesave.txt", []byte("hello world")...)
	if err != nil {
		filesystem.Clean(path)
		log.Fatal(err)
	}
	filesystem.Clean(path)
}
Output:

func Columns

func Columns(r io.Reader, lb LB) (int, error)

Columns counts the number of characters used per line in the reader interface.

func Controls

func Controls(r io.Reader) (int, error)

Controls counts the number of ANSI escape controls in the reader interface.

func DirExpansion

func DirExpansion(name string) string

DirExpansion returns the absolute directory path from a named path using shell-like expansions. It currently supports limited Bash tilde, shell dot and double dot syntax.

func IsPipe

func IsPipe() bool

IsPipe determines if Stdin (standard input) is piped from another command.

func LineBreak added in v0.0.31

func LineBreak(r LB, extraInfo bool) string

LineBreak humanizes the value of LineBreaks().

func Lines

func Lines(r io.Reader, lb LB) (int, error)

Lines counts the number of lines in the interface.

func Read

func Read(name string) ([]byte, error)

Read opens and returns the content of the named file.

func ReadAllBytes

func ReadAllBytes(name string) ([]byte, error)

ReadAllBytes reads the named file and returns the content as a byte array. Create a word and random character generator to make files larger than 64k.

func ReadChunk

func ReadChunk(name string, chars int) ([]byte, error)

ReadChunk reads and returns the start of the named file.

func ReadColumns

func ReadColumns(name string) (int, error)

ReadColumns counts the number of characters used per line in the named file.

func ReadControls

func ReadControls(name string) (int, error)

ReadControls counts the number of ANSI escape sequences in the named file.

func ReadLine

func ReadLine(name string, lb nl.LineBreaks) (string, error)

ReadLine reads a named file location or a named temporary file and returns its content.

func ReadLineBreaks added in v0.0.31

func ReadLineBreaks(name string) ([2]rune, error)

ReadLineBreaks scans the named file for the most commonly used line break method.

func ReadLines

func ReadLines(name string) (int, error)

ReadLines counts the number of lines in the named file.

func ReadPipe

func ReadPipe() ([]byte, error)

ReadPipe reads data piped by the operating system's STDIN. If no data is detected the program will exit.

func ReadRunes

func ReadRunes(name string) (int, error)

ReadRunes returns the number of runes in the named file.

func ReadTail

func ReadTail(name string, offset int) ([]byte, error)

ReadTail reads the named file from the offset position relative to the end of the file.

func ReadText

func ReadText(name string) (string, error)

ReadText reads a named file location or a named temporary file and returns its content.

func ReadWords

func ReadWords(name string) (int, error)

ReadWords counts the number of spaced words in the named file.

func Runes

func Runes(r io.Reader) (int, error)

Runes returns the number of runes in the reader interface.

func SaveTemp

func SaveTemp(name string, b ...byte) (string, error)

SaveTemp saves bytes to a named temporary file.

func Tar added in v0.0.31

func Tar(name string, files ...string) error

Tar add files to a named tar file archive.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/bengarrett/retrotxtgo/lib/filesystem"
	"github.com/bengarrett/retrotxtgo/lib/internal/tmp"
)

func main() {
	tmpTar := tmp.File("tar_test.tar")
	tmpFile, err := filesystem.SaveTemp(tmpTar, []byte("x")...)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(tmpFile)
	if err = filesystem.Tar(tmpTar, tmpFile); err != nil {
		log.Print(err)
		return
	}
	f, err := os.Stat(tmpFile)
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Printf("%s, %d", f.Name(), f.Size())
}
Output:

tar_test.tar, 1536

func Touch

func Touch(name string) (string, error)

Touch creates an empty file at the named location.

func UniqueName added in v0.0.31

func UniqueName(name string) (string, error)

UniqueName confirms the file name doesn't conflict with an existing file. If there is a conflict, a new incremental name will be returned.

Example
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/bengarrett/retrotxtgo/lib/filesystem"
	"github.com/bengarrett/retrotxtgo/lib/internal/tmp"
)

func main() {
	name := "retrotxtgo_uniquetest.txt"

	// Create a temporary 1 byte file in the temporary directory
	tmpFile, err := filesystem.SaveTemp(tmp.File(name), []byte("x")...)
	if err != nil {
		log.Fatal(err)
	}
	defer os.Remove(tmpFile)

	// Use UniqueName to find a new unique filename
	// so not to conflict with the previously saved file
	u, err := filesystem.UniqueName(tmpFile)
	if err != nil {
		log.Print(err)
		return
	}

	// In Linux the new name will be retrotxtgo_uniquetest_1.txt
	// In Windows the name be retrotxtgo_uniquetest (1).txt
	newName := filepath.Base(u)

	// As the new unique names vary based on the host operating system
	// Compare the name lengths to confirm the creation of a new filename
	unique := bool(len(newName) > len(name))
	fmt.Print(unique)
}
Output:

true

func Word added in v0.0.38

func Word(s string) bool

Word scans the content of a word for characters that are not digits, letters or punctuation and if discovered returns false. If a space or line break is encountered the scan will end.

func Words

func Words(r io.Reader) (int, error)

Words counts the number of spaced words in the reader interface.

func WordsEBCDIC added in v0.0.31

func WordsEBCDIC(r io.Reader) (int, error)

WordsEBCDIC counts the number of spaced words in the EBCDIC encoded reader interface.

Example
package main

import (
	"bytes"
	"fmt"
	"log"

	"github.com/bengarrett/retrotxtgo/lib/filesystem"
	"github.com/bengarrett/retrotxtgo/static"
)

func main() {
	b, err := static.File.ReadFile("text/cp037.txt")
	if err != nil {
		log.Fatal(err)
	}
	nr := bytes.NewReader(b)
	words, err := filesystem.WordsEBCDIC(nr)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%d words", words)
}
Output:

16 words

func Write added in v0.0.38

func Write(name string, b ...byte) (written int, path string, err error)

Write b to the named file.

Types

type Files added in v0.0.31

type Files []string

Files to zip.

func (*Files) Zip added in v0.0.31

func (files *Files) Zip(name, comment string, ow, quiet bool) error

Zip packages and compresses files to an archive using the provided name.

type LB added in v0.0.31

type LB [2]rune

LB is the text line break control represented as 2 runes.

func CR added in v0.0.31

func CR() LB

CR carriage return.

func CRLF added in v0.0.31

func CRLF() LB

CRLF carriage return + linefeed.

func LF added in v0.0.31

func LF() LB

LF linefeed.

func LFCR added in v0.0.31

func LFCR() LB

LFCR linefeed + carriage return.

func LineBreaks added in v0.0.31

func LineBreaks(utf bool, runes ...rune) LB

LineBreaks will try to guess the line break representation as a 2 byte value. A guess of Unix will return [10, 0], Windows [13, 10], otherwise a [0, 0] value is returned.

func NEL added in v0.0.31

func NEL() LB

NEL next line.

func NL added in v0.0.31

func NL() LB

NL new line.

type Zip added in v0.0.31

type Zip struct {
	// Zip path and filename.
	Name string
	// Root path of the directory to archive.
	Root string
	// Comment to embed.
	Comment string
	// Overwrite an existing named zip file if encountered.
	Overwrite bool
	// Quiet suppresses all non-error messages.
	Quiet bool
}

Zip archive details.

Example
package main

import (
	"fmt"
	"log"
	"os"
	"path"

	"github.com/bengarrett/retrotxtgo/lib/filesystem"
	"github.com/bengarrett/retrotxtgo/lib/internal/tmp"
)

func main() {
	// Create a temporary directory
	tmpZip := tmp.File("retrotxtgo_zip_directory_test")
	err := os.MkdirAll(tmpZip, 0o755)
	if err != nil {
		log.Fatal(err)
	}
	defer os.RemoveAll(tmpZip)

	// Create a temporary 1 byte file in the temporary directory
	tmpFile, err := filesystem.SaveTemp(path.Join(tmpZip, "temp.zip"), []byte("x")...)
	if err != nil {
		log.Print(err)
		return
	}
	defer os.Remove(tmpFile)

	// Initialize the Zip archive file
	name := tmp.File("exampleZip.zip")
	zip := filesystem.Zip{
		Name:      name,
		Root:      tmpZip,
		Comment:   "",
		Overwrite: true,
		Quiet:     true,
	}

	// Create the Zip archive file
	if err = zip.Create(); err != nil {
		log.Print(err)
		return
	}

	// Check the Zip archive exists
	s, err := os.Stat(name)
	if err != nil {
		log.Print(err)
		return
	}
	fmt.Printf("%s, %d", s.Name(), s.Size())
}
Output:

exampleZip.zip, 149

func (*Zip) Create added in v0.0.31

func (z *Zip) Create() error

Create zip packages and compresses files contained the root directory into an archive using the provided name.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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