Fix `vtctldclient`'s Root command to return an error on unknown command (#12481) (#12531)

* Add a Run func to `vtctldclient`'s Root command to return an error on unknown command

Closes #12480.



* Add test



* flags test data



---------

Signed-off-by: Andrew Mason <andrew@planetscale.com>
This commit is contained in:
Andrew Mason 2023-03-01 15:14:01 -05:00 коммит произвёл GitHub
Родитель 5701a05455
Коммит 7ac5c3939b
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 76 добавлений и 0 удалений

Просмотреть файл

@ -19,6 +19,7 @@ package command
import (
"context"
"errors"
"fmt"
"io"
"time"
@ -82,6 +83,26 @@ var (
// propagated).
SilenceErrors: true,
Version: servenv.AppVersion.String(),
// If we've reached this function, it means that:
//
// (1) The user specified some positional arguments, which, for the way
// we've structured things can only be a subcommand name, **and**
//
// (2) Cobra was unable to find a subcommand with that name for which to
// call a Run or RunE function.
//
// From this we conclude that the user was trying to either run a
// command that doesn't exist (e.g. "vtctldclient delete-my-data") or
// has misspelled a legitimate command (e.g. "vtctldclient StapReplication").
// If we think this has happened, return an error, which will get
// displayed to the user in main.go along with the usage.
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flags().NArg() > 0 {
return fmt.Errorf("unknown command: %s", cmd.Flags().Arg(0))
}
return nil
},
}
)

Просмотреть файл

@ -0,0 +1,54 @@
/*
Copyright 2023 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package command_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/cmd/vtctldclient/command"
"vitess.io/vitess/go/vt/vtctl/localvtctldclient"
vtctlservicepb "vitess.io/vitess/go/vt/proto/vtctlservice"
)
type emptyLocalServer struct {
vtctlservicepb.UnimplementedVtctldServer
}
func TestRoot(t *testing.T) {
t.Run("error on unknown subcommand", func(t *testing.T) {
args := append([]string{}, os.Args...)
protocol := command.VtctldClientProtocol
localvtctldclient.SetServer(&emptyLocalServer{})
t.Cleanup(func() {
os.Args = append([]string{}, args...)
command.VtctldClientProtocol = protocol
})
os.Args = []string{"vtctldclient", "this-is-bunk"}
command.VtctldClientProtocol = "local"
err := command.Root.Execute()
require.Error(t, err, "root command should error on unknown command")
assert.Contains(t, err.Error(), "unknown command")
})
}

Просмотреть файл

@ -1,6 +1,7 @@
Executes a cluster management command on the remote vtctld server.
Usage:
vtctldclient [flags]
vtctldclient [command]
Available Commands: