-
Notifications
You must be signed in to change notification settings - Fork 515
/
license-info.go
236 lines (201 loc) · 6.43 KB
/
license-info.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"fmt"
"net/http"
"time"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
"github.com/fatih/color"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/minio/mc/pkg/probe"
"github.com/minio/pkg/v2/console"
)
var licenseInfoCmd = cli.Command{
Name: "info",
Usage: "display license information",
OnUsageError: onUsageError,
Action: mainLicenseInfo,
Before: setGlobalsFromContext,
Flags: subnetCommonFlags,
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} ALIAS
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Display license configuration for cluster with alias 'play'
{{.Prompt}} {{.HelpName}} play
`,
}
const (
licInfoMsgTag = "licenseInfoMessage"
licInfoErrTag = "licenseInfoError"
licInfoFieldTag = "licenseInfoField"
licInfoValTag = "licenseValueField"
)
type licInfoMessage struct {
Status string `json:"status"`
Info licInfo `json:"info,omitempty"`
Error string `json:"error,omitempty"`
}
type licInfo struct {
LicenseID string `json:"license_id,omitempty"` // Unique ID of the license
Organization string `json:"org,omitempty"` // Subnet organization name
Plan string `json:"plan,omitempty"` // Subnet plan
IssuedAt *time.Time `json:"issued_at,omitempty"` // Time of license issue
ExpiresAt *time.Time `json:"expires_at,omitempty"` // Time of license expiry
DeploymentID string `json:"deployment_id,omitempty"` // Cluster deployment ID
Message string `json:"message,omitempty"` // Message to be displayed
APIKey string `json:"api_key,omitempty"` // API Key of the org account
}
func licInfoField(s string) string {
return console.Colorize(licInfoFieldTag, s)
}
func licInfoVal(s string) string {
return console.Colorize(licInfoValTag, s)
}
func licInfoMsg(s string) string {
return console.Colorize(licInfoMsgTag, s)
}
func licInfoErr(s string) string {
return console.Colorize(licInfoErrTag, s)
}
// String colorized license info
func (li licInfoMessage) String() string {
if len(li.Error) > 0 {
return licInfoErr(li.Error)
}
if len(li.Info.Message) > 0 {
return licInfoMsg(li.Info.Message)
}
return getLicInfoStr(li.Info)
}
// JSON jsonified license info
func (li licInfoMessage) JSON() string {
jsonBytes, e := json.MarshalIndent(li, "", " ")
fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
return string(jsonBytes)
}
func getLicInfoStr(li licInfo) string {
columns := []table.Column{
{Title: "License", Width: 20},
{Title: "", Width: 45},
}
rows := []table.Row{
{licInfoField("Organization"), licInfoVal(li.Organization)},
{licInfoField("Plan"), licInfoVal(li.Plan)},
{licInfoField("Issued"), licInfoVal(li.IssuedAt.Format(http.TimeFormat))},
{licInfoField("Expires"), licInfoVal(li.ExpiresAt.Format(http.TimeFormat))},
}
if len(li.LicenseID) > 0 {
rows = append(rows, table.Row{licInfoField("License ID"), licInfoVal(li.LicenseID)})
}
if len(li.DeploymentID) > 0 {
rows = append(rows, table.Row{licInfoField("Deployment ID"), licInfoVal(li.DeploymentID)})
}
if len(li.APIKey) > 0 {
rows = append(rows, table.Row{licInfoField("API Key"), licInfoVal(li.APIKey)})
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.Bold(false)
t.SetStyles(s)
return lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).Render(t.View())
}
func getAGPLMessage() string {
return `License: GNU AGPL v3 <https://www.gnu.org/licenses/agpl-3.0.txt>
If you are distributing or hosting MinIO along with your proprietary application as combined works, you may require a commercial license included in the Standard and Enterprise subscription plans. (https://min.io/signup?ref=mc)`
}
func initLicInfoColors() {
console.SetColor(licInfoMsgTag, color.New(color.FgGreen, color.Bold))
console.SetColor(licInfoErrTag, color.New(color.FgRed, color.Bold))
console.SetColor(licInfoFieldTag, color.New(color.FgCyan))
console.SetColor(licInfoValTag, color.New(color.FgWhite))
}
func mainLicenseInfo(ctx *cli.Context) error {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, 1) // last argument is exit code
}
initLicInfoColors()
aliasedURL := ctx.Args().Get(0)
alias, _ := initSubnetConnectivity(ctx, aliasedURL, true)
apiKey, lic, e := getSubnetCreds(alias)
fatalIf(probe.NewError(e), "Error in checking cluster registration status")
var lim licInfoMessage
if len(lic) > 0 {
lim = getLicInfoMsg(lic)
} else if len(apiKey) > 0 {
lim = licInfoMessage{
Status: "success",
Info: licInfo{
Message: fmt.Sprintf("%s is registered with SUBNET. License info not available.", alias),
},
}
} else {
// Not registered. Default to AGPLv3
lim = licInfoMessage{
Status: "success",
Info: licInfo{
Plan: "AGPLv3",
Message: getAGPLMessage(),
},
}
}
printMsg(lim)
return nil
}
func getLicInfoMsg(lic string) licInfoMessage {
li, e := parseLicense(lic)
if e != nil {
return licErrMsg(e)
}
return licInfoMessage{
Status: "success",
Info: licInfo{
LicenseID: li.LicenseID,
Organization: li.Organization,
Plan: li.Plan,
IssuedAt: &li.IssuedAt,
ExpiresAt: &li.ExpiresAt,
DeploymentID: li.DeploymentID,
APIKey: li.APIKey,
},
}
}
func licErrMsg(e error) licInfoMessage {
return licInfoMessage{
Status: "error",
Error: e.Error(),
}
}