feat: added downloading templated archives
This commit is contained in:
parent
1ebea8cb73
commit
98f9acad5d
5 changed files with 48 additions and 23 deletions
|
|
@ -92,7 +92,10 @@ var downloadCmd = cobra.Command{
|
||||||
}
|
}
|
||||||
if res.StatusCode != http.StatusOK {
|
if res.StatusCode != http.StatusOK {
|
||||||
log.Error().
|
log.Error().
|
||||||
Int("status", res.StatusCode).
|
Any("status", map[string]any{
|
||||||
|
"code": res.StatusCode,
|
||||||
|
"message": res.Status,
|
||||||
|
}).
|
||||||
Str("host", host).
|
Str("host", host).
|
||||||
Str("path", path).
|
Str("path", path).
|
||||||
Str("output", outputPath).
|
Str("output", outputPath).
|
||||||
|
|
@ -125,6 +128,7 @@ func init() {
|
||||||
downloadCmd.Flags().StringP("output", "o", "", "Set the output path to write files")
|
downloadCmd.Flags().StringP("output", "o", "", "Set the output path to write files")
|
||||||
downloadCmd.Flags().StringSlice("profiles", []string{}, "Set the profile to use to populate data store")
|
downloadCmd.Flags().StringSlice("profiles", []string{}, "Set the profile to use to populate data store")
|
||||||
downloadCmd.Flags().StringSlice("plugins", []string{}, "Set the plugins to run before downloading files")
|
downloadCmd.Flags().StringSlice("plugins", []string{}, "Set the plugins to run before downloading files")
|
||||||
|
downloadCmd.Flags().Bool("extract", false, "Set whether to extract archive locally after downloading")
|
||||||
|
|
||||||
downloadCmd.AddCommand(downloadProfileCmd, downloadPluginCmd)
|
downloadCmd.AddCommand(downloadProfileCmd, downloadPluginCmd)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package archive
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -37,27 +38,47 @@ func Expand(path string) error {
|
||||||
|
|
||||||
func addToArchive(tw *tar.Writer, filename string, hooks []makeshift.Hook) error {
|
func addToArchive(tw *tar.Writer, filename string, hooks []makeshift.Hook) error {
|
||||||
var (
|
var (
|
||||||
hook makeshift.Hook
|
tempfile = fmt.Sprintf("%s.tmp", filename)
|
||||||
file *os.File
|
file *os.File
|
||||||
|
contents []byte
|
||||||
data any
|
data any
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
// open file to write to archive
|
|
||||||
file, err = os.Open(filename)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
// run pre-hooks to modify the contents of the file
|
// run pre-hooks to modify the contents of the file
|
||||||
// before archiving using plugins
|
// before archiving using plugins
|
||||||
for _, hook := range hooks {
|
for _, hook := range hooks {
|
||||||
|
// set the file in the data store before running hook
|
||||||
|
contents, err = os.ReadFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to read '%s' to download: %v", filename, err)
|
||||||
|
}
|
||||||
|
hook.Data.Set("file", contents)
|
||||||
|
|
||||||
err = hook.Run()
|
err = hook.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create temporary file to use to add to archive
|
||||||
|
hook = hooks[len(hooks)-1]
|
||||||
|
data, err = hook.Data.Get("out")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get output data from '%s' plugin: %v", hook.Plugin.Name(), err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(tempfile, data.([]byte), 0o777)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write temporary file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// open file to write to archive
|
||||||
|
file, err = os.Open(tempfile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open temporary file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
// get FileInfo for file size, mode, etc.
|
// get FileInfo for file size, mode, etc.
|
||||||
info, err := file.Stat()
|
info, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -72,7 +93,7 @@ func addToArchive(tw *tar.Writer, filename string, hooks []makeshift.Hook) error
|
||||||
// create a tar Header from the FileInfo data
|
// create a tar Header from the FileInfo data
|
||||||
header, err := tar.FileInfoHeader(info, info.Name())
|
header, err := tar.FileInfoHeader(info, info.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("failed to create FileInfoHeader: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// use full path as name (FileInfoHeader only takes the basename) to
|
// use full path as name (FileInfoHeader only takes the basename) to
|
||||||
|
|
@ -86,15 +107,14 @@ func addToArchive(tw *tar.Writer, filename string, hooks []makeshift.Hook) error
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// take the contents from the last hook and update files
|
// copy file content to tar archive
|
||||||
hook = hooks[len(hooks)-1]
|
_, err = io.Copy(tw, strings.NewReader(string(data.([]byte))))
|
||||||
data, err = hook.Data.Get("out")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy file content to tar archive
|
// delete the temporary file since we're done with it
|
||||||
_, err = io.Copy(tw, strings.NewReader(data.(string)))
|
err = os.Remove(tempfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ func (p *Jinja2) Run(store storage.KVStore, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// write render templates to data store output
|
// write render templates to data store output
|
||||||
store.Set("out", output.String())
|
store.Set("out", output.Bytes())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ func (p *SmdClient) Run(store storage.KVStore, args []string) error {
|
||||||
// write data back to shared data store to be used by other plugins
|
// write data back to shared data store to be used by other plugins
|
||||||
bytes, err = json.Marshal(client)
|
bytes, err = json.Marshal(client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("(smd) failed to marshal SMD client: %v")
|
return fmt.Errorf("(smd) failed to marshal SMD client: %v", err)
|
||||||
}
|
}
|
||||||
store.Set("shared", bytes)
|
store.Set("shared", bytes)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,13 +53,14 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
// determine if path is directory, file, or exists
|
// determine if path is directory, file, or exists
|
||||||
if fileInfo, err = os.Stat(path); err == nil {
|
if fileInfo, err = os.Stat(path); err == nil {
|
||||||
if fileInfo.IsDir() {
|
if fileInfo.IsDir() {
|
||||||
|
// get the final archive path
|
||||||
|
archivePath := fmt.Sprintf("%d.tar.gz", time.Now().Unix())
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
|
Str("archive_path", archivePath).
|
||||||
Str("type", "directory").
|
Str("type", "directory").
|
||||||
Msg("Service.Download()")
|
Msg("Service.Download()")
|
||||||
|
|
||||||
// get the final archive path
|
|
||||||
archivePath := fmt.Sprintf("%d.tar.gz", time.Now().Unix())
|
|
||||||
out, err = os.Create(archivePath)
|
out, err = os.Create(archivePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.writeErrorResponse(w, fmt.Sprintf("failed to create named file: %v", err), http.StatusInternalServerError)
|
s.writeErrorResponse(w, fmt.Sprintf("failed to create named file: %v", err), http.StatusInternalServerError)
|
||||||
|
|
@ -157,7 +158,7 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
s.writeErrorResponse(w, fmt.Sprintf("failed to get data from hook: %v", err), http.StatusInternalServerError)
|
s.writeErrorResponse(w, fmt.Sprintf("failed to get data from hook: %v", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write([]byte(data.(string)))
|
w.Write(data.([]byte))
|
||||||
} else {
|
} else {
|
||||||
w.Write(contents)
|
w.Write(contents)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue