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 Setenv(ctx context.Context, key, value string) error
- func Stat(ctx context.Context, name string) (fs.FileInfo, error)
- 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 WriteFile(ctx context.Context, name string, data []byte, perm fs.FileMode) error
- type Env
- type File
- type FileSystem
- type MapEnv
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 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
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 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