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/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.towk2.me/towk/makeshift/pkg/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
|
@ -30,7 +28,7 @@ var downloadCmd = cobra.Command{
|
|||
curl $MAKESHIFT_HOST/download/test?plugins=smd,jinja2&profile=test
|
||||
|
||||
# download directory and extract it's contents automatically
|
||||
makeshift download --extract
|
||||
makeshift download -xr
|
||||
`,
|
||||
Short: "Download and modify files with plugins",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
|
|
@ -52,15 +50,6 @@ var downloadCmd = cobra.Command{
|
|||
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)
|
||||
if len(pluginNames) > 0 {
|
||||
query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ","))
|
||||
|
|
@ -102,33 +91,66 @@ var downloadCmd = cobra.Command{
|
|||
Msg("response returned bad status")
|
||||
os.Exit(1)
|
||||
}
|
||||
if outputPath != "" {
|
||||
|
||||
// helper to write downloaded files
|
||||
var writeFiles = func(path string, body []byte) {
|
||||
err = os.WriteFile(outputPath, body, 0o755)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to write file(s) from download")
|
||||
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{
|
||||
Use: "profile",
|
||||
Use: "profile",
|
||||
Short: "Download a profile",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
var downloadPluginCmd = &cobra.Command{
|
||||
Use: "plugin",
|
||||
Use: "plugin",
|
||||
Short: "Download a plugin",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
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("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("plugins", []string{}, "Set the plugins to run before downloading files")
|
||||
downloadCmd.Flags().Bool("extract", false, "Set whether to extract archive locally after downloading")
|
||||
downloadCmd.Flags().StringSlice("profiles", []string{}, "Set the profile(s) to use to populate data store")
|
||||
downloadCmd.Flags().StringSlice("plugins", []string{}, "Set the plugin(s) to run before downloading files")
|
||||
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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue