mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-06 12:58:35 +02:00
Moved archive package to public pkg/
This commit is contained in:
parent
28c618ffce
commit
1244405747
10 changed files with 160 additions and 104 deletions
101
pkg/archive/archive.go
Normal file
101
pkg/archive/archive.go
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
// Implements generic access to files inside an archive.
|
||||
//
|
||||
// An archive in this context can be any container that holds files.
|
||||
// In this implementation the archive can be a ZIP file or a directory.
|
||||
package archive
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Generic interface to access files inside an archive.
|
||||
type ArchiveReader interface {
|
||||
io.Closer
|
||||
|
||||
// Open the file inside the archive identified by the given path.
|
||||
// The path is relative to the archive's root.
|
||||
// The caller must call [fs.File.Close] when finished using the file.
|
||||
Open(path string) (fs.File, error)
|
||||
|
||||
// List files inside the archive which satisfy the given glob pattern.
|
||||
// This method only returns files, not directories.
|
||||
Glob(pattern string) ([]FileInfo, error)
|
||||
}
|
||||
|
||||
// Open an archive in path.
|
||||
// The archive can be a ZIP file or a directory. The implementation
|
||||
// will detect the type of archive and return the appropriate
|
||||
// implementation of the Archive interface.
|
||||
func OpenArchive(path string) (ArchiveReader, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch mode := fi.Mode(); {
|
||||
case mode.IsRegular():
|
||||
archive := &zipArchive{}
|
||||
err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return archive, nil
|
||||
case mode.IsDir():
|
||||
archive := &dirArchive{}
|
||||
err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return archive, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported file mode: %s", mode)
|
||||
}
|
||||
}
|
||||
|
||||
// Interface for a file that can be opened when needed.
|
||||
type OpenableFile interface {
|
||||
// Open the file for reading.
|
||||
// The caller is responsible to call [io.ReadCloser.Close] when
|
||||
// finished reading the file.
|
||||
Open() (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
// Generic information about a file inside an archive.
|
||||
// This provides the filename and allows opening the file for reading.
|
||||
type FileInfo struct {
|
||||
Name string
|
||||
File OpenableFile
|
||||
}
|
||||
|
||||
// A openable file in the filesystem.
|
||||
type filesystemFile struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (f *filesystemFile) Open() (io.ReadCloser, error) {
|
||||
return os.Open(f.path)
|
||||
}
|
189
pkg/archive/archive_test.go
Normal file
189
pkg/archive/archive_test.go
Normal file
|
@ -0,0 +1,189 @@
|
|||
/*
|
||||
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package archive_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"go.uploadedlobster.com/scotty/pkg/archive"
|
||||
)
|
||||
|
||||
func ExampleOpenArchive() {
|
||||
a, err := archive.OpenArchive("testdata/archive.zip")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer a.Close()
|
||||
|
||||
files, err := a.Glob("a/*.txt")
|
||||
for _, fi := range files {
|
||||
fmt.Println(fi.Name)
|
||||
f, err := fi.File.Open()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
|
||||
// Output: a/1.txt
|
||||
// a1
|
||||
}
|
||||
|
||||
var testArchives = []string{
|
||||
"testdata/archive",
|
||||
"testdata/archive.zip",
|
||||
}
|
||||
|
||||
func TestGlob(t *testing.T) {
|
||||
for _, path := range testArchives {
|
||||
a, err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.Close()
|
||||
|
||||
files, err := a.Glob("[ab]/1.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(files) != 2 {
|
||||
t.Errorf("Expected 2 files, got %d", len(files))
|
||||
}
|
||||
|
||||
expectedName := "b/1.txt"
|
||||
var fileInfo *archive.FileInfo = nil
|
||||
for _, file := range files {
|
||||
if file.Name == expectedName {
|
||||
fileInfo = &file
|
||||
}
|
||||
}
|
||||
|
||||
if fileInfo == nil {
|
||||
t.Fatalf("Expected file %q to be found", expectedName)
|
||||
}
|
||||
|
||||
if fileInfo.File == nil {
|
||||
t.Fatalf("Expected FileInfo to hold an openable File")
|
||||
}
|
||||
|
||||
f, err := fileInfo.File.Open()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedData := "b1\n"
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != expectedData {
|
||||
fmt.Printf("%s: Expected file content to be %q, got %q",
|
||||
path, expectedData, string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobAll(t *testing.T) {
|
||||
for _, path := range testArchives {
|
||||
a, err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.Close()
|
||||
|
||||
files, err := a.Glob("*/*")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
filenames := make([]string, 0, len(files))
|
||||
for _, f := range files {
|
||||
fmt.Printf("%v: %v\n", path, f.Name)
|
||||
filenames = append(filenames, f.Name)
|
||||
}
|
||||
|
||||
slices.Sort(filenames)
|
||||
|
||||
expectedFilenames := []string{
|
||||
"a/1.txt",
|
||||
"b/1.txt",
|
||||
"b/2.txt",
|
||||
}
|
||||
if !slices.Equal(filenames, expectedFilenames) {
|
||||
t.Errorf("%s: Expected filenames to be %q, got %q",
|
||||
path, expectedFilenames, filenames)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpen(t *testing.T) {
|
||||
for _, path := range testArchives {
|
||||
a, err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.Close()
|
||||
|
||||
f, err := a.Open("b/2.txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedData := "b2\n"
|
||||
data, err := io.ReadAll(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(data) != expectedData {
|
||||
fmt.Printf("%s: Expected file content to be %q, got %q",
|
||||
path, expectedData, string(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOpenError(t *testing.T) {
|
||||
for _, path := range testArchives {
|
||||
a, err := archive.OpenArchive(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer a.Close()
|
||||
|
||||
_, err = a.Open("b/3.txt")
|
||||
if err == nil {
|
||||
t.Errorf("%s: Expected the Open command to fail", path)
|
||||
}
|
||||
}
|
||||
}
|
77
pkg/archive/dir.go
Normal file
77
pkg/archive/dir.go
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package archive
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// An implementation of the [ArchiveReader] interface for directories.
|
||||
type dirArchive struct {
|
||||
path string
|
||||
dirFS fs.FS
|
||||
}
|
||||
|
||||
func (a *dirArchive) OpenArchive(path string) error {
|
||||
a.path = filepath.Clean(path)
|
||||
a.dirFS = os.DirFS(path)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *dirArchive) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Open opens the named file in the archive.
|
||||
// [fs.File.Close] must be called to release any associated resources.
|
||||
func (a *dirArchive) Open(path string) (fs.File, error) {
|
||||
return a.dirFS.Open(path)
|
||||
}
|
||||
|
||||
func (a *dirArchive) Glob(pattern string) ([]FileInfo, error) {
|
||||
files, err := fs.Glob(a.dirFS, pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]FileInfo, 0)
|
||||
for _, name := range files {
|
||||
stat, err := fs.Stat(a.dirFS, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if stat.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
fullPath := filepath.Join(a.path, name)
|
||||
info := FileInfo{
|
||||
Name: name,
|
||||
File: &filesystemFile{path: fullPath},
|
||||
}
|
||||
result = append(result, info)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
BIN
pkg/archive/testdata/archive.zip
vendored
Normal file
BIN
pkg/archive/testdata/archive.zip
vendored
Normal file
Binary file not shown.
1
pkg/archive/testdata/archive/a/1.txt
vendored
Normal file
1
pkg/archive/testdata/archive/a/1.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
a1
|
1
pkg/archive/testdata/archive/b/1.txt
vendored
Normal file
1
pkg/archive/testdata/archive/b/1.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
b1
|
1
pkg/archive/testdata/archive/b/2.txt
vendored
Normal file
1
pkg/archive/testdata/archive/b/2.txt
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
b2
|
80
pkg/archive/zip.go
Normal file
80
pkg/archive/zip.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
Copyright © 2025 Philipp Wolfer <phw@uploadedlobster.com>
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
package archive
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// An implementation of the [ArchiveReader] interface for zip files.
|
||||
type zipArchive struct {
|
||||
zip *zip.ReadCloser
|
||||
}
|
||||
|
||||
func (a *zipArchive) OpenArchive(path string) error {
|
||||
zip, err := zip.OpenReader(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
a.zip = zip
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *zipArchive) Close() error {
|
||||
if a.zip == nil {
|
||||
return nil
|
||||
}
|
||||
return a.zip.Close()
|
||||
}
|
||||
|
||||
func (a *zipArchive) Glob(pattern string) ([]FileInfo, error) {
|
||||
result := make([]FileInfo, 0)
|
||||
for _, file := range a.zip.File {
|
||||
if file.FileInfo().IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
if matched, err := filepath.Match(pattern, file.Name); matched {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
info := FileInfo{
|
||||
Name: file.Name,
|
||||
File: file,
|
||||
}
|
||||
result = append(result, info)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *zipArchive) Open(path string) (fs.File, error) {
|
||||
file, err := a.zip.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return file, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue