-
Notifications
You must be signed in to change notification settings - Fork 72
/
create.go
93 lines (68 loc) · 2.4 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
package create
import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/connector/connectorcmdutil"
"github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/flagutil"
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/dump"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
"github.com/redhat-developer/app-services-cli/pkg/shared/contextutil"
"github.com/redhat-developer/app-services-cli/pkg/shared/factory"
"github.com/spf13/cobra"
connectormgmtclient "github.com/redhat-developer/app-services-sdk-go/connectormgmt/apiv1/client"
)
type options struct {
f *factory.Factory
name string
outputFormat string
}
// NewCreateCommand a new command to create a new namespace
func NewCreateCommand(f *factory.Factory) *cobra.Command {
opts := &options{
f: f,
}
cmd := &cobra.Command{
Use: "create",
Short: f.Localizer.MustLocalize("connector.namespace.create.cmd.shortDescription"),
Long: f.Localizer.MustLocalize("connector.namespace.create.cmd.longDescription"),
Example: f.Localizer.MustLocalize("connector.namespace.create.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
validator := connectorcmdutil.Validator{
Localizer: f.Localizer,
}
if err := validator.ValidateNamespace(opts.name); err != nil {
return err
}
return runCreate(opts)
},
}
flags := flagutil.NewFlagSet(cmd, f.Localizer)
flags.StringVar(&opts.name, "name", "", f.Localizer.MustLocalize("connector.namespace.create.flag.name.description"))
flags.AddOutput(&opts.outputFormat)
return cmd
}
func runCreate(opts *options) error {
f := opts.f
conn, err := f.Connection()
if err != nil {
return err
}
api := conn.API()
a := api.ConnectorsMgmt().ConnectorNamespacesApi.CreateEvaluationNamespace(f.Context)
connectorNameSpaceEvalReq := connectormgmtclient.ConnectorNamespaceEvalRequest{
Name: &opts.name,
}
var newErr error
a = a.ConnectorNamespaceEvalRequest(connectorNameSpaceEvalReq)
namespace, _, newErr := a.Execute()
if newErr != nil {
return newErr
}
if newErr = dump.Formatted(f.IOStreams.Out, opts.outputFormat, namespace); newErr != nil {
return newErr
}
if err = contextutil.SetCurrentNamespaceInstance(&namespace, &conn, f); err != nil {
return err
}
f.Logger.Info(f.Localizer.MustLocalize("connector.namespace.create.info.success"), localize.NewEntry("Name", opts.name))
return nil
}