-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
126 lines (97 loc) · 2.31 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"github.com/sebastianwebber/hba/cmd/ui"
prompt "github.com/c-bata/go-prompt"
"github.com/sebastianwebber/hba"
)
var (
version = "devel"
hbaFile string
sugestionList = []prompt.Suggest{
{Text: "show", Description: "to display all rules from pg_hba.conf file"},
{Text: "quit", Description: "to quit"},
{Text: "help", Description: "for help with hba commands"},
}
histCommands []string
fileRules *[]hba.Rule
)
func init() {
flag.StringVar(&hbaFile, "hba-file", "", "full path of the pg_hba.conf file")
flag.Parse()
if hbaFile == "" {
fmt.Println("Missing pg_hba.conf config file")
os.Exit(1)
}
}
func completer(d prompt.Document) []prompt.Suggest {
return prompt.FilterHasPrefix(sugestionList, d.GetWordBeforeCursor(), true)
}
func main() {
fmt.Printf("hba version %s\n", version)
loadFile()
fmt.Println(`Type "help" for help.`)
p := prompt.New(
executer,
completer,
prompt.OptionTitle("kube-prompt: interactive kubernetes client"),
prompt.OptionPrefix(fmt.Sprintf("%s#= ", hbaFile)),
prompt.OptionPrefixTextColor(prompt.Yellow),
prompt.OptionHistory(histCommands),
prompt.OptionSelectedSuggestionBGColor(prompt.LightGray),
prompt.OptionSuggestionBGColor(prompt.DarkGray),
)
p.Run()
}
func executer(cmd string) {
defer func() {
histCommands = append(histCommands, cmd)
}()
if strings.HasPrefix(cmd, "show") {
parts := strings.Fields(cmd)
if len(parts) == 1 {
showRules(*fileRules)
return
}
showRules(
hba.Filter(
*fileRules,
parts[1]))
return
}
if cmd == "help" {
showHelp()
return
}
if cmd == "quit" || cmd == "q" {
os.Exit(0)
}
return
}
func loadFile() {
var err error
file, err := os.Open(hbaFile)
defer file.Close()
if err != nil {
log.Fatalf("could not open file: %v", file)
}
fileRules, err = hba.ParseReader(file)
if err != nil {
log.Fatalf("could not parse file: %v", file)
}
fmt.Printf("Loaded rules from '%s' file.\n", hbaFile)
}
func showRules(rules []hba.Rule) {
ui.DisplayRules(rules, os.Stdout)
}
func showHelp() {
fmt.Println("You are using hba, an experimental command-line interface to pg_hba.conf file.")
fmt.Println("type:")
for i := 0; i < len(sugestionList); i++ {
fmt.Printf("\t%s %s\n", sugestionList[i].Text, sugestionList[i].Description)
}
}