-
Notifications
You must be signed in to change notification settings - Fork 72
/
create.go
115 lines (95 loc) · 2.92 KB
/
create.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
package create
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/redhat-developer/app-services-cli/pkg/cmd/registry/artifact/util"
"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"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
"github.com/spf13/cobra"
)
type options struct {
file string
outputFormat string
f *factory.Factory
}
// NewCreateCommand creates a new command to create a Connector
func NewCreateCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "create",
Short: f.Localizer.MustLocalize("connector.create.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.create.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.create.cmd.example"),
Hidden: true,
Args: cobra.NoArgs,
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 runCreate(opts)
},
}
flags := flagutil.NewFlagSet(cmd, f.Localizer)
flags.StringVar(&opts.file, "file", "", f.Localizer.MustLocalize("connector.file.flag.description"))
flags.AddOutput(&opts.outputFormat)
return cmd
}
func runCreate(opts *options) error {
f := opts.f
var conn connection.Connection
conn, err := f.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
var specifiedFile *os.File
if opts.file == "" {
opts.f.Logger.Info(opts.f.Localizer.MustLocalize("common.message.reading.file"))
file, newErr := util.CreateFileFromStdin()
if newErr != nil {
return newErr
}
specifiedFile = file
} else {
if util.IsURL(opts.file) {
specifiedFile, err = util.GetContentFromFileURL(f.Context, opts.file)
} else {
specifiedFile, err = os.Open(opts.file)
}
if err != nil {
return err
}
}
defer specifiedFile.Close()
byteValue, err := ioutil.ReadAll(specifiedFile)
if err != nil {
return err
}
var connector connectormgmtclient.ConnectorRequest
err = json.Unmarshal(byteValue, &connector)
if err != nil {
return err
}
api := conn.API()
a := api.ConnectorsMgmt().ConnectorsApi.CreateConnector(f.Context)
a = a.ConnectorRequest(connector)
a = a.Async(true)
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.create.info.success"))
return nil
}