feat: updated implementations for cmds

This commit is contained in:
David Allen 2025-08-24 20:39:39 -06:00
parent 7a96bfd6c7
commit 59a5225b28
Signed by: towk
GPG key ID: 0430CDBE22619155
7 changed files with 331 additions and 102 deletions

View file

@ -3,11 +3,13 @@ package cmd
import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"git.towk2.me/towk/configurator/pkg/client"
"git.towk2.me/towk/makeshift/pkg/client"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
@ -15,30 +17,40 @@ import (
var downloadCmd = cobra.Command{
Use: "download",
Example: `
# download a file or directory (as archive)
configurator download
configurator download --host https://example.com --path test
configurator download --plugins smd,jinja2 --profile compute
curl $CONFIGURATOR_HOST/download/test?plugins=smd,jinja2
# set up environment
export MAKESHIFT_HOST=http://localhost:5050
export MAKESHIFT_PATH=test
# download a file or directory (as archive)
makeshift download
makeshift download --host http://localhost:5050.com --path test
# download a file or directory and run plugins with profile data
makeshift download --plugins smd,jinja2 --profile compute
curl $MAKESHIFT_HOST/download/test?plugins=smd,jinja2&profile=test
# download directory and extract it's contents automatically
makeshift download --extract
`,
Short: "Download and modify files with plugins",
PreRun: func(cmd *cobra.Command, args []string) {
setenv(&host, "CONFIGURATOR_HOST")
setenv(&path, "CONFIGURATOR_PATH")
setenv(cmd, "host", "MAKESHIFT_HOST")
setenv(cmd, "path", "MAKESHIFT_PATH")
},
Run: func(cmd *cobra.Command, args []string) {
var (
c = client.New(host)
res *http.Response
body []byte
err error
)
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")
log.Debug().
Str("host", host).
Str("path", path).
Str("output", outputPath).
Send()
c = client.New(host)
res *http.Response
query string
body []byte
err error
)
// set output path to match path if empty
if outputPath == "" {
@ -49,22 +61,42 @@ var downloadCmd = cobra.Command{
}
}
// make request to /download endpoint
// _, err = c.Download(outputPath, client.HTTPEnvelope{
// Path: fmt.Sprintf("/download/%s", path),
// Method: http.MethodGet,
// })
query = fmt.Sprintf("/download/%s?", path)
if len(pluginNames) > 0 {
query += "plugins=" + url.QueryEscape(strings.Join(pluginNames, ","))
}
if len(profileIDs) > 0 {
query += "&profiles=" + url.QueryEscape(strings.Join(profileIDs, ","))
}
log.Debug().
Str("host", host).
Str("path", path).
Str("query", query).
Str("output", outputPath).
Strs("profiles", profileIDs).
Strs("plugins", pluginNames).
Send()
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: fmt.Sprintf("/download/%s", path),
Path: query,
Method: http.MethodGet,
})
if err != nil {
log.Error().Err(err).Str("host", host).Msg("failed to make request")
log.Error().Err(err).
Str("host", host).
Str("path", path).
Str("output", outputPath).
Msg("failed to make request")
os.Exit(1)
}
if res.StatusCode != http.StatusOK {
log.Error().Int("status", res.StatusCode).Str("host", host).Msg("response returned bad status")
log.Error().
Int("status", res.StatusCode).
Str("host", host).
Str("path", path).
Str("output", outputPath).
Msg("response returned bad status")
os.Exit(1)
}
if outputPath != "" {
@ -80,12 +112,15 @@ var downloadCmd = cobra.Command{
},
}
var downloadProfileCmd = &cobra.Command{}
var downloadPluginCmd = &cobra.Command{}
func init() {
downloadCmd.Flags().StringVar(&host, "host", "http://localhost:5050", "Set the configurator remote host (can be set with CONFIGURATOR_HOST)")
downloadCmd.Flags().StringVarP(&path, "path", "p", ".", "Set the path to list files (can be set with CONFIGURATOR_PATH)")
downloadCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Set the output path to write files")
downloadCmd.Flags().StringVar(&profile, "profile", "", "Set the profile to use to populate data store")
downloadCmd.Flags().StringSliceVar(&plugins, "plugins", []string{}, "Set the plugins to run before downloading files")
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")
rootCmd.AddCommand(&downloadCmd)
}