An extended reimplementation of the txtar archive format 📂
Package txtar re-implements and extends the original [golang.org/x/tools/txtar] package, making a number of modifications and (hopefully) improvements to the package.
No modifications are made to the txtar syntax, all txtar archives produced with this package are compatible with the original.
Improvements include:
- Files stored in the archive may be looked up by name and operated on individually
- Methods and functions are provided to help easily facilitate individual file editing
- An ergonomic API for constructing an archive, rather than simply exposing struct fields
- File names and contents are stored with all leading and trailing whitespace trimmed so that formatting the archive is easier and more consistent
- Parsing an archive from its serialised format can error in the presence of a malformed document
- Parse accepts an
io.Readerrather than a[]bytefor greater flexibility - Dump is provided to serialise an archive to an
io.Writer
go get go.followtheprocess.codes/txtar@latestpackage main
import (
"fmt"
"log"
"os"
"go.followtheprocess.codes/txtar"
)
func main() {
file, err := os.Open("archive.txtar")
if err != nil {
log.Fatal(err)
}
defer file.Close()
archive, err := txtar.Parse(file)
if err != nil {
log.Fatal(err)
}
// Do things with the archive
fmt.Println(archive.Comment())
for name, contents := range archive.Files() {
fmt.Printf("file: %s\ncontents: %s\n", name, contents)
}
}Inspired and adapted from the original source https://pkg.go.dev/golang.org/x/tools/txtar, all credit to the original Go Authors. Licensed under BSD-3-Clause.