/
list_versions.go
131 lines (113 loc) · 3.73 KB
/
list_versions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright Mia srl
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package marketplace
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"github.com/mia-platform/miactl/internal/client"
"github.com/mia-platform/miactl/internal/clioptions"
"github.com/mia-platform/miactl/internal/resources/marketplace"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
const listItemVersionsEndpointTemplate = "/api/backend/marketplace/tenants/%s/resources/%s/versions"
var (
ErrGenericServerError = errors.New("server error while fetching item versions")
)
// ListVersionCmd return a new cobra command for listing marketplace item versions
func ListVersionCmd(options *clioptions.CLIOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "list-versions",
Short: "List versions of a Marketplace item (ALPHA)",
Long: `List the currently available versions of a Marketplace item.
The command will output a table with each version of the item.
This command is in ALPHA state. This means that it can be subject to breaking changes in the next versions of miactl.`,
Run: func(cmd *cobra.Command, _ []string) {
restConfig, err := options.ToRESTConfig()
cobra.CheckErr(err)
client, err := client.APIClientForConfig(restConfig)
cobra.CheckErr(err)
releases, err := getItemVersions(
cmd.Context(),
client,
restConfig.CompanyID,
options.MarketplaceItemID,
)
cobra.CheckErr(err)
table := buildItemVersionListTable(releases)
fmt.Println(table)
},
}
flagName := options.AddMarketplaceItemIDFlag(cmd.Flags())
err := cmd.MarkFlagRequired(flagName)
if err != nil {
// the error is only due to a programming error (missing command flag), hence panic
panic(err)
}
return cmd
}
func getItemVersions(ctx context.Context, client *client.APIClient, companyID, itemID string) (*[]marketplace.Release, error) {
if companyID == "" {
return nil, marketplace.ErrMissingCompanyID
}
resp, err := client.
Get().
APIPath(
fmt.Sprintf(listItemVersionsEndpointTemplate, companyID, itemID),
).
Do(ctx)
if err != nil {
return nil, fmt.Errorf("error executing request: %w", err)
}
switch resp.StatusCode() {
case http.StatusOK:
releases := &[]marketplace.Release{}
err = resp.ParseResponse(releases)
if err != nil {
return nil, fmt.Errorf("error parsing response body: %w", err)
}
return releases, nil
case http.StatusNotFound:
return nil, fmt.Errorf("%w: %s", marketplace.ErrItemNotFound, itemID)
}
return nil, ErrGenericServerError
}
func buildItemVersionListTable(releases *[]marketplace.Release) string {
strBuilder := &strings.Builder{}
table := tablewriter.NewWriter(strBuilder)
table.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false})
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetAutoWrapText(false)
table.SetHeader([]string{"Version", "Name", "Description"})
for _, release := range *releases {
description := "-"
if release.Description != "" {
description = release.Description
}
table.Append([]string{
release.Version,
release.Name,
description,
})
}
table.Render()
return strBuilder.String()
}