71 lines
1.5 KiB
Go
71 lines
1.5 KiB
Go
// Copyright (C) 2023-2024 Umorpha Systems
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package source
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"embed"
|
|
"io"
|
|
"path"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
func readFile(fs embed.FS, filename string) []byte {
|
|
// No need to bother checking for errors--an embed.FS will
|
|
// never have an error other than "file does not exist", and
|
|
// it's a programming error (not a runtime error) to call this
|
|
// function for a file that doesn't exist.
|
|
fh, _ := fs.Open(filename)
|
|
ret, _ := io.ReadAll(fh)
|
|
_ = fh.Close()
|
|
return ret
|
|
}
|
|
|
|
func genTar(fn func(w *tar.Writer)) []byte {
|
|
var buf bytes.Buffer
|
|
w := tar.NewWriter(&buf)
|
|
fn(w)
|
|
_ = w.Close()
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func tarDir(w *tar.Writer, fs embed.FS, fsDir, tarPrefix string) {
|
|
_ = w.WriteHeader(&tar.Header{
|
|
Typeflag: tar.TypeDir,
|
|
Name: tarPrefix,
|
|
Mode: 0755,
|
|
})
|
|
|
|
entries, _ := fs.ReadDir(fsDir)
|
|
var names []string
|
|
for _, entry := range entries {
|
|
name := entry.Name()
|
|
if entry.IsDir() {
|
|
name += "/"
|
|
}
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
|
|
for _, name := range names {
|
|
fpath := path.Join(fsDir, name)
|
|
if strings.HasSuffix(name, "/") {
|
|
tarDir(w, fs, fpath, tarPrefix+name)
|
|
} else {
|
|
fb := readFile(fs, fpath)
|
|
_ = w.WriteHeader(&tar.Header{
|
|
Typeflag: tar.TypeReg,
|
|
Name: tarPrefix + name,
|
|
Size: int64(len(fb)),
|
|
Mode: 0644,
|
|
})
|
|
_, _ = w.Write(fb)
|
|
}
|
|
}
|
|
}
|
|
|
|
var Tarball = genTar(func(w *tar.Writer) { tarDir(w, fs, ".", "eclipse/") })
|