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 ¶
- func Abs(ctx context.Context, name string) (string, error)
- func Chdir(ctx context.Context, path string) error
- func Environ(ctx context.Context) []string
- func ExpandEnv(ctx context.Context, s string) string
- func Getenv(ctx context.Context, key string) string
- func Getwd(ctx context.Context) (string, error)
- func LookupEnv(ctx context.Context, key string) (string, bool)
- func Mkdir(ctx context.Context, name string, perm fs.FileMode) error
- func MkdirAll(ctx context.Context, name string, perm fs.FileMode) error
- func ReadDir(ctx context.Context, name string) ([]fs.DirEntry, error)
- func ReadFile(ctx context.Context, name string) ([]byte, error)
- func RealPath(ctx context.Context, name string) (string, error)
- func Remove(ctx context.Context, name string) error
- func RemoveAll(ctx context.Context, name string) error
- func Rename(ctx context.Context, oldName, newName string) error
- func Setenv(ctx context.Context, key, value string) error
- func Stat(ctx context.Context, name string) (fs.FileInfo, error)
- func Stderr(ctx context.Context) io.Writer
- func Stdin(ctx context.Context) io.Reader
- func Stdout(ctx context.Context) io.Writer
- func Symlink(ctx context.Context, oldName, newName string) error
- func WithEnv(ctx context.Context, env Env) context.Context
- func WithFS(ctx context.Context, fs FileSystem) context.Context
- func WithLockedFs(ctx context.Context) context.Context
- func WithStderr(ctx context.Context, w io.Writer) context.Context
- func WithStdin(ctx context.Context, w io.Reader) context.Context
- func WithStdio(ctx context.Context, io Stdio) context.Context
- func WithStdout(ctx context.Context, w io.Writer) context.Context
- func WriteFile(ctx context.Context, name string, data []byte, perm fs.FileMode) error
- type Env
- type File
- type FileSystem
- type MapEnv
- type OwnedFile
- type Stdio
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RealPath ¶
RealPath returns the real path in the underlying os filesystem or an error if there's no os filesystem.
func Stderr ¶ added in v2.11.0
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
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
Stdout returns the context's stdout io.Writer. If no such writer has been defined, it returns os.Stdout.
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 WithStderr ¶ added in v2.11.0
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
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
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
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.
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.
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.
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 ¶
func FromEnvPairs ¶ added in v2.17.0
FromEnvPairs converts a slice of strings int the form KEY=VALUE into a map[string]string.
func (MapEnv) MergeEnvPairs ¶ added in v2.17.0
MergeEnvPairs merges env entries in the form of KEY=VALUE into the given map, giving the env entries higher priority.