refactor: changed how file/archives are downloaded and saved
This commit is contained in:
parent
98f9acad5d
commit
2536848541
2 changed files with 58 additions and 27 deletions
|
|
@ -5,9 +5,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.towk2.me/towk/makeshift/pkg/client"
|
"git.towk2.me/towk/makeshift/pkg/client"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
@ -30,7 +28,7 @@ var downloadCmd = cobra.Command{
|
||||||
curl $MAKESHIFT_HOST/download/test?plugins=smd,jinja2&profile=test
|
curl $MAKESHIFT_HOST/download/test?plugins=smd,jinja2&profile=test
|
||||||
|
|
||||||
# download directory and extract it's contents automatically
|
# download directory and extract it's contents automatically
|
||||||
makeshift download --extract
|
makeshift download -xr
|
||||||
`,
|
`,
|
||||||
Short: "Download and modify files with plugins",
|
Short: "Download and modify files with plugins",
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
@ -52,15 +50,6 @@ var downloadCmd = cobra.Command{
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
|
||||||
// set output path to match path if empty
|
|
||||||
if outputPath == "" {
|
|
||||||
if path != "." || path != "" {
|
|
||||||
outputPath = filepath.Base(path)
|
|
||||||
} else {
|
|
||||||
outputPath = fmt.Sprintf("%d.file", time.Now().Unix())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
query = fmt.Sprintf("/download/%s?", path)
|
query = fmt.Sprintf("/download/%s?", path)
|
||||||
if len(pluginNames) > 0 {
|
if len(pluginNames) > 0 {
|
||||||
query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ","))
|
query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ","))
|
||||||
|
|
@ -102,33 +91,66 @@ var downloadCmd = cobra.Command{
|
||||||
Msg("response returned bad status")
|
Msg("response returned bad status")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
if outputPath != "" {
|
|
||||||
|
// helper to write downloaded files
|
||||||
|
var writeFiles = func(path string, body []byte) {
|
||||||
err = os.WriteFile(outputPath, body, 0o755)
|
err = os.WriteFile(outputPath, body, 0o755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("failed to write file(s) from download")
|
log.Error().Err(err).Msg("failed to write file(s) from download")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
fmt.Println(string(body))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// determine if output path is an archive or file
|
||||||
|
switch res.Header.Get("FILETYPE") {
|
||||||
|
case "archive":
|
||||||
|
// write archive to disk with or without '-o' specified
|
||||||
|
if outputPath == "" {
|
||||||
|
outputPath = fmt.Sprintf("%s.tar.gz", path)
|
||||||
|
writeFiles(outputPath, body)
|
||||||
|
log.Debug().Str("path", outputPath).Msg("wrote archive to pre-determined path")
|
||||||
|
} else {
|
||||||
|
writeFiles(outputPath, body)
|
||||||
|
log.Debug().Str("path", outputPath).Msg("wrote archive to specified path")
|
||||||
|
}
|
||||||
|
case "file":
|
||||||
|
// write to file if '-o' specified otherwise stdout
|
||||||
|
if outputPath != "" {
|
||||||
|
writeFiles(outputPath, body)
|
||||||
|
log.Debug().Str("path", outputPath).Msg("wrote file to specified path")
|
||||||
|
} else {
|
||||||
|
fmt.Println(string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var downloadProfileCmd = &cobra.Command{
|
var downloadProfileCmd = &cobra.Command{
|
||||||
Use: "profile",
|
Use: "profile",
|
||||||
|
Short: "Download a profile",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var downloadPluginCmd = &cobra.Command{
|
var downloadPluginCmd = &cobra.Command{
|
||||||
Use: "plugin",
|
Use: "plugin",
|
||||||
|
Short: "Download a plugin",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
downloadCmd.Flags().String("host", "http://localhost:5050", "Set the makeshift remote host (can be set with MAKESHIFT_HOST)")
|
downloadCmd.Flags().String("host", "http://localhost:5050", "Set the makeshift remote host (can be set with MAKESHIFT_HOST)")
|
||||||
downloadCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
|
downloadCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
|
||||||
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(s) 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 plugin(s) to run before downloading files")
|
||||||
downloadCmd.Flags().Bool("extract", false, "Set whether to extract archive locally after downloading")
|
downloadCmd.Flags().BoolP("extract", "x", false, "Set whether to extract archive locally after downloading")
|
||||||
|
downloadCmd.Flags().BoolP("remove-archive", "r", false, "Set whether to remove the archive after extracting (used with '--extract' flag)")
|
||||||
|
|
||||||
|
downloadCmd.MarkFlagsRequiredTogether("remove-archive", "extract")
|
||||||
|
|
||||||
downloadCmd.AddCommand(downloadProfileCmd, downloadPluginCmd)
|
downloadCmd.AddCommand(downloadProfileCmd, downloadPluginCmd)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.towk2.me/towk/makeshift/internal/archive"
|
"git.towk2.me/towk/makeshift/internal/archive"
|
||||||
makeshift "git.towk2.me/towk/makeshift/pkg"
|
makeshift "git.towk2.me/towk/makeshift/pkg"
|
||||||
|
|
@ -54,7 +53,7 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
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
|
// get the final archive path
|
||||||
archivePath := fmt.Sprintf("%d.tar.gz", time.Now().Unix())
|
archivePath := fmt.Sprintf("%s.tar.gz", path)
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("archive_path", archivePath).
|
Str("archive_path", archivePath).
|
||||||
|
|
@ -98,6 +97,8 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send the archive back as response
|
||||||
|
w.Header().Add("FILETYPE", "archive")
|
||||||
w.Write(contents)
|
w.Write(contents)
|
||||||
|
|
||||||
// clean up the temporary archive
|
// clean up the temporary archive
|
||||||
|
|
@ -128,7 +129,6 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
errs = []error{}
|
errs = []error{}
|
||||||
}
|
}
|
||||||
if len(hooks) > 0 {
|
if len(hooks) > 0 {
|
||||||
|
|
||||||
// run pre-hooks to modify the contents of the file before archiving
|
// run pre-hooks to modify the contents of the file before archiving
|
||||||
log.Debug().Int("hook_count", len(hooks)).Msg("running hooks")
|
log.Debug().Int("hook_count", len(hooks)).Msg("running hooks")
|
||||||
for _, hook := range hooks {
|
for _, hook := range hooks {
|
||||||
|
|
@ -143,7 +143,10 @@ func (s *Service) Download() http.HandlerFunc {
|
||||||
}).Send()
|
}).Send()
|
||||||
err = hook.Run()
|
err = hook.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Str("plugin", hook.Plugin.Name()).Msg("failed to run plugin")
|
log.Error().
|
||||||
|
Err(err).
|
||||||
|
Str("plugin", hook.Plugin.Name()).
|
||||||
|
Msg("failed to run plugin")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -158,8 +161,13 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send processed (with plugins) file back as response
|
||||||
|
w.Header().Add("FILETYPE", "file")
|
||||||
w.Write(data.([]byte))
|
w.Write(data.([]byte))
|
||||||
} else {
|
} else {
|
||||||
|
// send non-processed file back as response
|
||||||
|
w.Header().Add("FILETYPE", "file")
|
||||||
w.Write(contents)
|
w.Write(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -223,7 +231,7 @@ func (s *Service) GetStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
err := json.NewEncoder(w).Encode(map[string]any{
|
err := json.NewEncoder(w).Encode(map[string]any{
|
||||||
"code": http.StatusOK,
|
"code": http.StatusOK,
|
||||||
"message": "Configurator is healthy",
|
"message": "The makeshift server is healthy",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to encode JSON response body: %v\n", err)
|
fmt.Printf("failed to encode JSON response body: %v\n", err)
|
||||||
|
|
@ -274,17 +282,18 @@ func (s *Service) loadPlugins(pluginNames []string, store storage.KVStore, args
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
if i > DEFAULT_PLUGINS_MAX_COUNT {
|
if i > DEFAULT_PLUGINS_MAX_COUNT {
|
||||||
log.Warn().Msg("max plugins count reached...stopping")
|
log.Warn().Msg("max plugins count reached or exceeded...stopping")
|
||||||
return hooks, errs
|
return hooks, errs
|
||||||
}
|
}
|
||||||
if pluginName == "" {
|
if pluginName == "" {
|
||||||
log.Warn().Msg("plugin name is empty...skipping")
|
log.Warn().Msgf("no plugin name found with index %d...skipping", i)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("name", pluginName).
|
Str("name", pluginName).
|
||||||
Str("path", pluginPath).
|
Str("path", pluginPath).
|
||||||
Msg("load plugin")
|
Msg("load plugin")
|
||||||
|
|
||||||
// load the plugin from disk
|
// load the plugin from disk
|
||||||
plugin, err = LoadPluginFromFile(pluginPath)
|
plugin, err = LoadPluginFromFile(pluginPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue