debme

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 5 Imported by: 47

README


embed.FS wrapper providing additional functionality

CodeFactor CodeFactor

Features

  • Get an embed.FS from an embedded subdirectory
  • Handy Copy(sourcePath, targetPath) method to copy an embedded file to the filesystem
  • 100% embed.FS compatible
  • 100% code coverage

Example

package main

import (
	"embed"
	"github.com/leaanthony/debme"
	"io/fs"
)

// Example Filesystem:
//
// fixtures/
// ├── test1
// |   └── onefile.txt
// └── test2
//     └── inner
//         ├── deeper
//         |   └── three.txt
//         ├── one.txt
//         └── two.txt

//go:embed fixtures
var fixtures embed.FS

func main() {
	root, _ := debme.FS(fixtures, "fixtures")

	// Anchor to "fixtures/test1"
	test1, _ := root.FS("test1")
	files1, _ := test1.ReadDir(".")

	println(len(files1)) // 1
	println(files1[0].Name()) // "onefile.txt"

	// Anchor to "fixtures/test2/inner"
	inner, _ := root.FS("test2/inner")
	one, _ := inner.ReadFile("one.txt")

	println(string(one)) // "1"

	// Fully compatible FS
	fs.WalkDir(inner, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		println("Path:", path, " Name:", d.Name())
		return nil
	})

	/*
		Path: .  Name: inner
		Path: deeper  Name: deeper
		Path: deeper/three.txt  Name: three.txt
		Path: one.txt  Name: one.txt
		Path: two.txt  Name: two.txt
	*/
	
	// Go deeper
	deeper, _ := inner.FS("deeper")
	deeperFiles, _ := deeper.ReadDir(".")

	println(len(deeperFiles)) // 1
	println(files1[0].Name()) // "three.txt"
	
	// Copy files
	err := deeper.Copy("three.txt", "/path/to/target.txt")
}

Why

Go's new embed functionality is awesome! The only thing I found a little frustrating was the need to manage base paths. This module was created out of the need to embed multiple templates in the Wails CLI.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Debme

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

Debme is an embed.FS compatible wrapper, providing Sub() functionality

func FS added in v1.1.0

func FS(fs embed.FS, basedir string) (Debme, error)

FS creates an embed.FS compatible struct, anchored to the given basedir.

func (Debme) CopyFile added in v1.2.0

func (d Debme) CopyFile(sourcePath string, target string, perm os.FileMode) error

func (Debme) FS added in v1.1.0

func (d Debme) FS(subDir string) (Debme, error)

FS returns a new Debme anchored at the given subdirectory.

func (Debme) Open

func (d Debme) Open(name string) (fs.File, error)

Open opens the named file for reading and returns it as an fs.File.

func (Debme) ReadDir

func (d Debme) ReadDir(name string) ([]fs.DirEntry, error)

ReadDir reads and returns the entire named directory.

func (Debme) ReadFile

func (d Debme) ReadFile(name string) ([]byte, error)

ReadFile reads and returns the content of the named file.

Jump to

Keyboard shortcuts

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