2024-05-06 00:48:54 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package target
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "context"
|
|
|
|
_ "encoding/json"
|
|
|
|
_ "fmt"
|
|
|
|
_ "gopkg.in/yaml.v3"
|
|
|
|
"net/url"
|
|
|
|
"path/filepath"
|
|
|
|
"decl/internal/resource"
|
|
|
|
"compress/gzip"
|
|
|
|
"archive/tar"
|
|
|
|
_ "regexp"
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"log/slog"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Tar struct {
|
|
|
|
Path string `yaml:"path" json:"path"`
|
|
|
|
Gzip bool `yaml:"gzip" json:"gzip"`
|
2024-05-09 08:50:56 +00:00
|
|
|
writer *tar.Writer `yaml:"-" json:"-"`
|
|
|
|
closer func() error `yaml:"-" json:"-"`
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTar() *Tar {
|
2024-05-09 08:50:56 +00:00
|
|
|
return &Tar{ Gzip: false, closer: func() error { return nil } }
|
2024-05-06 00:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
TargetTypes.Register([]string{"tar"}, func(u *url.URL) DocTarget {
|
|
|
|
t := NewTar()
|
|
|
|
t.Path,_ = filepath.Abs(filepath.Join(u.Hostname(), u.Path))
|
2024-05-09 08:50:56 +00:00
|
|
|
t.Open()
|
2024-05-06 00:48:54 +00:00
|
|
|
return t
|
|
|
|
})
|
|
|
|
|
|
|
|
TargetTypes.Register([]string{"tar.gz", "tgz"}, func(u *url.URL) DocTarget {
|
|
|
|
t := NewTar()
|
|
|
|
if u.Scheme == "file" {
|
|
|
|
fileAbsolutePath, _ := filepath.Abs(filepath.Join(u.Hostname(), u.RequestURI()))
|
|
|
|
t.Path = fileAbsolutePath
|
|
|
|
} else {
|
|
|
|
t.Path = filepath.Join(u.Hostname(), u.Path)
|
|
|
|
}
|
|
|
|
t.Gzip = true
|
2024-05-09 08:50:56 +00:00
|
|
|
t.Open()
|
2024-05-06 00:48:54 +00:00
|
|
|
return t
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-09 08:50:56 +00:00
|
|
|
func (t *Tar) Open() error {
|
2024-05-06 00:48:54 +00:00
|
|
|
file, fileErr := os.Create(t.Path)
|
|
|
|
if fileErr != nil {
|
|
|
|
return fileErr
|
|
|
|
}
|
|
|
|
var fileWriter io.WriteCloser
|
|
|
|
if t.Gzip {
|
|
|
|
fileWriter = gzip.NewWriter(file)
|
|
|
|
} else {
|
|
|
|
fileWriter = file
|
|
|
|
}
|
|
|
|
|
2024-05-09 08:50:56 +00:00
|
|
|
t.writer = tar.NewWriter(fileWriter)
|
|
|
|
t.closer = func() error {
|
|
|
|
t.writer.Close()
|
2024-05-06 00:48:54 +00:00
|
|
|
fileWriter.Close()
|
2024-05-09 08:50:56 +00:00
|
|
|
return file.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Tar) Close() error {
|
|
|
|
return t.closer()
|
|
|
|
}
|
2024-05-06 00:48:54 +00:00
|
|
|
|
2024-05-09 08:50:56 +00:00
|
|
|
func (t *Tar) Type() string { return "tar" }
|
|
|
|
|
|
|
|
func (t *Tar) EmitResources(documents []*resource.Document, filter resource.ResourceSelector) error {
|
2024-05-06 00:48:54 +00:00
|
|
|
for _,document := range documents {
|
|
|
|
for _,res := range document.Filter(func(d *resource.Declaration) bool {
|
2024-05-06 04:40:34 +00:00
|
|
|
return d.Type == "file"
|
2024-05-06 00:48:54 +00:00
|
|
|
}) {
|
|
|
|
var f *resource.File = res.Attributes.(*resource.File)
|
|
|
|
slog.Info("Tar.EmitResources", "file", f)
|
|
|
|
hdr, fiErr := tar.FileInfoHeader(f.FileInfo(), "")
|
|
|
|
slog.Info("Tar.EmitResources", "header", hdr, "err", fiErr)
|
2024-05-09 08:50:56 +00:00
|
|
|
if err := t.writer.WriteHeader(hdr); err != nil {
|
2024-05-06 00:48:54 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-05-09 08:50:56 +00:00
|
|
|
if _, err := t.writer.Write([]byte(f.Content)); err != nil {
|
2024-05-06 00:48:54 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|