/
terminal.go
191 lines (162 loc) · 4.08 KB
/
terminal.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package console
/*
terminal console
type "exit" to exit console
*/
import (
"fmt"
"regexp"
"strings"
"github.com/peterh/liner"
"github.com/stratosnet/sds/framework/utils/cmd"
)
var (
cmdNames [100]string
cmdPos int
)
// Mystdin default terminal process
var Mystdin = NewTerminal()
// ProcessFunc
// param parameters
//
//line command
type ProcessFunc func(line string, param []string) bool
type ProcessCmd struct {
pFunc ProcessFunc
allowExec bool
}
type Terminal struct {
*liner.State
mapFunc map[string]ProcessCmd
supported bool
normalMode liner.ModeApplier
rawMode liner.ModeApplier
Isrun bool
}
func (c *Terminal) RegisterProcessFunc(key string, f ProcessFunc, allowExec bool) {
strKey := strings.ToLower(key)
c.mapFunc[strKey] = ProcessCmd{pFunc: f, allowExec: allowExec}
cmdNames[cmdPos] = strKey
cmdPos++
}
func NewTerminal() *Terminal {
p := new(Terminal)
normalMode, _ := liner.TerminalMode()
p.State = liner.NewLiner()
p.mapFunc = make(map[string]ProcessCmd)
rawMode, err := liner.TerminalMode()
if err != nil || !liner.TerminalSupported() {
p.supported = false
} else {
p.supported = true
p.normalMode = normalMode
p.rawMode = rawMode
_ = normalMode.ApplyMode()
}
p.SetCtrlCAborts(true)
p.SetTabCompletionStyle(liner.TabPrints)
p.SetMultiLineMode(true)
return p
}
func (c *Terminal) Run() {
c.Isrun = true
defer c.Close()
c.SetCompleter(func(line string) (c []string) {
for _, n := range cmdNames {
if strings.HasPrefix(n, strings.ToLower(line)) {
c = append(c, n)
}
}
return
})
if c.supported {
_ = c.rawMode.ApplyMode()
defer func() {
_ = c.normalMode.ApplyMode()
}()
} else {
defer fmt.Println()
}
for c.Isrun {
if name, err := c.Prompt(">"); err == nil {
//log.Print("Got: " + name)
name = strings.TrimSpace(name) // Trim leading and trailing whitespaces
re := regexp.MustCompile(`\s+`) // Matches one or more consecutive whitespace characters
trimmedCmd := re.ReplaceAllString(name, " ") // Replace with a single space
//log.Print("Trimmed: " + trimmedCmd)
cmdstring := strings.Split(trimmedCmd, " ")
// if len(cmdstring) == 2 {
// param = strings.Split(cmdstring[1], " ")
// utils.DebugLog("param", param)
// }
param := cmdstring[1:]
// utils.DebugLog("cmdstring", cmdstring)
strkey := strings.ToLower(cmdstring[0])
// utils.DebugLog("cmdstring", strkey, c.mapFunc)
if exit := c.RunCmd(strkey, param, false); exit {
return
}
c.AppendHistory(name)
} else if err == liner.ErrPromptAborted {
fmt.Println("Exit")
return
} else {
fmt.Println("Error reading line: ", err)
}
}
}
func (c *Terminal) RunCmd(strkey string, param []string, isExec bool) bool {
if pCmd, ok := c.mapFunc[strkey]; ok {
if isExec && !pCmd.allowExec {
fmt.Println("The command is not supported with 'exec'. Please run it in interaction mode")
} else {
pCmd.pFunc(strkey, param[:])
}
} else {
if strkey == "exit" {
return true
}
fmt.Println("The command is not found: ", strkey)
}
return false
}
func (p *Terminal) PromptPassword(prompt string) (passwd string, err error) {
if p.supported {
_ = p.rawMode.ApplyMode()
defer func() {
_ = p.normalMode.ApplyMode()
}()
return p.State.PasswordPrompt(prompt)
}
// Just as in Prompt, handle printing the prompt here instead of relying on liner.
fmt.Print(prompt)
passwd, err = p.State.Prompt("")
fmt.Println()
return passwd, err
}
func MyGetPassword(prompt string, confirmation bool) string {
// Otherwise prompt the user for the password
if prompt != "" {
fmt.Println(prompt)
}
password, err := Mystdin.PromptPassword("password: ")
if err != nil {
cmd.Fatalf("Failed to read passphrase: %v", err)
}
if confirmation {
confirm, err := Mystdin.PromptPassword("Repeat password: ")
if err != nil {
cmd.Fatalf("Failed to read passphrase confirmation: %v", err)
}
if password != confirm {
password = ""
cmd.Fatalf("password do not match")
}
}
if Mystdin.supported {
_ = Mystdin.rawMode.ApplyMode()
} else {
defer fmt.Println()
}
return password
}