Skip to content

Commit

Permalink
Autocomplete property names and options
Browse files Browse the repository at this point in the history
  • Loading branch information
ldmberman committed Aug 18, 2015
1 parent 3330dc6 commit ca2d812
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions autocomplete/autocomplete.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package autocomplete

import (
"github.com/centurylinkcloud/clc-go-cli/base"
"github.com/centurylinkcloud/clc-go-cli/command_loader"
"github.com/centurylinkcloud/clc-go-cli/options"
"github.com/centurylinkcloud/clc-go-cli/parser"
"reflect"
"strings"
)

Expand All @@ -19,13 +23,58 @@ func Run(args []string) string {
if len(args) == 1 {
return strings.Join(command_loader.GetCommands(resource), " ")
}

cmdArg := args[1]
_, err = command_loader.LoadCommand(resource, cmdArg)
cmd, err := command_loader.LoadCommand(resource, cmdArg)
if err != nil {
if len(args) == 2 {
return strings.Join(command_loader.GetCommands(resource), " ")
}
return ""
}
return ""

var arguments []string
if cmd.Command() == "" {
arguments = args[1:]
} else {
arguments = args[2:]
}
if len(arguments) == 0 {
return strings.Join(optionsAndArguments(cmd), " ")
}
parsed, err := parser.ParseArguments(arguments)
if err != nil {
return ""
}
yes, _, err := options.AreToBeTakenFromFile(parsed)
if yes || err != nil {
return ""
}
_, err = options.ExtractFrom(parsed)
if err != nil {
// Do not show anything if option needs a value.
return ""
}
last := args[len(args)-1]
if strings.HasPrefix(last, "--") {
if hasArg(cmd.InputModel(), parser.NormalizePropertyName(last)) {
// TODO autocomplete property values
return ""
}
}
return strings.Join(optionsAndArguments(cmd), " ")
}

func optionsAndArguments(command base.Command) []string {
opts := options.Get()
args := command.Arguments()
return append(opts, args...)
}

func hasArg(m interface{}, f string) bool {
meta := reflect.ValueOf(m).Elem()
if meta.FieldByName(f).IsValid() {
return true
}
return false
}

0 comments on commit ca2d812

Please sign in to comment.