-
Notifications
You must be signed in to change notification settings - Fork 72
/
describe.go
80 lines (61 loc) · 1.99 KB
/
describe.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
package describe
import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/shared/connection"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
)
type options struct {
id string
outputFormat string
f *factory.Factory
}
// NewDescribeCommand creates a new command to view a connector
func NewDescribeCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "describe",
Short: f.Localizer.MustLocalize("connector.describe.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.describe.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.describe.cmd.example"),
Args: cobra.NoArgs,
Hidden: false,
RunE: func(cmd *cobra.Command, args []string) error {
validOutputFormats := flagutil.ValidOutputFormats
if opts.outputFormat != "" && !flagutil.IsValidInput(opts.outputFormat, validOutputFormats...) {
return flagutil.InvalidValueError("output", opts.outputFormat, validOutputFormats...)
}
return runDescribe(opts)
},
}
flags := connectorcmdutil.NewFlagSet(cmd, f)
_ = flags.AddConnectorID(&opts.id).Required()
flags.AddOutput(&opts.outputFormat)
return cmd
}
func runDescribe(opts *options) error {
f := opts.f
var conn connection.Connection
conn, err := f.Connection()
if err != nil {
return err
}
api := conn.API()
a := api.ConnectorsMgmt().ConnectorsApi.GetConnector(f.Context, opts.id)
response, httpRes, err := a.Execute()
if httpRes != nil {
defer httpRes.Body.Close()
}
if err != nil {
return err
}
if err = dump.Formatted(f.IOStreams.Out, opts.outputFormat, response); err != nil {
return err
}
f.Logger.Info(f.Localizer.MustLocalize("connector.describe.info.success"))
return nil
}