Added the project auto completion code (#959)

* Added the project auto completion code

Signed-off-by: mik-dass <mrinald7@gmail.com>

* Added getUserTypedCommands function fro getting user typed entitites

Signed-off-by: mik-dass <mrinald7@gmail.com>
This commit is contained in:
Mrinal Das
2018-11-15 15:04:16 +05:30
committed by Chris Laprun
parent 6ddd306701
commit 80e299f8ea
5 changed files with 85 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
"os"
"strings"
@@ -151,6 +152,7 @@ func printUnmountedStorage(client *occlient.Client, applicationName string) {
func addProjectFlag(cmd *cobra.Command) {
cmd.Flags().String(util.ProjectFlagName, "", "Project, defaults to active project")
completion.RegisterCommandFlagHandler(cmd, "project", completion.ProjectNameCompletionHandler)
}
func addComponentFlag(cmd *cobra.Command) {

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"os"
"strings"
@@ -200,6 +201,7 @@ func init() {
projectGetCmd.Flags().BoolVarP(&projectShortFlag, "short", "q", false, "If true, display only the project name")
projectSetCmd.Flags().BoolVarP(&projectShortFlag, "short", "q", false, "If true, display only the project name")
projectDeleteCmd.Flags().BoolVarP(&projectForceDeleteFlag, "force", "f", false, "Delete project without prompting")
projectDeleteCmd.Flags().BoolVarP(&projectShortFlag, "short", "q", false, "Delete project without prompting")
projectCmd.Flags().AddFlagSet(projectGetCmd.Flags())
projectCmd.AddCommand(projectGetCmd)
@@ -212,5 +214,8 @@ func init() {
projectCmd.Annotations = map[string]string{"command": "other"}
projectCmd.SetUsageTemplate(cmdUsageTemplate)
completion.RegisterCommandHandler(projectSetCmd, completion.ProjectNameCompletionHandler)
completion.RegisterCommandHandler(projectDeleteCmd, completion.ProjectNameCompletionHandler)
rootCmd.AddCommand(projectCmd)
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/redhat-developer/odo/pkg/occlient"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ktesting "k8s.io/client-go/testing"
@@ -20,17 +21,20 @@ func TestCompletions(t *testing.T) {
tests := []struct {
name string
handler completion.ContextualizedPredictor
cmd *cobra.Command
last string
want []string
}{
{
name: "Completing service create without input returns all available service class external names",
handler: completion.ServiceClassCompletionHandler,
cmd: serviceCreateCmd,
want: []string{"foo", "bar", "boo"},
},
{
name: "Completing service delete without input returns all available service instances",
handler: completion.ServiceCompletionHandler,
cmd: serviceDeleteCmd,
want: []string{"foo"},
},
}
@@ -67,7 +71,9 @@ func TestCompletions(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := complete.Args{Last: tt.last}
got := tt.handler(nil, a, context)
got := tt.handler(tt.cmd, a, context)
if !equal(got, tt.want) {
t.Errorf("Failed %s: got: %q, want: %q", t.Name(), got, tt.want)
}