From 0f6f8957f6370ad68ef0c65af20089d7d0e1d8d2 Mon Sep 17 00:00:00 2001 From: David Allen Date: Sat, 30 Aug 2025 00:04:17 -0600 Subject: [PATCH] refactor: updated cmd request and added plugin info --- cmd/download.go | 4 +-- cmd/plugins.go | 59 ++++++++++++++++++++++++++++++++++++++++-- pkg/service/plugins.go | 13 ++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/cmd/download.go b/cmd/download.go index 0649e5b..25e87aa 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -214,7 +214,7 @@ var downloadPluginCmd = &cobra.Command{ makeshift download plugin smd jinja2 `, Args: cobra.ExactArgs(1), - Short: "Download a plugin", + Short: "Download a raw plugin", Run: func(cmd *cobra.Command, args []string) { var ( host, _ = cmd.Flags().GetString("host") @@ -233,7 +233,7 @@ var downloadPluginCmd = &cobra.Command{ Send() for _, pluginName := range args { - query = fmt.Sprintf("/plugins/%s", pluginName) + query = fmt.Sprintf("/plugins/%s/raw", pluginName) res, body, err = c.MakeRequest(client.HTTPEnvelope{ Path: query, Method: http.MethodGet, diff --git a/cmd/plugins.go b/cmd/plugins.go index 3078e43..0308125 100644 --- a/cmd/plugins.go +++ b/cmd/plugins.go @@ -3,11 +3,13 @@ package cmd import ( "fmt" "io/fs" + "net/http" "os" "os/exec" "path/filepath" makeshift "git.towk2.me/towk/makeshift/pkg" + "git.towk2.me/towk/makeshift/pkg/client" "git.towk2.me/towk/makeshift/pkg/service" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -15,7 +17,7 @@ import ( var pluginsCmd = &cobra.Command{ 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{ @@ -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() { pluginsCompileCmd.Flags().StringP("output", "o", "", "Set the path to save compiled plugin") - pluginsCmd.AddCommand(pluginsCompileCmd, pluginInspectCmd) + pluginsCmd.AddCommand(pluginsCompileCmd, pluginInspectCmd, pluginInfoCmd) rootCmd.AddCommand(pluginsCmd) } diff --git a/pkg/service/plugins.go b/pkg/service/plugins.go index 0e1efc5..a8b0b6e 100644 --- a/pkg/service/plugins.go +++ b/pkg/service/plugins.go @@ -69,7 +69,20 @@ func (s *Service) GetPluginInfo() http.HandlerFunc { func (s *Service) GetPluginRaw() http.HandlerFunc { 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) } }