dos

package
v2.21.0 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package dos contains an abstraction of some functions in the go os package. When used in code, it allows those functions to be mocked in unit tests. In general, the functions are implemented using an interface which is then stored in the context. The functions are then called using dos instead of os, and with an additional first context argument. E.g.

ctx := dos.WithFS(ctx, mockFS)
f, err := dos.Open(ctx, "/etc/resolv.conf")

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(ctx context.Context, name string) (string, error)

Abs is like filepath.Abs but delegates to the context's FS.

func Chdir

func Chdir(ctx context.Context, path string) error

Chdir is like os.Chdir but delegates to the context's FS.

func Environ

func Environ(ctx context.Context) []string

func Executable added in v2.18.0

func Executable(ctx context.Context) (string, error)

func ExpandEnv

func ExpandEnv(ctx context.Context, s string) string

func Getenv

func Getenv(ctx context.Context, key string) string

func Getwd

func Getwd(ctx context.Context) (string, error)

Getwd is like os.Getwd but delegates to the context's FS.

func LookupEnv

func LookupEnv(ctx context.Context, key string) (string, bool)

func Mkdir

func Mkdir(ctx context.Context, name string, perm fs.FileMode) error

Mkdir is like os.Mkdir but delegates to the context's FS.

func MkdirAll

func MkdirAll(ctx context.Context, name string, perm fs.FileMode) error

MkdirAll is like os.MkdirAll but delegates to the context's FS.

func ReadDir

func ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error)

ReadDir is like os.ReadDir but delegates to the context's FS.

func ReadFile

func ReadFile(ctx context.Context, name string) ([]byte, error)

ReadFile is like os.ReadFile but delegates to the context's FS.

func RealPath

func RealPath(ctx context.Context, name string) (string, error)

RealPath returns the real path in the underlying os filesystem or an error if there's no os filesystem.

func Remove

func Remove(ctx context.Context, name string) error

Remove is like os.Remove but delegates to the context's FS.

func RemoveAll added in v2.6.3

func RemoveAll(ctx context.Context, name string) error

RemoveAll is like os.RemoveAll but delegates to the context's FS.

func Rename added in v2.12.0

func Rename(ctx context.Context, oldName, newName string) error

Rename is like os.Rename but delegates to the context's FS.

func Setenv

func Setenv(ctx context.Context, key, value string) error

func Stat

func Stat(ctx context.Context, name string) (fs.FileInfo, error)

Stat is like os.Stat but delegates to the context's FS.

func Stderr added in v2.11.0

func Stderr(ctx context.Context) io.Writer

Stderr returns the context's stdout io.Writer. If no such writer has been defined, it returns os.Stderr.

func Stdin added in v2.11.0

func Stdin(ctx context.Context) io.Reader

Stdin returns the context's stdin io.Reader. If no such reader has been defined, it returns os.Stdin.

func Stdout added in v2.11.0

func Stdout(ctx context.Context) io.Writer

Stdout returns the context's stdout io.Writer. If no such writer has been defined, it returns os.Stdout.

func Symlink(ctx context.Context, oldName, newName string) error

Symlink is like os.Symlink but delegates to the context's FS.

func WithEnv

func WithEnv(ctx context.Context, env Env) context.Context

func WithExe added in v2.18.0

func WithExe(ctx context.Context, exe Exe) context.Context

func WithFS

func WithFS(ctx context.Context, fs FileSystem) context.Context

WithFS assigns the FileSystem to be used by subsequent file system related dos functions.

Example

Example using afero.MemMapFs.

package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"log"
	"os"

	"github.com/spf13/afero"

	"github.com/telepresenceio/telepresence/v2/pkg/dos"
	"github.com/telepresenceio/telepresence/v2/pkg/dos/aferofs"
)

func main() {
	appFS := afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs())
	ctx := dos.WithFS(context.Background(), aferofs.Wrap(appFS))

	if err := dos.MkdirAll(ctx, "/etc", 0o700); err != nil {
		log.Fatal(err)
	}
	hosts, err := dos.Create(ctx, "/etc/example.conf")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintln(hosts, "example = conf")
	hosts.Close()

	if hosts, err = dos.Open(ctx, "/etc/example.conf"); err != nil {
		log.Fatal(err)
	}
	_, err = io.Copy(os.Stdout, hosts)
	_ = hosts.Close()
	if err != nil {
		log.Fatal(err)
	}

	if hosts, err = dos.Open(context.Background(), "/etc/example.conf"); err != nil {
		if errors.Is(err, fs.ErrNotExist) {
			fmt.Println("file does not exist")
		} else {
			fmt.Println(err)
		}
	}
}
Output:

example = conf
file does not exist

func WithLockedFs added in v2.16.0

func WithLockedFs(ctx context.Context) context.Context

func WithStderr added in v2.11.0

func WithStderr(ctx context.Context, w io.Writer) context.Context

WithStderr returns a new context that is parented by the given context, that will return the given writer when used as an argument to in a call to Stderr.

func WithStdin added in v2.11.0

func WithStdin(ctx context.Context, w io.Reader) context.Context

WithStdin returns a new context that is parented by the given context, that will return the given reader when used as an argument to in a call to Stdin.

func WithStdio added in v2.11.0

func WithStdio(ctx context.Context, io Stdio) context.Context

WithStdio returns a new context that is parented by the given context, that will return the values from the given Stdio when used as an argument to in a call to Stdin, Stdout, or Stderr.

func WithStdout added in v2.11.0

func WithStdout(ctx context.Context, w io.Writer) context.Context

WithStdout returns a new context that is parented by the given context, that will return the given writer when used as an argument to in a call to Stdout.

func WriteFile

func WriteFile(ctx context.Context, name string, data []byte, perm fs.FileMode) error

Types

type Env

type Env interface {
	Environ() []string
	ExpandEnv(string) string
	Getenv(string) string
	Setenv(string, string) error
	Lookup(string) (string, bool)
}

Env is an abstraction of the environment related functions with the same name in the os package.

func EnvAPI

func EnvAPI(ctx context.Context) Env

EnvAPI returns the Env that has been registered with the given context, or the instance that delegates to the env functions in the os package.

type Exe added in v2.18.0

type Exe interface {
	Executable() (string, error)
}

Exe is an abstraction of the executable related functions with the same name in the os package.

func ExeAPI added in v2.18.0

func ExeAPI(ctx context.Context) Exe

ExeAPI returns the Exe that has been registered with the given context, or the instance that delegates to the corresponding functions in the os package.

type File

type File interface {
	io.Closer
	io.Reader
	io.ReaderAt
	io.Seeker
	io.Writer
	io.WriterAt

	Name() string
	Readdir(count int) ([]fs.FileInfo, error)
	Readdirnames(n int) ([]string, error)
	Stat() (fs.FileInfo, error)
	Sync() error
	Truncate(size int64) error
	WriteString(s string) (ret int, err error)
	ReadDir(count int) ([]fs.DirEntry, error)
}

File represents a file in the filesystem. The os.File struct implements this interface.

func Create

func Create(ctx context.Context, name string) (File, error)

Create is like os.Create but delegates to the context's FS.

func Open

func Open(ctx context.Context, name string) (File, error)

Open is like os.Open but delegates to the context's FS.

func OpenFile

func OpenFile(ctx context.Context, name string, flag int, perm fs.FileMode) (File, error)

OpenFile is like os.OpenFile but delegates to the context's FS.

type FileSystem

type FileSystem interface {
	Abs(name string) (string, error)
	Chdir(name string) error
	Create(name string) (File, error)
	Getwd() (string, error)
	Mkdir(name string, perm fs.FileMode) error
	MkdirAll(name string, perm fs.FileMode) error
	Open(name string) (File, error)
	OpenFile(name string, flag int, perm fs.FileMode) (File, error)
	ReadDir(name string) ([]fs.DirEntry, error)
	ReadFile(name string) ([]byte, error)
	RealPath(name string) (string, error)
	Remove(name string) error
	RemoveAll(name string) error
	Rename(oldName, newName string) error
	Stat(name string) (fs.FileInfo, error)
	Symlink(oldName, newName string) error
	WriteFile(name string, data []byte, perm fs.FileMode) error
}

FileSystem is an interface that implements functions in the os package.

func WorkingDirWrapper

func WorkingDirWrapper(f FileSystem) FileSystem

type MapEnv

type MapEnv map[string]string

func FromEnvPairs added in v2.17.0

func FromEnvPairs(env []string) MapEnv

FromEnvPairs converts a slice of strings int the form KEY=VALUE into a map[string]string.

func (MapEnv) Environ

func (e MapEnv) Environ() []string

func (MapEnv) ExpandEnv

func (e MapEnv) ExpandEnv(s string) string

func (MapEnv) Getenv

func (e MapEnv) Getenv(key string) string

func (MapEnv) Lookup

func (e MapEnv) Lookup(key string) (string, bool)

func (MapEnv) MergeEnvPairs added in v2.17.0

func (e MapEnv) MergeEnvPairs(env []string)

MergeEnvPairs merges env entries in the form of KEY=VALUE into the given map, giving the env entries higher priority.

func (MapEnv) Setenv

func (e MapEnv) Setenv(key, value string) error

type OwnedFile added in v2.16.0

type OwnedFile interface {
	Chown(uid, gid int) error
}

type Stdio added in v2.11.0

type Stdio interface {
	InOrStdin() io.Reader
	OutOrStdout() io.Writer
	ErrOrStderr() io.Writer
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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