Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pdctl interactive mode autocomplete #2785

Merged
merged 20 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions tools/pd-ctl/pdctl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/chzyer/readline"
"github.com/mattn/go-shellwords"
Expand All @@ -37,9 +38,10 @@ type CommandFlags struct {
var (
commandFlags = CommandFlags{}

detach bool
interact bool
version bool
detach bool
interact bool
version bool
readlinecompleter *readline.PrefixCompleter
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
Expand Down Expand Up @@ -117,6 +119,7 @@ func getMainCmd(args []string) *cobra.Command {
rootCmd.ParseFlags(args)
rootCmd.SetOutput(os.Stdout)

readlinecompleter = readline.NewPrefixCompleter(genCompleter(rootCmd)...)
return rootCmd
}

Expand Down Expand Up @@ -156,6 +159,7 @@ func loop() {
l, err := readline.NewEx(&readline.Config{
Prompt: "\033[31m»\033[0m ",
HistoryFile: "/tmp/readline.tmp",
AutoComplete: readlinecompleter,
InterruptPrompt: "^C",
EOFPrompt: "^D",
HistorySearchFold: true,
Expand Down Expand Up @@ -190,3 +194,23 @@ func loop() {
Start(args)
}
}

func genCompleter(cmd *cobra.Command) []readline.PrefixCompleterInterface {
pc := []readline.PrefixCompleterInterface{}
if len(cmd.Commands()) != 0 {
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range cmd.Commands() {
if v.HasFlags() {
flagspc := []readline.PrefixCompleterInterface{}
flaguseages := strings.Split(strings.Trim(v.Flags().FlagUsages(), " "), "\n")
for i := 0; i < len(flaguseages)-1; i++ {
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
flagspc = append(flagspc, readline.PcItem(strings.Split(strings.Trim(flaguseages[i], " "), " ")[0]))
}
flagspc = append(flagspc, genCompleter(v)...)
pc = append(pc, readline.PcItem(strings.Split(v.Use, " ")[0], flagspc...))
} else {
pc = append(pc, readline.PcItem(strings.Split(v.Use, " ")[0], genCompleter(v)...))
}
}
}
return pc
}
87 changes: 87 additions & 0 deletions tools/pd-ctl/pdctl/ctl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2020 TiKV Project 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package pdctl
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

"github.com/spf13/cobra"
)

func newACommand() *cobra.Command {
a := &cobra.Command{
Use: "testa",
Short: "test A command",
}
return a
}

func newBCommand() *cobra.Command {
a := &cobra.Command{
Use: "testb",
Short: "test b command",
}
return a
}
func newDEFCommand() *cobra.Command {
a := &cobra.Command{
Use: "testdef",
Short: "test def command",
}
return a
}

func newCCommand() *cobra.Command {
a := &cobra.Command{
Use: "testc",
Short: "test c command",
}
return a
}
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved

func Test_genCompleter(t *testing.T) {
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
var subcommand = []string{"testa", "testb", "testc", "testdef"}

rootCmd := &cobra.Command{
Use: "roottest",
Short: "test root cmd",
}
rootCmd.AddCommand(newACommand(), newBCommand(), newCCommand(), newDEFCommand())

pc := genCompleter(rootCmd)

for _, cmd := range subcommand {
runarray := []rune(cmd)
inprefixarray := true
jiashiwen marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range pc {
inprefixarray = true
if len(runarray) != len(v.GetName())-1 {
continue
}
for i := 0; i < len(runarray); i++ {
if runarray[i] != v.GetName()[i] {
inprefixarray = false
}
}
if inprefixarray == true {
break
}
}

if inprefixarray == false {
t.Errorf("%s not in prefix array", cmd)
}
}

}