refactor: updated cmd and pkg implementations
This commit is contained in:
parent
d88ab2c01f
commit
fbed466c3d
10 changed files with 287 additions and 196 deletions
86
cmd/list.go
86
cmd/list.go
|
|
@ -81,10 +81,10 @@ var listPluginsCmd = &cobra.Command{
|
|||
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
|
||||
query string
|
||||
plugins []string
|
||||
body []byte
|
||||
err error
|
||||
|
|
@ -96,24 +96,7 @@ var listPluginsCmd = &cobra.Command{
|
|||
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)
|
||||
}
|
||||
handleResponseError(res, host, "/plugins", err)
|
||||
err = json.Unmarshal(body, &plugins)
|
||||
if err != nil {
|
||||
log.Error().Err(err).
|
||||
|
|
@ -123,28 +106,12 @@ var listPluginsCmd = &cobra.Command{
|
|||
} else {
|
||||
for _, pluginName := range args {
|
||||
// make request to /list endpoint
|
||||
query = fmt.Sprintf("/plugins/%s/info", pluginName)
|
||||
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||
Path: fmt.Sprintf("/plugins/%s/info", pluginName),
|
||||
Path: query,
|
||||
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)
|
||||
}
|
||||
handleResponseError(res, host, query, err)
|
||||
|
||||
plugins = append(plugins, string(body))
|
||||
}
|
||||
|
|
@ -159,12 +126,12 @@ var listProfilesCmd = &cobra.Command{
|
|||
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
|
||||
query string
|
||||
err error
|
||||
)
|
||||
|
||||
|
|
@ -174,24 +141,7 @@ var listProfilesCmd = &cobra.Command{
|
|||
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)
|
||||
}
|
||||
handleResponseError(res, host, "/profiles", err)
|
||||
|
||||
err = json.Unmarshal(body, &profiles)
|
||||
if err != nil {
|
||||
|
|
@ -202,28 +152,12 @@ var listProfilesCmd = &cobra.Command{
|
|||
} else {
|
||||
for _, profileID := range args {
|
||||
// make request to /list endpoint
|
||||
query = fmt.Sprintf("/profiles/%s", profileID)
|
||||
res, body, err = c.MakeRequest(client.HTTPEnvelope{
|
||||
Path: fmt.Sprintf("/profiles/%s", profileID),
|
||||
Path: fmt.Sprintf(query),
|
||||
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)
|
||||
}
|
||||
handleResponseError(res, host, query, err)
|
||||
var profile makeshift.Profile
|
||||
err = json.Unmarshal(body, &profile)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue