-
Notifications
You must be signed in to change notification settings - Fork 72
/
start.go
93 lines (71 loc) · 2.47 KB
/
start.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
package start
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/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
"github.com/spf13/cobra"
)
type options struct {
outputFormat string
f *factory.Factory
connectorID string
}
// NewStartCommand creates a new command to start a connector
func NewStartCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "start",
Short: f.Localizer.MustLocalize("connector.start.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.start.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.start.cmd.example"),
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 runUpdateCommand(opts)
},
}
flags := connectorcmdutil.NewFlagSet(cmd, f)
flags.AddConnectorID(&opts.connectorID)
flags.AddOutput(&opts.outputFormat)
return cmd
}
func runUpdateCommand(opts *options) error {
f := opts.f
var conn connection.Connection
conn, err := f.Connection()
if err != nil {
return err
}
if opts.connectorID == "" {
connectorInstance, instance_err := contextutil.GetCurrentConnectorInstance(&conn, f)
if instance_err != nil {
return instance_err
}
opts.connectorID = *connectorInstance.Id
}
api := conn.API()
patch := make(map[string]interface{})
patch["desired_state"] = connectormgmtclient.CONNECTORDESIREDSTATE_READY
a := api.ConnectorsMgmt().ConnectorsApi.PatchConnector(f.Context, opts.connectorID).Body(patch)
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.update.info.success"))
return nil
}