iohelp

package
v0.0.0-...-1dd1f65 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrPathsetNil = errors.New("pathset nil")

ErrPathsetNil is a standard error for when things that expect a pathset don't get one.

Functions

func CheckDone

func CheckDone(ctx context.Context) error

CheckDone checks, without blocking, to see if ctx has been cancelled as of this instant in time. If so, it propagates the context's error value; if not, it returns nil.

Example

ExampleCheckDone is a runnable example for CheckDone.

package main

import (
	"context"
	"errors"
	"fmt"

	"github.com/c4-project/c4t/internal/helper/iohelp"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	// This shouldn't block, but should return without an error:
	err1 := iohelp.CheckDone(ctx)

	// This should also not block, but return a 'cancelled' error:
	cancel()
	err2 := iohelp.CheckDone(ctx)

	fmt.Printf("1: nil=%v, cancelled=%v\n", err1 == nil, errors.Is(err1, context.Canceled))
	fmt.Printf("2: nil=%v, cancelled=%v\n", err2 == nil, errors.Is(err2, context.Canceled))

}
Output:

1: nil=true, cancelled=false
2: nil=false, cancelled=true

func CopyClose

func CopyClose(dst io.WriteCloser, src io.ReadCloser) (int64, error)

CopyClose copies src to dst, then closes both. It closes src and dst even if there was an error while copying.

func CopyCloseSrc

func CopyCloseSrc(dst io.Writer, src io.ReadCloser) (int64, error)

CopyCloseSrc copies src to dst, then closes src. It closes src even if there was an error while copying.

func DiscardCloser

func DiscardCloser() io.WriteCloser

DiscardCloser is like io.Discard, but implements WriteCloser.

func EnsureWriter

func EnsureWriter(w io.Writer) io.Writer

EnsureWriter passes through w if non-nil, or supplies io.Discard otherwise.

func ExpandMany

func ExpandMany(paths []string) ([]string, error)

ExpandMany applies homedir.Expand to every (file)path in paths.

func ExtlessFile

func ExtlessFile(fpath string) string

ExtlessFile gets the file part of slash-path fpath without its extension.

Example

ExampleExtlessFile is a runnable example for ExtlessFile.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/helper/iohelp"
)

func main() {
	fmt.Println(iohelp.ExtlessFile("foo.c"))
	fmt.Println(iohelp.ExtlessFile("/home/piers/test"))
	fmt.Println(iohelp.ExtlessFile("/home/piers/example.txt"))

}
Output:

foo
test
example

func IsFileEmpty

func IsFileEmpty(f *os.File) (bool, error)

IsFileEmpty checks whether the open file f is empty. It can fail if we can't stat the file.

func Mkdirs

func Mkdirs(ps ...string) error

Mkdirs tries to make each directory in the directory set d.

func Rmdirs

func Rmdirs(ps ...string) error

Rmdirs tries to remove each directory in the directory set d.

Types

type ErrWriter

type ErrWriter struct {
	Err error
}

ErrWriter provides an io.Writer that fails with the wrapped error.

func (ErrWriter) Write

func (e ErrWriter) Write(_ []byte) (n int, err error)

type NopWriteCloser

type NopWriteCloser struct {
	io.Writer
}

NopWriteCloser is like a NopCloser, but implements WriteCloser rather than ReadCloser.

func (NopWriteCloser) Close

func (n NopWriteCloser) Close() error

Close does nothing.

Jump to

Keyboard shortcuts

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