feat: allow expanding and remove archive after download

This commit is contained in:
David Allen 2025-08-29 11:04:16 -06:00
parent c2d5be5eed
commit e5c1b59bc1
Signed by: towk
GPG key ID: 0430CDBE22619155
2 changed files with 223 additions and 19 deletions

View file

@ -5,8 +5,10 @@ import (
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"git.towk2.me/towk/makeshift/internal/archive"
"git.towk2.me/towk/makeshift/pkg/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
@ -37,11 +39,13 @@ var downloadCmd = cobra.Command{
},
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
path, _ = cmd.Flags().GetString("path")
outputPath, _ = cmd.Flags().GetString("output")
pluginNames, _ = cmd.Flags().GetStringSlice("plugins")
profileIDs, _ = cmd.Flags().GetStringSlice("profiles")
host, _ = cmd.Flags().GetString("host")
path, _ = cmd.Flags().GetString("path")
outputPath, _ = cmd.Flags().GetString("output")
pluginNames, _ = cmd.Flags().GetStringSlice("plugins")
profileIDs, _ = cmd.Flags().GetStringSlice("profiles")
extract, _ = cmd.Flags().GetBool("extract")
removeArchive, _ = cmd.Flags().GetBool("remove-archive")
c = client.New(host)
res *http.Response
@ -92,15 +96,6 @@ var downloadCmd = cobra.Command{
os.Exit(1)
}
// 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)
}
}
// determine if output path is an archive or file
switch res.Header.Get("FILETYPE") {
case "archive":
@ -113,6 +108,36 @@ var downloadCmd = cobra.Command{
writeFiles(outputPath, body)
log.Debug().Str("path", outputPath).Msg("wrote archive to specified path")
}
// extract files if '-x' flag is passed
if extract {
var (
dir = filepath.Dir(outputPath)
base = strings.TrimSuffix(filepath.Base(outputPath), ".tar.gz")
)
err = archive.Expand(outputPath, fmt.Sprintf("%s/%s", dir, base))
if err != nil {
log.Error().Err(err).
Str("path", outputPath).
Msg("failed to expand archive")
os.Exit(1)
}
}
// optionally, remove archive if '-r' flag is passed
// NOTE: this can only be used if `-x` flag is set
if removeArchive {
if !extract {
log.Warn().Msg("requires '-x/--extract' flag to be set to 'true'")
} else {
err = os.Remove(outputPath)
if err != nil {
log.Error().Err(err).
Str("path", outputPath).
Msg("failed to remove archive")
}
}
}
case "file":
// write to file if '-o' specified otherwise stdout
if outputPath != "" {
@ -126,18 +151,111 @@ var downloadCmd = cobra.Command{
}
var downloadProfileCmd = &cobra.Command{
Use: "profile",
Use: "profile",
Example: `
// download a profile
makeshift download profile default
`,
Args: cobra.ExactArgs(1),
Short: "Download a profile",
PreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH")
},
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
outputPath, _ = cmd.Flags().GetString("output")
c = client.New(host)
res *http.Response
body []byte
query string
err error
)
for _, profileID := range args {
query = fmt.Sprintf("/profile/{%s}", profileID)
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query,
Method: http.MethodGet,
})
if err != nil {
log.Error().Err(err).
Str("host", host).
Msg("failed to make request")
os.Exit(1)
}
if res.StatusCode != http.StatusOK {
log.Error().
Any("status", map[string]any{
"code": res.StatusCode,
"message": res.Status,
}).
Str("host", host).
Msg("response returned bad status")
os.Exit(1)
}
if outputPath != "" {
writeFiles(outputPath, body)
} else {
fmt.Println(string(body))
}
}
},
}
var downloadPluginCmd = &cobra.Command{
Use: "plugin",
Use: "plugin",
Example: `
// download a plugin
makeshift download plugin smd jinja2
`,
Args: cobra.ExactArgs(1),
Short: "Download a plugin",
PreRun: func(cmd *cobra.Command, args []string) {
setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH")
},
Run: func(cmd *cobra.Command, args []string) {
var (
host, _ = cmd.Flags().GetString("host")
outputPath, _ = cmd.Flags().GetString("output")
c = client.New(host)
res *http.Response
query string
body []byte
err error
)
for _, pluginName := range args {
query = fmt.Sprintf("/profile/%s?", pluginName)
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query,
Method: http.MethodGet,
})
if err != nil {
log.Error().Err(err).
Str("host", host).
Msg("failed to make request")
os.Exit(1)
}
if res.StatusCode != http.StatusOK {
log.Error().
Any("status", map[string]any{
"code": res.StatusCode,
"message": res.Status,
}).
Str("host", host).
Msg("response returned bad status")
os.Exit(1)
}
if outputPath != "" {
writeFiles(outputPath, body)
} else {
writeFiles(fmt.Sprintf("%s.so", pluginName), body)
}
}
},
}
@ -150,9 +268,16 @@ func init() {
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)
rootCmd.AddCommand(&downloadCmd)
}
// helper to write downloaded files
func writeFiles(path string, body []byte) {
var err = os.WriteFile(path, body, 0o755)
if err != nil {
log.Error().Err(err).Msg("failed to write file(s) from download")
os.Exit(1)
}
}