-
Notifications
You must be signed in to change notification settings - Fork 72
/
connect.go
112 lines (97 loc) · 3.75 KB
/
connect.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
package connect
import (
"context"
"github.com/redhat-developer/app-services-cli/internal/build"
"github.com/redhat-developer/app-services-cli/internal/config"
"github.com/redhat-developer/app-services-cli/pkg/cluster"
"github.com/redhat-developer/app-services-cli/pkg/cmd/factory"
"github.com/redhat-developer/app-services-cli/pkg/connection"
"github.com/redhat-developer/app-services-cli/pkg/iostreams"
"github.com/redhat-developer/app-services-cli/pkg/kafka"
"github.com/redhat-developer/app-services-cli/pkg/localize"
"github.com/redhat-developer/app-services-cli/pkg/logging"
"github.com/spf13/cobra"
)
type options struct {
Config config.IConfig
Connection func(connectionCfg *connection.Config) (connection.Connection, error)
Logger logging.Logger
IO *iostreams.IOStreams
localizer localize.Localizer
Context context.Context
kubeconfigLocation string
namespace string
offlineAccessToken string
forceCreationWithoutAsk bool
ignoreContext bool
selectedKafka string
}
func NewConnectCommand(f *factory.Factory) *cobra.Command {
opts := &options{
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
localizer: f.Localizer,
Context: f.Context,
}
cmd := &cobra.Command{
Use: "connect",
Short: opts.localizer.MustLocalize("cluster.connect.cmd.shortDescription"),
Long: opts.localizer.MustLocalize("cluster.connect.cmd.longDescription"),
Example: opts.localizer.MustLocalize("cluster.connect.cmd.example"),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, _ []string) error {
if opts.ignoreContext == true && !opts.IO.CanPrompt() {
return opts.localizer.MustLocalizeError("flag.error.requiredWhenNonInteractive", localize.NewEntry("Flag", "ignore-context"))
}
return runConnect(opts)
},
}
cmd.Flags().StringVar(&opts.kubeconfigLocation, "kubeconfig", "", opts.localizer.MustLocalize("cluster.common.flag.kubeconfig.description"))
cmd.Flags().StringVar(&opts.offlineAccessToken, "token", "", opts.localizer.MustLocalize("cluster.common.flag.offline.token.description", localize.NewEntry("OfflineTokenURL", build.OfflineTokenURL)))
cmd.Flags().StringVarP(&opts.namespace, "namespace", "n", "", opts.localizer.MustLocalize("cluster.common.flag.namespace.description"))
cmd.Flags().BoolVarP(&opts.forceCreationWithoutAsk, "yes", "y", false, opts.localizer.MustLocalize("cluster.common.flag.yes.description"))
cmd.Flags().BoolVar(&opts.ignoreContext, "ignore-context", false, opts.localizer.MustLocalize("cluster.common.flag.ignoreContext.description"))
return cmd
}
func runConnect(opts *options) error {
conn, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
clusterConn, err := cluster.NewKubernetesClusterConnection(conn, opts.Config, opts.Logger, opts.kubeconfigLocation, opts.IO, opts.localizer)
if err != nil {
return err
}
cfg, err := opts.Config.Load()
if err != nil {
return err
}
// In future config will include Id's of other services
if cfg.Services.Kafka == nil || opts.ignoreContext {
// nolint
selectedKafka, err := kafka.InteractiveSelect(opts.Context, conn, opts.Logger, opts.localizer)
if err != nil {
return err
}
if selectedKafka == nil {
return nil
}
opts.selectedKafka = selectedKafka.GetId()
} else {
opts.selectedKafka = cfg.Services.Kafka.ClusterID
}
arguments := &cluster.ConnectArguments{
OfflineAccessToken: opts.offlineAccessToken,
ForceCreationWithoutAsk: opts.forceCreationWithoutAsk,
IgnoreContext: opts.ignoreContext,
SelectedKafka: opts.selectedKafka,
Namespace: opts.namespace,
}
err = clusterConn.Connect(opts.Context, arguments)
if err != nil {
return err
}
return nil
}