This repository has been archived by the owner on Oct 28, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
prompt.go
85 lines (80 loc) · 3.14 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package app
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/nsf/termbox-go"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func (a *App) PromptRunE(cmd *cobra.Command, args []string) error {
err := a.generateYangSchema(a.Config.GlobalFlags.Dir, a.Config.GlobalFlags.File, a.Config.GlobalFlags.Exclude)
if err != nil {
a.Logger.Printf("failed to load paths from yang: %v", err)
if !a.Config.Log {
fmt.Fprintf(os.Stderr, "ERR: failed to load paths from yang: %v\n", err)
}
}
a.PromptMode = true
// load history
a.PromptHistory = make([]string, 0, 256)
home, err := homedir.Dir()
if err != nil {
if a.Config.Debug {
a.Logger.Printf("failed to get home directory: %v", err)
}
return nil
}
content, err := os.ReadFile(filepath.Join(home, ".gnmic.history"))
if err != nil {
if a.Config.Debug {
a.Logger.Printf("failed to read history file: %v", err)
}
return nil
}
history := strings.Split(string(content), "\n")
for i := range history {
if history[i] != "" {
a.PromptHistory = append(a.PromptHistory, history[i])
}
}
return nil
}
// PreRun resolve the glob patterns and checks if --max-suggestions is bigger that the terminal height and lowers it if needed.
func (a *App) PromptPreRunE(cmd *cobra.Command, args []string) error {
a.Config.SetLocalFlagsFromFile(cmd)
err := a.yangFilesPreProcessing()
if err != nil {
return err
}
err = termbox.Init()
if err != nil {
return fmt.Errorf("could not initialize a terminal box: %v", err)
}
_, h := termbox.Size()
termbox.Close()
// set max suggestions to terminal height-1 if the supplied value is greater
if uint(a.Config.LocalFlags.PromptMaxSuggestions) > uint(h) {
if h > 1 {
a.Config.LocalFlags.PromptMaxSuggestions = uint16(h - 2)
} else {
a.Config.LocalFlags.PromptMaxSuggestions = 0
}
}
return nil
}
func (a *App) InitPromptFlags(cmd *cobra.Command) {
cmd.Flags().Uint16Var(&a.Config.LocalFlags.PromptMaxSuggestions, "max-suggestions", 10, "terminal suggestion max list size")
cmd.Flags().StringVar(&a.Config.LocalFlags.PromptPrefixColor, "prefix-color", "dark_blue", "terminal prefix color")
cmd.Flags().StringVar(&a.Config.LocalFlags.PromptSuggestionsBGColor, "suggestions-bg-color", "dark_blue", "suggestion box background color")
cmd.Flags().StringVar(&a.Config.LocalFlags.PromptDescriptionBGColor, "description-bg-color", "dark_gray", "description box background color")
cmd.Flags().BoolVar(&a.Config.LocalFlags.PromptSuggestAllFlags, "suggest-all-flags", false, "suggest local as well as inherited flags of subcommands")
cmd.Flags().BoolVar(&a.Config.LocalFlags.PromptDescriptionWithPrefix, "description-with-prefix", false, "show YANG module prefix in XPATH suggestion description")
cmd.Flags().BoolVar(&a.Config.LocalFlags.PromptDescriptionWithTypes, "description-with-types", false, "show YANG types in XPATH suggestion description")
cmd.Flags().BoolVar(&a.Config.LocalFlags.PromptSuggestWithOrigin, "suggest-with-origin", false, "suggest XPATHs with origin prepended ")
cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) {
a.Config.FileConfig.BindPFlag(fmt.Sprintf("%s-%s", cmd.Name(), flag.Name), flag)
})
}