~evanj/fs2tar

c0866eede69f64b352c0076acb13050c3e610758 — Evan Jones 2 years ago master
Feat(*): Project init.
5 files changed, 115 insertions(+), 0 deletions(-)

A LICENSE
A README
A TODO
A fs2tar.go
A go.mod
A  => LICENSE +19 -0
@@ 1,19 @@
Copyright (c) 2021 Evan J

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

A  => README +27 -0
@@ 1,27 @@
Minimal example:
```go
package main

import (
	"bytes"
	"embed"
	"fmt"
	"log"

	"git.sr.ht/~evanj/fs2tar"
)

//go:embed main.go
var sourceCode embed.FS

func main() {
	var (
		buf bytes.Buffer
		err = fs2tar.Do(sourceCode, &buf)
	)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(buf.String())
}
```

A  => TODO +3 -0
@@ 1,3 @@
godoc
test
https://go.dev/about#best-practices-h2

A  => fs2tar.go +63 -0
@@ 1,63 @@
package fs2tar

import (
	"archive/tar"
	"io"
	"io/fs"
	"os"
)

func Do(fsys fs.FS, w io.Writer) error {
	tw := tar.NewWriter(w)
	defer tw.Close()

	files, err := fs.Glob(fsys, "*")
	if err != nil {
		return err
	}

	for _, file := range files {
		err := fs.WalkDir(fsys, file, func(path string, d fs.DirEntry, err error) error {
			if err != nil {
				return err
			}

			if d.IsDir() {
				return nil
			}

			info, err := d.Info()
			if err != nil {
				return err
			}

			return appendfile(tw, path, info)
		})
		if err != nil {
			return err
		}
	}

	return nil
}

func appendfile(tw *tar.Writer, path string, info fs.FileInfo) error {
	file, err := os.Open(path)
	if err != nil {
		return err
	}
	defer file.Close()

	err = tw.WriteHeader(&tar.Header{
		Name:    path,
		Mode:    int64(info.Mode()),
		Size:    info.Size(),
		ModTime: info.ModTime(),
	})
	if err != nil {
		return err
	}

	_, err = io.Copy(tw, file)
	return err
}

A  => go.mod +3 -0
@@ 1,3 @@
module git.sr.ht/~evanj/fs2tar

go 1.16