refactor: updated cmd request and added plugin info

This commit is contained in:
David Allen 2025-08-30 00:04:17 -06:00
parent 4d96010199
commit 0f6f8957f6
Signed by: towk
GPG key ID: 0430CDBE22619155
3 changed files with 72 additions and 4 deletions

View file

@ -214,7 +214,7 @@ var downloadPluginCmd = &cobra.Command{
makeshift download plugin smd jinja2 makeshift download plugin smd jinja2
`, `,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Short: "Download a plugin", Short: "Download a raw plugin",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
var ( var (
host, _ = cmd.Flags().GetString("host") host, _ = cmd.Flags().GetString("host")
@ -233,7 +233,7 @@ var downloadPluginCmd = &cobra.Command{
Send() Send()
for _, pluginName := range args { for _, pluginName := range args {
query = fmt.Sprintf("/plugins/%s", pluginName) query = fmt.Sprintf("/plugins/%s/raw", pluginName)
res, body, err = c.MakeRequest(client.HTTPEnvelope{ res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query, Path: query,
Method: http.MethodGet, Method: http.MethodGet,

View file

@ -3,11 +3,13 @@ package cmd
import ( import (
"fmt" "fmt"
"io/fs" "io/fs"
"net/http"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
makeshift "git.towk2.me/towk/makeshift/pkg" makeshift "git.towk2.me/towk/makeshift/pkg"
"git.towk2.me/towk/makeshift/pkg/client"
"git.towk2.me/towk/makeshift/pkg/service" "git.towk2.me/towk/makeshift/pkg/service"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -15,7 +17,7 @@ import (
var pluginsCmd = &cobra.Command{ var pluginsCmd = &cobra.Command{
Use: "plugins", Use: "plugins",
Short: "Manage and compile plugins (requires Go build tools)", Short: "Manage, inspect, and compile plugins (requires Go build tools)",
} }
var pluginsCompileCmd = &cobra.Command{ var pluginsCompileCmd = &cobra.Command{
@ -118,9 +120,62 @@ var pluginInspectCmd = &cobra.Command{
}, },
} }
var pluginInfoCmd = &cobra.Command{
Use: "info",
Short: "Show plugin information",
Args: cobra.MinimumNArgs(1),
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
)
log.Debug().
Str("host", host).
Str("output", outputPath).
Send()
for _, pluginName := range args {
query = fmt.Sprintf("/plugins/%s/info", pluginName)
res, body, err = c.MakeRequest(client.HTTPEnvelope{
Path: query,
Method: http.MethodGet,
})
if err != nil {
log.Error().Err(err).
Str("host", host).
Str("query", query).
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))
}
}
},
}
func init() { func init() {
pluginsCompileCmd.Flags().StringP("output", "o", "", "Set the path to save compiled plugin") pluginsCompileCmd.Flags().StringP("output", "o", "", "Set the path to save compiled plugin")
pluginsCmd.AddCommand(pluginsCompileCmd, pluginInspectCmd) pluginsCmd.AddCommand(pluginsCompileCmd, pluginInspectCmd, pluginInfoCmd)
rootCmd.AddCommand(pluginsCmd) rootCmd.AddCommand(pluginsCmd)
} }

View file

@ -69,7 +69,20 @@ func (s *Service) GetPluginInfo() http.HandlerFunc {
func (s *Service) GetPluginRaw() http.HandlerFunc { func (s *Service) GetPluginRaw() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var (
pluginName = chi.URLParam(r, "name")
path = s.PathForPluginWithName(pluginName)
rawPlugin []byte
err error
)
rawPlugin, err = os.ReadFile(path)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Write(rawPlugin)
} }
} }