feat: added plugin and profile list commands
This commit is contained in:
parent
94887aae9e
commit
947fbba854
2 changed files with 176 additions and 3 deletions
|
|
@ -177,7 +177,7 @@ var downloadProfileCmd = &cobra.Command{
|
||||||
Send()
|
Send()
|
||||||
|
|
||||||
for _, profileID := range args {
|
for _, profileID := range args {
|
||||||
query = fmt.Sprintf("/profile/%s", profileID)
|
query = fmt.Sprintf("/profiles/%s", profileID)
|
||||||
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||||
Path: query,
|
Path: query,
|
||||||
Method: http.MethodGet,
|
Method: http.MethodGet,
|
||||||
|
|
@ -233,7 +233,7 @@ var downloadPluginCmd = &cobra.Command{
|
||||||
Send()
|
Send()
|
||||||
|
|
||||||
for _, pluginName := range args {
|
for _, pluginName := range args {
|
||||||
query = fmt.Sprintf("/plugin/%s", pluginName)
|
query = fmt.Sprintf("/plugins/%s", 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,
|
||||||
|
|
|
||||||
175
cmd/list.go
175
cmd/list.go
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
makeshift "git.towk2.me/towk/makeshift/pkg"
|
||||||
"git.towk2.me/towk/makeshift/pkg/client"
|
"git.towk2.me/towk/makeshift/pkg/client"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
@ -67,9 +68,181 @@ var listCmd = &cobra.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var listPluginsCmd = &cobra.Command{
|
||||||
|
Use: "plugins",
|
||||||
|
Example: `
|
||||||
|
# show all plugins
|
||||||
|
makeshift list plugins
|
||||||
|
|
||||||
|
# show details for specific plugins
|
||||||
|
makeshift list plugins smd jinja2
|
||||||
|
`,
|
||||||
|
Short: "Show plugins information",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
var (
|
||||||
|
host, _ = cmd.Flags().GetString("host")
|
||||||
|
path, _ = cmd.Flags().GetString("path")
|
||||||
|
|
||||||
|
c = client.New(host)
|
||||||
|
res *http.Response
|
||||||
|
plugins []string
|
||||||
|
body []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
// make request to /list endpoint
|
||||||
|
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||||
|
Path: "/plugins",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Str("host", host).
|
||||||
|
Str("path", path).
|
||||||
|
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).
|
||||||
|
Str("path", path).
|
||||||
|
Msg("response returned bad status")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println(string(body))
|
||||||
|
err = json.Unmarshal(body, &plugins)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Msg("failed to unmarshal plugins")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, pluginName := range args {
|
||||||
|
// make request to /list endpoint
|
||||||
|
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||||
|
Path: fmt.Sprintf("/plugins/%s", pluginName),
|
||||||
|
Method: http.MethodGet,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Str("host", host).
|
||||||
|
Str("path", path).
|
||||||
|
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).
|
||||||
|
Str("path", path).
|
||||||
|
Msg("response returned bad status")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins = append(plugins, string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Info().Strs("plugins", plugins).Send()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var listProfilesCmd = &cobra.Command{
|
||||||
|
Use: "profiles",
|
||||||
|
Short: "Show all available profiles",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
var (
|
||||||
|
host, _ = cmd.Flags().GetString("host")
|
||||||
|
path, _ = cmd.Flags().GetString("path")
|
||||||
|
|
||||||
|
c = client.New(host)
|
||||||
|
res *http.Response
|
||||||
|
profiles []makeshift.Profile
|
||||||
|
body []byte
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) == 0 {
|
||||||
|
// make request to /list endpoint
|
||||||
|
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||||
|
Path: "/profiles",
|
||||||
|
Method: http.MethodGet,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Str("host", host).
|
||||||
|
Str("path", path).
|
||||||
|
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).
|
||||||
|
Str("path", path).
|
||||||
|
Msg("response returned bad status")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.Unmarshal(body, &profiles)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Msg("failed to unmarshal plugins")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for _, profileID := range args {
|
||||||
|
// make request to /list endpoint
|
||||||
|
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||||
|
Path: fmt.Sprintf("/profiles/%s", profileID),
|
||||||
|
Method: http.MethodGet,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Str("host", host).
|
||||||
|
Str("path", path).
|
||||||
|
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).
|
||||||
|
Str("path", path).
|
||||||
|
Msg("response returned bad status")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
var profile makeshift.Profile
|
||||||
|
err = json.Unmarshal(body, &profile)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).
|
||||||
|
Msg("failed to unmarshal plugin")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
profiles = append(profiles, profile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Info().Any("plugins", profiles).Send()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
listCmd.Flags().String("host", "http://localhost:5050", "Set the configurator remote host (can be set with MAKESHIFT_HOST)")
|
listCmd.PersistentFlags().String("host", "http://localhost:5050", "Set the configurator remote host (can be set with MAKESHIFT_HOST)")
|
||||||
listCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
|
listCmd.Flags().StringP("path", "p", ".", "Set the path to list files (can be set with MAKESHIFT_PATH)")
|
||||||
|
|
||||||
|
listCmd.AddCommand(listPluginsCmd, listProfilesCmd)
|
||||||
rootCmd.AddCommand(listCmd)
|
rootCmd.AddCommand(listCmd)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue