stdio

package
v0.0.0-...-6d0aecd Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: ISC Imports: 4 Imported by: 0

Documentation

Overview

Package stdio reads from sets the source for standard input and the destination for standard output:

  • if the filename is "-", input is read from stdin and output is written to stdout
  • otherwise input is read from the named file with output written to stdout
  • if the WithInPlace option is set, a temporary file is created and the original file overwritten when the file descriptor is closed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	*os.File
	// contains filtered or unexported fields
}

func Open

func Open(name string, opt ...Option) (*File, error)

Open opens the file using a temporary file by appending a random string to the filename. The file is renamed to the original name when it is closed.

If the filename is "-", input is read from stdin and written to stdout.

Example
package main

import (
	"fmt"

	"codeberg.org/msantos/redact/pkg/stdio"
)

func main() {
	f, err := stdio.Open("-")
	if err != nil {
		fmt.Printf("unexpected error: %v", err)
		return
	}
	_, _ = f.WriteString("stdout")
}
Output:

stdout

func (*File) Close

func (f *File) Close() error

Close closes the file and conditionally renames to the original filename based on the error status set by SetErr:

  • nil: file is renamed
  • non-nil: the temporary file is deleted, the source file closed and the error returned

func (*File) Name

func (f *File) Name() string

Name returns the original (non-temporary) filename.

func (*File) SetErr

func (f *File) SetErr(err error)

SetErr sets the error state for Close.

func (*File) Write

func (f *File) Write(b []byte) (int, error)

func (*File) WriteAt

func (f *File) WriteAt(b []byte, off int64) (int, error)

func (*File) WriteString

func (f *File) WriteString(s string) (int, error)

func (*File) WriteTo

func (f *File) WriteTo(w io.Writer) (int64, error)

type Opt

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

type Option

type Option func(*Opt)

func WithInPlace

func WithInPlace(b bool) Option

WithInPlace redacts the target file in-place.

Jump to

Keyboard shortcuts

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