Skip to content

Commit

Permalink
Add list subcommand to list extensions on KubeSphere Cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Jun 6, 2024
1 parent bee4408 commit 0d01192
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
52 changes: 52 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"

"github.com/kubesphere/ksbuilder/pkg/cloud"
"github.com/kubesphere/ksbuilder/pkg/utils"
)

type listOptions struct{}

func listCmd() *cobra.Command {
o := listOptions{}

cmd := &cobra.Command{
Use: "list",
Short: "List all extensions of the current user on KubeSphere Cloud",
Args: cobra.NoArgs,
RunE: o.list,
}
return cmd
}

func (o *listOptions) list(_ *cobra.Command, _ []string) error {
client, err := cloud.NewClient()
if err != nil {
return fmt.Errorf("login failed: %v", err)
}

extensions, err := client.ListExtensions()
if err != nil {
return err
}
rows := make([]table.Row, 0)
for _, extension := range extensions.Extensions {
rows = append(rows, table.Row{
extension.ExtensionID,
extension.Name,
extension.Status,
extension.LatestVersion.Version,
})
}

t := utils.NewTableWriter()
t.AppendHeader(table.Row{"ID", "Name", "Status", "Latest version"})
t.AppendRows(rows)
t.Render()
return nil
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewRootCmd(version string) *cobra.Command {
cmd.AddCommand(logoutCmd())
cmd.AddCommand(pushCmd())
cmd.AddCommand(getCmd())
cmd.AddCommand(listCmd())

return cmd
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,23 @@ func (c *Client) SubmitExtension(snapshotID string) error {
)
}

func (c *Client) ListExtensions() (*ListExtensionsResponse, error) {
body := &bytes.Buffer{}
body.WriteString(fmt.Sprintf(`{"developer_ids": ["%s"], "statuses": ["*"]}`, c.userID))

data := &ListExtensionsResponse{}
if err := c.sendRequest(
http.MethodPost,
fmt.Sprintf("/apis/extension/v1/extensions/search"),

Check failure on line 217 in pkg/cloud/cloud.go

View workflow job for this annotation

GitHub Actions / Lint and unit test

S1039: unnecessary use of fmt.Sprintf (gosimple)
body,
map[string]string{"Content-Type": "application/json"},
data,
); err != nil {
return nil, err
}
return data, nil
}

func (c *Client) GetExtension(extensionName string) (*Extension, error) {
data := &Extension{}
if err := c.sendRequest(
Expand Down
5 changes: 5 additions & 0 deletions pkg/cloud/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,13 @@ type Snapshot struct {

type Extension struct {
ExtensionID string `json:"extension_id"`
Name string `json:"name"`
Status string `json:"status"`
LatestVersion struct {
Version string `json:"version"`
} `json:"latest_version"`
}

type ListExtensionsResponse struct {
Extensions []Extension `json:"extensions"`
}

0 comments on commit 0d01192

Please sign in to comment.