jx/internal/source/tar.go

104 lines
2.2 KiB
Go
Raw Normal View History

2024-04-18 00:07:12 +00:00
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
package source
import (
_ "context"
_ "encoding/json"
_ "fmt"
_ "gopkg.in/yaml.v3"
"net/url"
"path/filepath"
"decl/internal/resource"
2024-05-12 08:20:51 +00:00
"decl/internal/transport"
2024-04-18 00:07:12 +00:00
"compress/gzip"
"archive/tar"
"regexp"
2024-05-12 08:20:51 +00:00
_ "os"
2024-04-18 00:07:12 +00:00
"io"
)
type Tar struct {
Path string `yaml:"path" json:"path"`
2024-05-12 08:20:51 +00:00
transport *transport.Reader `yaml:"-" json:"-"`
2024-04-18 00:07:12 +00:00
}
func NewTar() *Tar {
return &Tar{}
}
func init() {
SourceTypes.Register([]string{"tar"}, func(u *url.URL) DocSource {
t := NewTar()
t.Path,_ = filepath.Abs(filepath.Join(u.Hostname(), u.Path))
2024-05-12 08:20:51 +00:00
t.transport,_ = transport.NewReader(u)
2024-04-18 00:07:12 +00:00
return t
})
SourceTypes.Register([]string{"tar.gz", "tgz"}, func(u *url.URL) DocSource {
t := NewTar()
if u.Scheme == "file" {
fileAbsolutePath, _ := filepath.Abs(filepath.Join(u.Hostname(), u.Path))
2024-04-18 00:07:12 +00:00
t.Path = fileAbsolutePath
} else {
t.Path = filepath.Join(u.Hostname(), u.Path)
}
2024-05-12 08:20:51 +00:00
t.transport,_ = transport.NewReader(u)
2024-04-18 00:07:12 +00:00
return t
})
}
func (t *Tar) Type() string { return "tar" }
func (t *Tar) ExtractResources(filter ResourceSelector) ([]*resource.Document, error) {
documents := make([]*resource.Document, 0, 100)
d := resource.NewDocument()
documents = append(documents, d)
TarGzipFileName := regexp.MustCompile(`^.*\.(tar\.gz|tgz)$`)
TarFileName := regexp.MustCompile(`^.*\.tar$`)
2024-05-12 08:20:51 +00:00
defer t.transport.Close()
2024-04-18 00:07:12 +00:00
var gzipReader io.Reader
switch t.Path {
case TarGzipFileName.FindString(t.Path):
2024-05-12 08:20:51 +00:00
zr, err := gzip.NewReader(t.transport)
2024-04-18 00:07:12 +00:00
if err != nil {
return documents, err
}
gzipReader = zr
fallthrough
case TarFileName.FindString(t.Path):
var fileReader io.Reader
if gzipReader == nil {
2024-05-12 08:20:51 +00:00
fileReader = t.transport
2024-04-18 00:07:12 +00:00
} else {
fileReader = gzipReader
}
tarReader := tar.NewReader(fileReader)
for {
hdr, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return documents, err
}
f := resource.NewFile()
f.Path = hdr.Name
if fiErr := f.UpdateAttributesFromFileInfo(hdr.FileInfo()); fiErr != nil {
return documents, fiErr
}
readErr := f.SetContent(tarReader)
2024-04-18 00:07:12 +00:00
if readErr != nil {
return documents, readErr
}
d.AddResourceDeclaration("file", f)
}
}
return documents, nil
}