-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathuse.go
More file actions
167 lines (136 loc) · 3.9 KB
/
use.go
File metadata and controls
167 lines (136 loc) · 3.9 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package use
import (
"context"
"errors"
"fmt"
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/connection"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"
"github.com/spf13/cobra"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmd/factory"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/logging"
)
type Options struct {
id string
name string
interactive bool
IO *iostreams.IOStreams
Config config.IConfig
Connection factory.ConnectionFunc
Logger func() (logging.Logger, error)
}
func NewUseCommand(f *factory.Factory) *cobra.Command {
opts := &Options{
Config: f.Config,
Connection: f.Connection,
Logger: f.Logger,
IO: f.IOStreams,
}
cmd := &cobra.Command{
Use: localizer.MustLocalizeFromID("kafka.use.cmd.use"),
Short: localizer.MustLocalizeFromID("kafka.use.cmd.shortDescription"),
Long: localizer.MustLocalizeFromID("kafka.use.cmd.longDescription"),
Example: localizer.MustLocalizeFromID("kafka.use.cmd.example"),
Args: cobra.RangeArgs(0, 1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
var searchName string
if len(args) > 0 {
searchName = args[0]
}
return cmdutil.FilterValidKafkas(f, searchName)
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
opts.name = args[0]
} else if opts.id == "" {
if !opts.IO.CanPrompt() {
return errors.New(localizer.MustLocalizeFromID("kafka.use.error.idOrNameRequired"))
}
opts.interactive = true
}
if opts.name != "" && opts.id != "" {
return errors.New(localizer.MustLocalizeFromID("kafka.common.error.idAndNameCannotBeUsed"))
}
return runUse(opts)
},
}
cmd.Flags().StringVar(&opts.id, "id", "", localizer.MustLocalizeFromID("kafka.use.flag.id"))
return cmd
}
func runUse(opts *Options) error {
if opts.interactive {
// run the use command interactively
err := runInteractivePrompt(opts)
if err != nil {
return err
}
}
logger, err := opts.Logger()
if err != nil {
return err
}
cfg, err := opts.Config.Load()
if err != nil {
return err
}
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
api := connection.API()
var res *kasclient.KafkaRequest
ctx := context.Background()
if opts.name != "" {
res, _, err = kafka.GetKafkaByName(ctx, api.Kafka(), opts.name)
if err.Error() != "" {
return err
}
} else {
res, _, err = kafka.GetKafkaByID(ctx, api.Kafka(), opts.id)
if err.Error() != "" {
return err
}
}
// build Kafka config object from the response
var kafkaConfig config.KafkaConfig = config.KafkaConfig{
ClusterID: *res.Id,
}
cfg.Services.Kafka = &kafkaConfig
if err := opts.Config.Save(cfg); err != nil {
saveErrMsg := localizer.MustLocalize(&localizer.Config{
MessageID: "kafka.use.error.saveError",
TemplateData: map[string]interface{}{
"Name": res.GetName(),
},
})
return fmt.Errorf("%v: %w", saveErrMsg, err)
}
logger.Info(localizer.MustLocalize(&localizer.Config{
MessageID: "kafka.use.log.info.useSuccess",
TemplateData: map[string]interface{}{
"Name": res.GetName(),
},
}))
return nil
}
func runInteractivePrompt(opts *Options) error {
logger, err := opts.Logger()
if err != nil {
return err
}
connection, err := opts.Connection(connection.DefaultConfigSkipMasAuth)
if err != nil {
return err
}
logger.Debug(localizer.MustLocalizeFromID("common.log.debug.startingInteractivePrompt"))
selectedKafka, err := kafka.InteractiveSelect(connection, logger)
if err != nil {
return err
}
opts.name = selectedKafka.GetName()
return nil
}