Skip to content
Snippets Groups Projects

Improve usability and better output formatting for gosndc

Merged Malte Bauch requested to merge cli-refactoring-pterm into develop
All threads resolved!
2 files
+ 42
32
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 18
29
@@ -36,7 +36,6 @@ import (
"os"
"strings"
"code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd"
"code.fbi.h-da.de/danet/gosdn/cli/completer"
"github.com/c-bata/go-prompt"
"github.com/google/uuid"
@@ -47,9 +46,10 @@ import (
// PromptCompleter provides completion for a device
type PromptCompleter struct {
yangSchemaCompleterMap map[uuid.UUID]*completer.YangSchemaCompleter
currentDeviceSuggestions []prompt.Suggest
history []string
yangSchemaCompleterMap map[uuid.UUID]*completer.YangSchemaCompleter
currentSuggestions []prompt.Suggest
document *prompt.Document
history []string
}
// NewPromptCompleter returns a new promptCompleter
@@ -132,11 +132,11 @@ func filterFlagSlice(input []string) (commandSlice []string, flagSlice []string)
func deviceGetCompletion(c *PromptCompleter, d prompt.Document, inputSplit []string) []prompt.Suggest {
switch inputLen := len(inputSplit); inputLen {
case 2:
return devicePrompt(c, d)
return c.updateSuggestionsThroughFunc(d, getDevices)
case 3:
id, err := uuid.Parse(inputSplit[inputLen-1])
if err != nil {
return devicePrompt(c, d)
return c.updateSuggestionsThroughFunc(d, getDevices)
}
if c, ok := c.yangSchemaCompleterMap[id]; ok {
return c.Complete(d)
@@ -155,7 +155,7 @@ func deviceGetCompletion(c *PromptCompleter, d prompt.Document, inputSplit []str
}
id, err := uuid.Parse(inputSplit[inputLen-2])
if err != nil {
return devicePrompt(c, d)
return c.updateSuggestionsThroughFunc(d, getDevices)
}
if yc, ok := c.yangSchemaCompleterMap[id]; ok {
return yc.Complete(d)
@@ -166,15 +166,15 @@ func deviceGetCompletion(c *PromptCompleter, d prompt.Document, inputSplit []str
return []prompt.Suggest{}
}
func devicePrompt(c *PromptCompleter, d prompt.Document) []prompt.Suggest {
if c.currentDeviceSuggestions == nil {
func (pc *PromptCompleter) updateSuggestionsThroughFunc(d prompt.Document, fn func() ([]prompt.Suggest, error)) []prompt.Suggest {
if pc.currentSuggestions == nil {
var err error
c.currentDeviceSuggestions, err = getDevices()
pc.currentSuggestions, err = fn()
if err != nil {
return prompt.FilterHasPrefix(c.currentDeviceSuggestions, d.GetWordBeforeCursor(), true)
return prompt.FilterHasPrefix(pc.currentSuggestions, d.GetWordBeforeCursor(), true)
}
}
return prompt.FilterHasPrefix(c.currentDeviceSuggestions, d.GetWordBeforeCursor(), true)
return prompt.FilterHasPrefix(pc.currentSuggestions, d.GetWordBeforeCursor(), true)
}
func cobraCommandCompletion(currCmd *cobra.Command, d prompt.Document, inputFlags []string, loaded []prompt.Suggest) []prompt.Suggest {
@@ -199,27 +199,16 @@ func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []s
switch cmd {
case pndUseCmd, pndGetCmd:
if len(inputSplit) < 3 || (len(inputSplit) == 3 && d.GetWordBeforeCursor() != "") {
pnds, err := getPnds()
if err != nil {
return []prompt.Suggest{}
}
return cobraCommandCompletion(cmd, d, inputFlags, pnds)
suggestions := c.updateSuggestionsThroughFunc(d, getPnds)
return cobraCommandCompletion(cmd, d, inputFlags, suggestions)
}
case commitCmd:
if len(inputSplit) < 3 || (len(inputSplit) == 3 && d.GetWordBeforeCursor() != "") {
ch, err := getChangesByType(pnd.ChangeState_CHANGE_STATE_PENDING)
if err != nil {
return []prompt.Suggest{}
}
return ch
return c.updateSuggestionsThroughFunc(d, getCommitedChanges)
}
case confirmCmd:
if len(inputSplit) < 3 || (len(inputSplit) == 3 && d.GetWordBeforeCursor() != "") {
ch, err := getChangesByType(pnd.ChangeState_CHANGE_STATE_COMMITTED)
if err != nil {
return []prompt.Suggest{}
}
return ch
return c.updateSuggestionsThroughFunc(d, getConfirmedChanges)
}
case deviceGetCmd, deviceSetCmd:
return deviceGetCompletion(c, d, inputSplit)
@@ -229,8 +218,8 @@ func completionBasedOnCmd(c *PromptCompleter, cmd *cobra.Command, inputSplit []s
return []prompt.Suggest{}
}
return devices
case deviceCmd:
c.currentDeviceSuggestions = nil
case deviceCmd, pndCmd, changeCmd:
c.currentSuggestions = nil
return cobraCommandCompletion(cmd, d, inputFlags, []prompt.Suggest{})
default:
return cobraCommandCompletion(cmd, d, inputFlags, []prompt.Suggest{})
Loading