From 9aa6392f6146b7b4b1bfbdcaef578a98e46f93d0 Mon Sep 17 00:00:00 2001 From: Mikhail Elhimov Date: Fri, 27 Dec 2024 00:26:18 +0300 Subject: [PATCH] replicaset downgrade: make version a positional argument Closes #1073 @TarantoolBot title: replicaset downgrade: make version a positional argument --- CHANGELOG.md | 2 + cli/cmd/replicaset.go | 38 +++++++++---------- .../replicaset/test_replicaset_downgrade.py | 14 +++---- 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ecd2a5a2..0d1c06801 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - `tt coredump pack`: if `-e` option is omitted first search tarantool executable in tt environment then in `PATH` instead of using the hardcoded path `/usr/bin/tarantool`. +- `tt replicaset downgrade`: make version a positional argument rather than + using the mandatory option `-v` (`--version`) ### Fixed diff --git a/cli/cmd/replicaset.go b/cli/cmd/replicaset.go index f6939bfa2..aa89a5cf6 100644 --- a/cli/cmd/replicaset.go +++ b/cli/cmd/replicaset.go @@ -3,7 +3,6 @@ package cmd import ( "errors" "fmt" - "os" "regexp" "strings" @@ -51,7 +50,6 @@ var ( chosenReplicasetAliases []string lsnTimeout int - downgradeVersion string replicasetUriHelp = " The URI can be specified in the following formats:\n" + " * [tcp://][username:password@][host:port]\n" + @@ -90,33 +88,34 @@ func newUpgradeCmd() *cobra.Command { // newDowngradeCmd creates a "replicaset downgrade" command. func newDowngradeCmd() *cobra.Command { + validateVersion := func(i int) cobra.PositionalArgs { + return func(cmd *cobra.Command, args []string) error { + var versionPattern = regexp.MustCompile(`^\d+\.\d+\.\d+$`) + if args[i] == "" { + return errors.New("need to specify the version to downgrade " + + "use --version (-v) option") + } else if !versionPattern.MatchString(args[i]) { + return errors.New("--version (-v) must be in the format " + + "'x.x.x', where x is a number") + } + return nil + } + } + cmd := &cobra.Command{ - Use: "downgrade ( | ) [flags]\n\n" + + Use: "downgrade ( | ) VERSION [flags]\n\n" + replicasetUriHelp, DisableFlagsInUseLine: true, Short: "Downgrade tarantool cluster", Long: "Downgrade tarantool cluster.\n\n" + libconnect.EnvCredentialsHelp + "\n\n", Run: func(cmd *cobra.Command, args []string) { - var versionPattern = regexp.MustCompile(`^\d+\.\d+\.\d+$`) - if downgradeVersion == "" { - err := errors.New("need to specify the version to downgrade " + - "use --version (-v) option") - util.HandleCmdErr(cmd, err) - os.Exit(1) - } else if !versionPattern.MatchString(downgradeVersion) { - err := errors.New("--version (-v) must be in the format " + - "'x.x.x', where x is a number") - util.HandleCmdErr(cmd, err) - os.Exit(1) - } - cmdCtx.CommandName = cmd.Name() err := modules.RunCmd(&cmdCtx, cmd.CommandPath(), &modulesInfo, internalReplicasetDowngradeModule, args) util.HandleCmdErr(cmd, err) }, - Args: cobra.ExactArgs(1), + Args: cobra.MatchAll(cobra.ExactArgs(2), validateVersion(1)), } cmd.Flags().StringArrayVarP(&chosenReplicasetAliases, "replicaset", "r", @@ -125,9 +124,6 @@ func newDowngradeCmd() *cobra.Command { cmd.Flags().IntVarP(&lsnTimeout, "timeout", "t", 5, "timeout for waiting the LSN synchronization (in seconds)") - cmd.Flags().StringVarP(&downgradeVersion, "version", "v", "", - "version to downgrade the schema to") - addOrchestratorFlags(cmd) addTarantoolConnectFlags(cmd) return cmd @@ -635,7 +631,7 @@ func internalReplicasetDowngradeModule(cmdCtx *cmdcontext.CmdCtx, args []string) }, replicasetcmd.DowngradeOpts{ ChosenReplicasetAliases: chosenReplicasetAliases, Timeout: lsnTimeout, - DowngradeVersion: downgradeVersion, + DowngradeVersion: args[1], }, connOpts) } diff --git a/test/integration/replicaset/test_replicaset_downgrade.py b/test/integration/replicaset/test_replicaset_downgrade.py index 12e91f9f4..60289d0ce 100644 --- a/test/integration/replicaset/test_replicaset_downgrade.py +++ b/test/integration/replicaset/test_replicaset_downgrade.py @@ -62,7 +62,7 @@ def test_downgrade_multi_master(tt_cmd, tmpdir_with_cfg): ) assert file != "" - downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-v=3.0.0"] + downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "3.0.0"] rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmpdir) assert rc == 1 @@ -97,7 +97,7 @@ def test_downgrade_t2_app_dummy_replicaset(tt_cmd): assert file != "" downgrade_cmd = [ - tt_cmd, "replicaset", "downgrade", app_name, "--custom", "-v=2.8.2" + tt_cmd, "replicaset", "downgrade", app_name, "--custom", "2.8.2" ] rc, out = run_command_and_get_output(downgrade_cmd, cwd=test_app_path) assert rc == 0 @@ -139,7 +139,7 @@ def test_cluster_replicasets(tt_cmd, tmp_path): cmd_master ) - downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=2.11.1"] + downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "2.11.1"] rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path) assert rc == 0 @@ -171,13 +171,13 @@ def test_downgrade_invalid_version(tt_cmd, tmp_path): app.build() app.start() - downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=1.1.1"] + downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "1.1.1"] rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path) assert rc == 1 assert "Version '1.1.1' is not allowed." in out - downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "-v=3.0"] + downgrade_cmd = [tt_cmd, "replicaset", "downgrade", app_name, "-t=15", "3.0"] rc, out = run_command_and_get_output(downgrade_cmd, cwd=tmp_path) assert "--version (-v) must be in the format 'x.x.x', where x is a number" in out @@ -215,7 +215,7 @@ def test_downgrade_remote_replicasets(tt_cmd, tmpdir_with_cfg): ) uri = "tcp://client:secret@127.0.0.1:3301" - upgrade_cmd = [tt_cmd, "replicaset", "downgrade", uri, "-t=15", "-v=2.11.1"] + upgrade_cmd = [tt_cmd, "replicaset", "downgrade", uri, "-t=15", "2.11.1"] rc, out = run_command_and_get_output(upgrade_cmd, cwd=tmpdir) assert rc == 0 assert "ok" in out @@ -267,7 +267,7 @@ def have_buckets_created(): assert wait_event(10, have_buckets_created) app_dir = cartridge_app.workdir - upgrade_cmd = [tt_cmd, "replicaset", "downgrade", cartridge_name, "-t=15", "-v=2.10.0"] + upgrade_cmd = [tt_cmd, "replicaset", "downgrade", cartridge_name, "-t=15", "2.10.0"] rc, out = run_command_and_get_output(upgrade_cmd, cwd=app_dir) assert rc == 0 assert "ok" in out