Skip to content

Commit 9514442

Browse files
committed
remove code duplication in Command.Run functions
Closes #1072
1 parent 71552ea commit 9514442

23 files changed

+140
-436
lines changed

cli/cmd/aeon.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/spf13/cobra"
88
aeon "github.com/tarantool/tt/cli/aeon/cmd"
99
"github.com/tarantool/tt/cli/cmdcontext"
10-
"github.com/tarantool/tt/cli/modules"
1110
"github.com/tarantool/tt/cli/util"
1211
libconnect "github.com/tarantool/tt/lib/connect"
1312
)
@@ -17,7 +16,7 @@ var aeonConnectCtx = aeon.ConnectCtx{
1716
}
1817

1918
func newAeonConnectCmd() *cobra.Command {
20-
var aeonCmd = &cobra.Command{
19+
var aeonCmd = setupTtModuleCmd(internalAeonConnect, &cobra.Command{
2120
Use: "connect URI",
2221
Short: "Connect to the aeon instance",
2322
Long: "Connect to the aeon instance.\n\n" +
@@ -28,13 +27,7 @@ func newAeonConnectCmd() *cobra.Command {
2827
util.HandleCmdErr(cmd, err)
2928
return err
3029
},
31-
Run: func(cmd *cobra.Command, args []string) {
32-
cmdCtx.CommandName = cmd.Name()
33-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
34-
internalAeonConnect, args)
35-
util.HandleCmdErr(cmd, err)
36-
},
37-
}
30+
})
3831
aeonCmd.Flags().StringVar(&aeonConnectCtx.Ssl.KeyFile, "sslkeyfile", "",
3932
"path to a private SSL key file")
4033
aeonCmd.Flags().StringVar(&aeonConnectCtx.Ssl.CertFile, "sslcertfile", "",

cli/cmd/cat.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/spf13/cobra"
1313
"github.com/tarantool/tt/cli/checkpoint"
1414
"github.com/tarantool/tt/cli/cmdcontext"
15-
"github.com/tarantool/tt/cli/modules"
1615
"github.com/tarantool/tt/cli/util"
1716
"github.com/tarantool/tt/cli/version"
1817
)
@@ -31,20 +30,14 @@ var catFlags = checkpoint.Opts{
3130

3231
// NewCatCmd creates a new cat command.
3332
func NewCatCmd() *cobra.Command {
34-
var catCmd = &cobra.Command{
33+
var catCmd = setupTtModuleCmd(internalCatModule, &cobra.Command{
3534
Use: "cat <FILE>...",
3635
Short: "Print into stdout the contents of .snap/.xlog FILE(s)",
37-
Run: func(cmd *cobra.Command, args []string) {
38-
cmdCtx.CommandName = cmd.Name()
39-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
40-
internalCatModule, args)
41-
util.HandleCmdErr(cmd, err)
42-
},
4336
Example: "tt cat /path/to/file.snap /path/to/file.xlog /path/to/dir/ " +
4437
"--timestamp 2024-11-13T14:02:36.818700000+00:00\n" +
4538
" tt cat /path/to/file.snap /path/to/file.xlog /path/to/dir/ " +
4639
"--timestamp=1731592956.818",
47-
}
40+
})
4841

4942
catCmd.Flags().Uint64Var(&catFlags.To, "to", catFlags.To,
5043
"Show operations ending with the given lsn")

cli/cmd/cfg_dump.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"github.com/spf13/cobra"
77
"github.com/tarantool/tt/cli/cfg"
88
"github.com/tarantool/tt/cli/cmdcontext"
9-
"github.com/tarantool/tt/cli/modules"
10-
"github.com/tarantool/tt/cli/util"
119
)
1210

1311
var (
@@ -16,16 +14,10 @@ var (
1614

1715
// NewDumpCmd creates a new dump command.
1816
func NewDumpCmd() *cobra.Command {
19-
var dumpCmd = &cobra.Command{
17+
var dumpCmd = setupTtModuleCmd(internalDumpModule, &cobra.Command{
2018
Use: "dump",
2119
Short: "Print environment configuration",
22-
Run: func(cmd *cobra.Command, args []string) {
23-
cmdCtx.CommandName = cmd.Name()
24-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
25-
internalDumpModule, args)
26-
util.HandleCmdErr(cmd, err)
27-
},
28-
}
20+
})
2921

3022
dumpCmd.Flags().BoolVarP(&rawDump, "raw", "r", false,
3123
"Display the raw contents of tt environment config.")

cli/cmd/check.go

+2-10
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@ import (
44
"github.com/apex/log"
55
"github.com/spf13/cobra"
66
"github.com/tarantool/tt/cli/cmdcontext"
7-
"github.com/tarantool/tt/cli/modules"
87
"github.com/tarantool/tt/cli/running"
9-
"github.com/tarantool/tt/cli/util"
108
)
119

1210
// NewCheckCmd creates a new check command.
1311
func NewCheckCmd() *cobra.Command {
14-
var checkCmd = &cobra.Command{
12+
var checkCmd = setupTtModuleCmd(internalCheckModule, &cobra.Command{
1513
Use: "check [<APPLICATION_NAME>]",
1614
Short: "Check an application file for syntax errors",
17-
Run: func(cmd *cobra.Command, args []string) {
18-
cmdCtx.CommandName = cmd.Name()
19-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
20-
internalCheckModule, args)
21-
util.HandleCmdErr(cmd, err)
22-
},
23-
}
15+
})
2416

2517
return checkCmd
2618
}

cli/cmd/cluster.go

+23-78
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
clustercmd "github.com/tarantool/tt/cli/cluster/cmd"
1414
"github.com/tarantool/tt/cli/cmd/internal"
1515
"github.com/tarantool/tt/cli/cmdcontext"
16-
"github.com/tarantool/tt/cli/modules"
1716
"github.com/tarantool/tt/cli/replicaset"
1817
"github.com/tarantool/tt/cli/running"
1918
"github.com/tarantool/tt/cli/util"
@@ -141,19 +140,13 @@ func newClusterReplicasetCmd() *cobra.Command {
141140
Aliases: []string{"rs"},
142141
}
143142

144-
promoteCmd := &cobra.Command{
143+
promoteCmd := setupTtModuleCmd(internalClusterReplicasetPromoteModule, &cobra.Command{
145144
Use: "promote [-f] [flags] <URI> <INSTANCE_NAME>",
146145
DisableFlagsInUseLine: true,
147146
Short: "Promote an instance",
148147
Long: "Promote an instance\n\n" + clusterUriHelp,
149-
Run: func(cmd *cobra.Command, args []string) {
150-
cmdCtx.CommandName = cmd.Name()
151-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
152-
internalClusterReplicasetPromoteModule, args)
153-
util.HandleCmdErr(cmd, err)
154-
},
155-
Args: cobra.ExactArgs(2),
156-
}
148+
Args: cobra.ExactArgs(2),
149+
})
157150
promoteCmd.Flags().StringVarP(&promoteCtx.Username, "username", "u", "",
158151
"username (used as etcd/tarantool config storage credentials)")
159152
promoteCmd.Flags().StringVarP(&promoteCtx.Password, "password", "p", "",
@@ -162,19 +155,13 @@ func newClusterReplicasetCmd() *cobra.Command {
162155
"skip selecting a key for patching")
163156
integrity.RegisterWithIntegrityFlag(promoteCmd.Flags(), &clusterIntegrityPrivateKey)
164157

165-
demoteCmd := &cobra.Command{
158+
demoteCmd := setupTtModuleCmd(internalClusterReplicasetDemoteModule, &cobra.Command{
166159
Use: "demote [-f] [flags] <URI> <INSTANCE_NAME>",
167160
DisableFlagsInUseLine: true,
168161
Short: "Demote an instance",
169162
Long: "Demote an instance\n\n" + clusterUriHelp,
170-
Run: func(cmd *cobra.Command, args []string) {
171-
cmdCtx.CommandName = cmd.Name()
172-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
173-
internalClusterReplicasetDemoteModule, args)
174-
util.HandleCmdErr(cmd, err)
175-
},
176-
Args: cobra.ExactArgs(2),
177-
}
163+
Args: cobra.ExactArgs(2),
164+
})
178165

179166
demoteCmd.Flags().StringVarP(&demoteCtx.Username, "username", "u", "",
180167
"username (used as etcd/tarantool config storage credentials)")
@@ -184,19 +171,13 @@ func newClusterReplicasetCmd() *cobra.Command {
184171
"skip selecting a key for patching")
185172
integrity.RegisterWithIntegrityFlag(demoteCmd.Flags(), &clusterIntegrityPrivateKey)
186173

187-
expelCmd := &cobra.Command{
174+
expelCmd := setupTtModuleCmd(internalClusterReplicasetExpelModule, &cobra.Command{
188175
Use: "expel [-f] [flags] <URI> <INSTANCE_NAME>",
189176
DisableFlagsInUseLine: true,
190177
Short: "Expel an instance",
191178
Long: "Expel an instance\n\n" + clusterUriHelp,
192-
Run: func(cmd *cobra.Command, args []string) {
193-
cmdCtx.CommandName = cmd.Name()
194-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
195-
internalClusterReplicasetExpelModule, args)
196-
util.HandleCmdErr(cmd, err)
197-
},
198-
Args: cobra.ExactArgs(2),
199-
}
179+
Args: cobra.ExactArgs(2),
180+
})
200181

201182
expelCmd.Flags().StringVarP(&expelCtx.Username, "username", "u", "",
202183
"username (used as etcd/tarantool config storage credentials)")
@@ -211,20 +192,14 @@ func newClusterReplicasetCmd() *cobra.Command {
211192
Short: "Add or remove roles in cluster replicaset",
212193
}
213194

214-
addRolesCmd := &cobra.Command{
195+
addRolesCmd := setupTtModuleCmd(internalClusterReplicasetRolesAddModule, &cobra.Command{
215196
Use: "add <URI> <ROLE_NAME> [flags]",
216197
Short: "Add role to an instance, group or instance",
217198
Long: "Add role to an instance, group or instance\n\n" + clusterUriHelp,
218-
Run: func(cmd *cobra.Command, args []string) {
219-
cmdCtx.CommandName = cmd.Name()
220-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
221-
internalClusterReplicasetRolesAddModule, args)
222-
util.HandleCmdErr(cmd, err)
223-
},
224199
Example: "tt cluster replicaset roles add http://user:pass@localhost:3301" +
225200
" roles.metrics-export --instance_name master",
226201
Args: cobra.ExactArgs(2),
227-
}
202+
})
228203

229204
addRolesCmd.Flags().StringVarP(&rolesChangeCtx.ReplicasetName, "replicaset", "r", "",
230205
"name of a target replicaset")
@@ -243,20 +218,14 @@ func newClusterReplicasetCmd() *cobra.Command {
243218
"skip selecting a key for patching")
244219
integrity.RegisterWithIntegrityFlag(addRolesCmd.Flags(), &clusterIntegrityPrivateKey)
245220

246-
removeRolesCmd := &cobra.Command{
221+
removeRolesCmd := setupTtModuleCmd(internalClusterReplicasetRolesRemoveModule, &cobra.Command{
247222
Use: "remove <URI> <ROLE_NAME> [flags]",
248223
Short: "Remove role from instance, group, instance or globally",
249224
Long: "Remove role from instance, group, instance or globally\n\n" + clusterUriHelp,
250-
Run: func(cmd *cobra.Command, args []string) {
251-
cmdCtx.CommandName = cmd.Name()
252-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
253-
internalClusterReplicasetRolesRemoveModule, args)
254-
util.HandleCmdErr(cmd, err)
255-
},
256225
Example: "tt cluster replicaset roles remove http://user:pass@localhost:3301" +
257226
" roles.metrics-export --instance_name master",
258227
Args: cobra.ExactArgs(2),
259-
}
228+
})
260229

261230
removeRolesCmd.Flags().StringVarP(&rolesChangeCtx.ReplicasetName, "replicaset", "r", "",
262231
"name of a target replicaset")
@@ -293,20 +262,14 @@ func newClusterFailoverCmd() *cobra.Command {
293262
Aliases: []string{"fo"},
294263
}
295264

296-
switchCmd := &cobra.Command{
265+
switchCmd := setupTtModuleCmd(internalClusterFailoverSwitchModule, &cobra.Command{
297266
Use: "switch <URI> <INSTANCE_NAME> [flags]",
298267
DisableFlagsInUseLine: true,
299268
Short: "Switch master instance",
300269
Long: "Switch master instance\n\n" + failoverUriHelp,
301270
Example: "tt cluster failover switch http://localhost:2379/app instance_name",
302-
Run: func(cmd *cobra.Command, args []string) {
303-
cmdCtx.CommandName = cmd.Name()
304-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
305-
internalClusterFailoverSwitchModule, args)
306-
util.HandleCmdErr(cmd, err)
307-
},
308-
Args: cobra.ExactArgs(2),
309-
}
271+
Args: cobra.ExactArgs(2),
272+
})
310273

311274
switchCmd.Flags().StringVarP(&switchCtx.Username, "username", "u", "",
312275
"username (used as etcd credentials)")
@@ -317,19 +280,13 @@ func newClusterFailoverCmd() *cobra.Command {
317280
switchCmd.Flags().BoolVarP(&switchCtx.Wait, "wait", "w", false,
318281
"wait for the command to complete execution")
319282

320-
switchStatusCmd := &cobra.Command{
283+
switchStatusCmd := setupTtModuleCmd(internalClusterFailoverSwitchStatusModule, &cobra.Command{
321284
Use: "switch-status <URI> <TASK_ID>",
322285
DisableFlagsInUseLine: true,
323286
Short: "Show master switching status",
324287
Long: "Show master switching status\n\n" + failoverUriHelp,
325-
Run: func(cmd *cobra.Command, args []string) {
326-
cmdCtx.CommandName = cmd.Name()
327-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
328-
internalClusterFailoverSwitchStatusModule, args)
329-
util.HandleCmdErr(cmd, err)
330-
},
331-
Args: cobra.ExactArgs(2),
332-
}
288+
Args: cobra.ExactArgs(2),
289+
})
333290

334291
cmd.AddCommand(switchCmd)
335292
cmd.AddCommand(switchStatusCmd)
@@ -343,7 +300,7 @@ func NewClusterCmd() *cobra.Command {
343300
Short: "Manage cluster configuration",
344301
}
345302

346-
show := &cobra.Command{
303+
show := setupTtModuleCmd(internalClusterShowModule, &cobra.Command{
347304
Use: "show (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>)",
348305
Short: "Show a cluster configuration",
349306
Long: "Show a cluster configuration for an application, instance," +
@@ -352,12 +309,6 @@ func NewClusterCmd() *cobra.Command {
352309
" tt cluster show application_name:instance_name\n" +
353310
" tt cluster show https://user:pass@localhost:2379/tt\n" +
354311
" tt cluster show https://user:pass@localhost:2379/tt?name=instance",
355-
Run: func(cmd *cobra.Command, args []string) {
356-
cmdCtx.CommandName = cmd.Name()
357-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
358-
internalClusterShowModule, args)
359-
util.HandleCmdErr(cmd, err)
360-
},
361312
Args: cobra.ExactArgs(1),
362313
ValidArgsFunction: func(
363314
cmd *cobra.Command,
@@ -371,7 +322,7 @@ func NewClusterCmd() *cobra.Command {
371322
running.ExtractActiveAppNames,
372323
running.ExtractActiveInstanceNames)
373324
},
374-
}
325+
})
375326
show.Flags().StringVarP(&showCtx.Username, "username", "u", "",
376327
"username (used as etcd credentials only)")
377328
show.Flags().StringVarP(&showCtx.Password, "password", "p", "",
@@ -380,7 +331,7 @@ func NewClusterCmd() *cobra.Command {
380331
"validate the configuration")
381332
clusterCmd.AddCommand(show)
382333

383-
publish := &cobra.Command{
334+
publish := setupTtModuleCmd(internalClusterPublishModule, &cobra.Command{
384335
Use: "publish (<APP_NAME> | <APP_NAME:INSTANCE_NAME> | <URI>) file",
385336
Short: "Publish a cluster configuration",
386337
Long: "Publish an application or an instance configuration to a cluster " +
@@ -399,12 +350,6 @@ func NewClusterCmd() *cobra.Command {
399350
" tt cluster publish --group group --replicaset replicaset " +
400351
"https://user:pass@localhost:2379/tt?name=instance " +
401352
"instance.yaml",
402-
Run: func(cmd *cobra.Command, args []string) {
403-
cmdCtx.CommandName = cmd.Name()
404-
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo,
405-
internalClusterPublishModule, args)
406-
util.HandleCmdErr(cmd, err)
407-
},
408353
Args: cobra.ExactArgs(2),
409354
ValidArgsFunction: func(
410355
cmd *cobra.Command,
@@ -415,7 +360,7 @@ func NewClusterCmd() *cobra.Command {
415360
running.ExtractActiveAppNames,
416361
running.ExtractActiveInstanceNames)
417362
},
418-
}
363+
})
419364
publish.Flags().StringVarP(&publishCtx.Username, "username", "u", "",
420365
"username (used as etcd credentials only)")
421366
publish.Flags().StringVarP(&publishCtx.Password, "password", "p", "",

cli/cmd/common.go

+14
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import (
55
"fmt"
66
"io"
77

8+
"github.com/spf13/cobra"
89
"github.com/tarantool/tt/cli/cmdcontext"
910
"github.com/tarantool/tt/cli/configure"
11+
"github.com/tarantool/tt/cli/modules"
12+
"github.com/tarantool/tt/cli/util"
1013
libcluster "github.com/tarantool/tt/lib/cluster"
1114
"github.com/tarantool/tt/lib/integrity"
1215
)
@@ -67,3 +70,14 @@ func createDataCollectorsAndDataPublishers(ctx integrity.IntegrityCtx,
6770
}
6871
return collectors, publishers, err
6972
}
73+
74+
func setupTtModuleCmd(internalModule modules.InternalFunc, cmd *cobra.Command) *cobra.Command {
75+
cmd.Run = func(cmd *cobra.Command, args []string) {
76+
cmdCtx.CommandName = cmd.Name()
77+
err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalModule, args)
78+
if err != nil {
79+
util.HandleCmdErr(cmd, err)
80+
}
81+
}
82+
return cmd
83+
}

0 commit comments

Comments
 (0)