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

@ -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)
}