-
Notifications
You must be signed in to change notification settings - Fork 7
/
show.go
86 lines (73 loc) · 2.7 KB
/
show.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
package databaseproperties
import (
"fmt"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-go-api/v6/upcloud/request"
)
// ShowCommand creates the "database properties <serviceType> show" command
func ShowCommand(serviceType string, serviceName string) commands.Command {
return &showCommand{
BaseCommand: commands.New("show", fmt.Sprintf("Show %s database property details", serviceName), fmt.Sprintf("upctl database properties %s show version", serviceType)),
serviceType: serviceType,
DatabaseProperty: completion.DatabaseProperty{ServiceType: serviceType},
}
}
type showCommand struct {
*commands.BaseCommand
completion.DatabaseProperty
serviceType string
}
// Execute implements commands.MultipleArgumentCommand
func (s *showCommand) Execute(exec commands.Executor, key string) (output.Output, error) {
svc := exec.All()
dbType, err := svc.GetManagedDatabaseServiceType(exec.Context(), &request.GetManagedDatabaseServiceTypeRequest{Type: s.serviceType})
if err != nil {
return nil, err
}
details, ok := dbType.Properties[key]
if !ok {
return nil, fmt.Errorf(`no property "%s" available for %s database`, key, s.serviceType)
}
rows := []output.DetailRow{
{Title: "Key:", Key: "key", Value: key},
{Title: "Title:", Key: "title", Value: details.Title},
{Title: "Description:", Key: "description", Value: details.Description},
{Title: "Help message:", Key: "user_error", Value: details.UserError},
{Title: "Create only:", Key: "createOnly", Value: details.CreateOnly, Format: format.Boolean},
{Title: "Type:", Key: "type", Value: details.Type, Format: formatAlternatives},
{Title: "Default:", Key: "default", Value: details.Default},
{Title: "Possible values:", Key: "enum", Value: details.Enum, Format: formatAlternatives},
{Title: "Pattern:", Key: "pattern", Value: details.Pattern},
{Title: "Max length:", Key: "maxLength", Value: details.MaxLength},
{Title: "Min length:", Key: "minLength", Value: details.MinLength},
}
return output.MarshaledWithHumanOutput{
Value: details,
Output: output.Details{
Sections: []output.DetailSection{
{
Rows: filterOutEmptyRows(rows),
},
},
},
}, nil
}
func filterOutEmptyRows(rows []output.DetailRow) []output.DetailRow {
nonEmpty := []output.DetailRow{}
for _, row := range rows {
if row.Value == nil {
continue
}
if val, ok := row.Value.(string); ok && val == "" {
continue
}
if val, ok := row.Value.(int); ok && val == 0 {
continue
}
nonEmpty = append(nonEmpty, row)
}
return nonEmpty
}