-
Notifications
You must be signed in to change notification settings - Fork 13
/
util.go
54 lines (49 loc) · 1001 Bytes
/
util.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
package util
import (
"encoding/json"
"errors"
"github.com/chzyer/readline"
"io/ioutil"
"strings"
)
const (
JSON_MARSHAL_PREFIX = ""
JSON_MARSHAL_INDENT = " "
)
func LoadJsonDataFromFile(filePath string, object interface{}) error {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
if err = json.Unmarshal(data, object); len(data) > 0 && err != nil {
return err
}
return nil
}
func Scan(prompt string, defaultInp string, historyFile string) (string, error) {
// create config
config := &readline.Config{
Prompt: prompt,
HistoryFile: historyFile,
HistorySearchFold: true,
InterruptPrompt: "^C",
EOFPrompt: "exit",
}
rl, err := readline.NewEx(config)
if err != nil {
return "", err
}
defer rl.Close()
for {
line, err := rl.ReadlineWithDefault(defaultInp)
if err != nil {
break
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
return line, nil
}
return "", errors.New("cancelled")
}