diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..04f9700 --- /dev/null +++ b/cmd/list.go @@ -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 +} diff --git a/cmd/root.go b/cmd/root.go index 80f6606..3a5d6d0 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,6 +30,7 @@ func NewRootCmd(version string) *cobra.Command { cmd.AddCommand(logoutCmd()) cmd.AddCommand(pushCmd()) cmd.AddCommand(getCmd()) + cmd.AddCommand(listCmd()) return cmd } diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 28a7267..daa881f 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -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"), + 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( diff --git a/pkg/cloud/types.go b/pkg/cloud/types.go index 492df54..45e8689 100644 --- a/pkg/cloud/types.go +++ b/pkg/cloud/types.go @@ -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"` +}