dos

package
v2.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: Apache-2.0 Imports: 7 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 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 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 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 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", 0700); 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 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 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
	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 (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) Setenv

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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