refactor: updated cmd request and added plugin info
This commit is contained in:
parent
4d96010199
commit
0f6f8957f6
3 changed files with 72 additions and 4 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue