-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
159 lines (130 loc) · 2.8 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
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
package main
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/olekukonko/tablewriter"
"github.com/peterh/liner"
)
var (
historyFn = filepath.Join(os.TempDir(), ".liner_example_history")
commands = []string{"load", "show", "list", "export"}
db *sqlx.DB
)
func init() {
var err error
db, err = sqlx.Connect("sqlite3", ":memory:")
if err != nil {
panic(err)
}
}
func main() {
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
if f, err := os.Open(historyFn); err == nil {
line.ReadHistory(f)
f.Close()
}
defer func() {
if f, err := os.Create(historyFn); err == nil {
line.WriteHistory(f)
f.Close()
}
}()
for {
statement, err := line.Prompt("➜ ")
if err != nil {
if err == liner.ErrPromptAborted {
break
}
fmt.Println(err.Error())
continue
}
line.AppendHistory(statement)
words := strings.Split(statement, " ")
switch {
case len(words) >= 1 && words[0] == "export":
exportCMD(words)
continue
case len(words) >= 1 && words[0] == "show":
showCMD(words)
continue
case len(words) >= 1 && words[0] == "list":
listCMD()
continue
case len(words) >= 3 && words[1] == "=" && words[2] == "load":
loadCMD(words)
continue
case len(words) >= 3 && words[1] == "=" && words[2] == "select":
selectCMD(words)
continue
case len(words) >= 3 && words[1] == "+=" && words[2] == "select":
selectAppendCMD(words)
continue
default:
fmt.Println("no such command supported")
}
}
}
func tableRender(header []string, rows [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(header)
table.SetAutoFormatHeaders(false)
table.AppendBulk(rows)
table.Render()
}
func createFileFromTable(tablename, filepath string) (err error) {
file, err := os.Create(filepath)
if err != nil {
return
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
headerColumns, rows, err := tableSelect(tablename, 0)
if err != nil {
return
}
// write header columns
err = writer.Write(headerColumns)
if err != nil {
return
}
for _, row := range rows {
err = writer.Write(row)
if err != nil {
return err
}
}
return nil
}
func list() (err error) {
rows, err := db.Query("select name from sqlite_master;")
if err != nil {
return
}
var tableRows [][]string
for rows.Next() {
var name string
err = rows.Scan(&name)
if err != nil {
return
}
tableRows = append(tableRows, []string{name})
}
tableRender([]string{"Tables"}, tableRows)
return
}
func validateCommandParamters(expected, found int) (ok bool) {
ok = (expected == found)
if expected != found {
fmt.Printf("error: wrong number of command parameters expected %d, found %d \n", expected, found)
return
}
return
}