fs

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2023 License: MIT Imports: 15 Imported by: 2

README

Charm FS

Example

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/fs"
	"os"

	charmfs "github.com/charmbracelet/charm/fs"
)

func main() {
	// Open the file system
	cfs, err := charmfs.NewFS()
	if err != nil {
		panic(err)
	}
	// Write a file
	data := []byte("some data")
	err = os.WriteFile("/tmp/data", data, 0644)
	if err != nil {
		panic(err)
	}
	file, err := os.Open("/tmp/data")
	if err != nil {
		panic(err)
	}
	err = cfs.WriteFile("/our/test/data", file)
	if err != nil {
		panic(err)
	}
	// Get a file
	f, err := cfs.Open("/our/test/data")
	if err != nil {
		panic(err)
	}
	buf = bytes.NewBuffer(nil)
	_, err = io.Copy(buf, f)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(buf.Bytes()))

	// Or use fs.ReadFileFS
	bs, err := cfs.ReadFile("/our/test/data")
	if err != nil {
		panic(err)
	}
	fmt.Println(string(bs))

	// Since we're using fs.FS interfaces we can also do things like walk a tree
	err = fs.WalkDir(cfs, "/", func(path string, d fs.DirEntry, err error) error {
		fmt.Println(path)
		return nil
	})
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

Package fs provides an fs.FS implementation for encrypted Charm Cloud storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DirFile

type DirFile struct {
	Buffer   *bytes.Buffer
	FileInfo fs.FileInfo
}

DirFile is a fs.File that represents a directory entry.

func (*DirFile) Close

func (df *DirFile) Close() error

Close is a no-op but satisfies fs.FS.

func (*DirFile) Read

func (df *DirFile) Read(buf []byte) (int, error)

Read reads from the DirFile and satisfies fs.FS.

func (*DirFile) Stat

func (df *DirFile) Stat() (fs.FileInfo, error)

Stat returns a fs.FileInfo.

type FS

type FS struct {
	// contains filtered or unexported fields
}

FS is an implementation of fs.FS, fs.ReadFileFS and fs.ReadDirFS with additional write methods. Data is stored across the network on a Charm Cloud server, with encryption and decryption happening client-side.

func NewFS

func NewFS() (*FS, error)

NewFS returns an FS with the default configuration.

func NewFSWithClient

func NewFSWithClient(cc *client.Client) (*FS, error)

NewFSWithClient returns an FS with a custom *client.Client.

func (*FS) Client

func (cfs *FS) Client() *client.Client

Client returns the underlying *client.Client.

func (*FS) DecryptPath

func (cfs *FS) DecryptPath(path string) (string, error)

DecryptPath returns the unencrypted path for a given path.

func (*FS) EncryptPath

func (cfs *FS) EncryptPath(path string) (string, error)

EncryptPath returns the encrypted path for a given path.

func (*FS) Open

func (cfs *FS) Open(name string) (fs.File, error)

Open implements Open for fs.FS.

func (*FS) ReadDir

func (cfs *FS) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads the named directory and returns a list of directory entries.

func (*FS) ReadFile

func (cfs *FS) ReadFile(name string) ([]byte, error)

ReadFile implements fs.ReadFileFS.

func (*FS) Remove

func (cfs *FS) Remove(name string) error

Remove deletes a file from the Charm Cloud server.

func (*FS) WriteFile

func (cfs *FS) WriteFile(name string, src fs.File) error

WriteFile encrypts data from the src io.Reader and stores it on the configured Charm Cloud server. The fs.FileMode is retained. If the file is in a directory that doesn't exist, it and any needed subdirectories are created.

type File

type File struct {
	// contains filtered or unexported fields
}

File implements the fs.File interface.

func (*File) Close

func (f *File) Close() error

Close closes the underlying file datasource.

func (*File) Read

func (f *File) Read(b []byte) (int, error)

Read reads bytes from the file returning number of bytes read or an error. The error io.EOF will be returned when there is nothing else to read.

func (*File) ReadDir

func (f *File) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir returns the directory entries for the directory file. If needed, the directory listing will be resolved from the Charm Cloud server.

func (*File) Stat

func (f *File) Stat() (fs.FileInfo, error)

Stat returns an fs.FileInfo that describes the file.

type FileInfo

type FileInfo struct {
	charm.FileInfo
	// contains filtered or unexported fields
}

FileInfo implements the fs.FileInfo interface.

func (*FileInfo) Info

func (fi *FileInfo) Info() (fs.FileInfo, error)

Info returns the fs.FileInfo, used to satisfy fs.DirEntry.

func (*FileInfo) IsDir

func (fi *FileInfo) IsDir() bool

IsDir returns a bool set to true if the file is a directory.

func (*FileInfo) ModTime

func (fi *FileInfo) ModTime() time.Time

ModTime returns the last modification time for the file.

func (*FileInfo) Mode

func (fi *FileInfo) Mode() fs.FileMode

Mode returns the fs.FileMode.

func (*FileInfo) Name

func (fi *FileInfo) Name() string

Name returns the file name.

func (*FileInfo) Size

func (fi *FileInfo) Size() int64

Size returns the file size in bytes.

func (*FileInfo) Sys

func (fi *FileInfo) Sys() interface{}

Sys returns the underlying system implementation, may be nil.

func (*FileInfo) Type

func (fi *FileInfo) Type() fs.FileMode

Type returns the type bits from the fs.FileMode.

Jump to

Keyboard shortcuts

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