Added command auto completion for odo storage (#986)

Fixes #941
This commit is contained in:
Suraj Narwade
2018-11-21 19:58:48 +05:30
committed by anmolbabu
parent ed57c1b0cb
commit 83cebd1a01
2 changed files with 66 additions and 2 deletions

View File

@@ -2,11 +2,13 @@ package cmd
import (
"fmt"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
"os"
"strings"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
odoutil "github.com/redhat-developer/odo/pkg/odo/util"
"github.com/redhat-developer/odo/pkg/odo/util/completion"
"github.com/redhat-developer/odo/pkg/storage"
"github.com/redhat-developer/odo/pkg/util"
"github.com/spf13/cobra"
@@ -279,4 +281,8 @@ func init() {
storageCmd.SetUsageTemplate(cmdUsageTemplate)
rootCmd.AddCommand(storageCmd)
completion.RegisterCommandHandler(storageDeleteCmd, completion.StorageDeleteCompletionHandler)
completion.RegisterCommandHandler(storageMountCmd, completion.StorageMountCompletionHandler)
completion.RegisterCommandHandler(storageUnmountCmd, completion.StorageUnMountCompletionHandler)
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/project"
"github.com/redhat-developer/odo/pkg/service"
"github.com/redhat-developer/odo/pkg/storage"
"github.com/redhat-developer/odo/pkg/url"
"github.com/spf13/cobra"
)
@@ -100,3 +101,60 @@ var URLCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *ge
}
return
}
// StorageDeleteCompletionHandler provides storage name completion for storage delete
var StorageDeleteCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
storages, err := storage.List(context.Client, context.Component(), context.Application)
if err != nil {
return completions
}
for _, storage := range storages {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if val, ok := args.commands[storage.Name]; ok && val {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}
// StorageMountCompletionHandler provides storage name completion for storage mount
var StorageMountCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
storages, err := storage.ListUnmounted(context.Client, context.Application)
if err != nil {
return completions
}
for _, storage := range storages {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if val, ok := args.commands[storage.Name]; ok && val {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}
// StorageUnMountCompletionHandler provides storage name completion for storage unmount
var StorageUnMountCompletionHandler = func(cmd *cobra.Command, args parsedArgs, context *genericclioptions.Context) (completions []string) {
completions = make([]string, 0)
storages, err := storage.ListMounted(context.Client, context.Component(), context.Application)
if err != nil {
return completions
}
for _, storage := range storages {
// we found the storage name in the list which means
// that the storage name has been already selected by the user so no need to suggest more
if val, ok := args.commands[storage.Name]; ok && val {
return nil
}
completions = append(completions, storage.Name)
}
return completions
}