forked from redhat-developer/odo
/
service.go
143 lines (121 loc) · 4.57 KB
/
service.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
package describe
import (
"fmt"
"github.com/olekukonko/tablewriter"
"github.com/openshift/odo/pkg/odo/genericclioptions"
svc "github.com/openshift/odo/pkg/service"
"github.com/spf13/cobra"
ktemplates "k8s.io/kubectl/pkg/util/templates"
"os"
"strings"
)
const serviceRecommendedCommandName = "service"
var (
serviceExample = ktemplates.Examples(` # Describe a service
%[1]s mysql-persistent`)
serviceLongDesc = ktemplates.LongDesc(`Describe a service type.
This describes the service and the associated plans.
`)
)
// DescribeServiceOptions encapsulates the options for the odo catalog describe service command
type DescribeServiceOptions struct {
// name of the service to describe, from command arguments
serviceName string
// resolved service
service svc.ServiceClass
plans []svc.ServicePlan
// generic context options common to all commands
*genericclioptions.Context
}
// NewDescribeServiceOptions creates a new DescribeServiceOptions instance
func NewDescribeServiceOptions() *DescribeServiceOptions {
return &DescribeServiceOptions{}
}
// Complete completes DescribeServiceOptions after they've been created
func (o *DescribeServiceOptions) Complete(name string, cmd *cobra.Command, args []string) (err error) {
o.Context = genericclioptions.NewContext(cmd, true)
o.serviceName = args[0]
return
}
// Validate validates the DescribeServiceOptions based on completed values
func (o *DescribeServiceOptions) Validate() (err error) {
o.service, o.plans, err = svc.GetServiceClassAndPlans(o.Client, o.serviceName)
return err
}
// Run contains the logic for the command associated with DescribeServiceOptions
func (o *DescribeServiceOptions) Run() (err error) {
table := tablewriter.NewWriter(os.Stdout)
table.SetBorder(false)
table.SetAlignment(tablewriter.ALIGN_LEFT)
serviceData := [][]string{
{"Name", o.service.Name},
{"Bindable", fmt.Sprint(o.service.Bindable)},
{"Operated by the broker", o.service.ServiceBrokerName},
{"Short Description", o.service.ShortDescription},
{"Long Description", o.service.LongDescription},
{"Versions Available", strings.Join(o.service.VersionsAvailable, ",")},
{"Tags", strings.Join(o.service.Tags, ",")},
}
table.AppendBulk(serviceData)
table.Append([]string{""})
if len(o.plans) > 0 {
table.Append([]string{"PLANS"})
for _, plan := range o.plans {
// create the display values for required and optional parameters
requiredWithMandatoryUserInputParameterNames := []string{}
requiredWithOptionalUserInputParameterNames := []string{}
optionalParameterDisplay := []string{}
for _, parameter := range plan.Parameters {
if parameter.Required {
// until we have a better solution for displaying the plan data (like a separate table perhaps)
// this is simplest thing to do
if len(parameter.Default) > 0 {
requiredWithOptionalUserInputParameterNames = append(
requiredWithOptionalUserInputParameterNames,
fmt.Sprintf("%s (default: '%s')", parameter.Name, parameter.Default))
} else {
requiredWithMandatoryUserInputParameterNames = append(requiredWithMandatoryUserInputParameterNames, parameter.Name)
}
} else {
optionalParameterDisplay = append(optionalParameterDisplay, parameter.Name)
}
}
table.Append([]string{"***********************", "*****************************************************"})
planLineSeparator := []string{"-----------------", "-----------------"}
planData := [][]string{
{"Name", plan.Name},
planLineSeparator,
{"Display Name", plan.DisplayName},
planLineSeparator,
{"Short Description", plan.Description},
planLineSeparator,
{"Required Params without a default value", strings.Join(requiredWithMandatoryUserInputParameterNames, ", ")},
planLineSeparator,
{"Required Params with a default value", strings.Join(requiredWithOptionalUserInputParameterNames, ", ")},
planLineSeparator,
{"Optional Params", strings.Join(optionalParameterDisplay, ", ")},
{"", ""},
}
table.AppendBulk(planData)
}
table.Render()
} else {
return fmt.Errorf("no plans found for service %s", o.serviceName)
}
return
}
// NewCmdCatalogDescribeService implements the odo catalog describe service command
func NewCmdCatalogDescribeService(name, fullName string) *cobra.Command {
o := NewDescribeServiceOptions()
command := &cobra.Command{
Use: name,
Short: "Describe a service",
Long: serviceLongDesc,
Example: fmt.Sprintf(serviceExample, fullName),
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
genericclioptions.GenericRun(o, cmd, args)
},
}
return command
}