forked from mathaou/termdbms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
190 lines (162 loc) · 4.02 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
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
package main
import (
"database/sql"
"flag"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"io/fs"
"io/ioutil"
_ "modernc.org/sqlite"
"os"
"path/filepath"
"strings"
"termdbms/database"
. "termdbms/tuiutil"
. "termdbms/viewer"
)
type DatabaseType string
const (
debugPath = "" // set to whatever hardcoded path for testing
)
const (
DatabaseSQLite DatabaseType = "sqlite"
DatabaseMySQL DatabaseType = "mysql"
)
var (
debug bool
path string
databaseType string
theme string
help bool
ascii bool
)
func main() {
debug = debugPath != ""
flag.Usage = func() {
help := GetHelpText()
lines := strings.Split(help, "\n")
for _, v := range lines {
println(v)
}
}
argLength := len(os.Args[1:])
if (argLength > 4 || argLength == 0) && !debug {
fmt.Printf("ERROR: Invalid number of arguments supplied: %d\n", argLength)
flag.Usage()
os.Exit(1)
}
// flags declaration using flag package
flag.StringVar(&databaseType, "d", string(DatabaseSQLite), "Specifies the SQL driver to use. Defaults to SQLite.")
flag.StringVar(&path, "p", "", "Path to the database file.")
flag.StringVar(&theme, "t", "default", "sets the color theme of the app.")
flag.BoolVar(&help, "h", false, "Prints the help message.")
flag.BoolVar(&ascii, "a", false, "Denotes that the app should render with minimal styling to remove ANSI sequences.")
flag.Parse()
handleFlags()
var c *sql.Rows
defer func() {
if c != nil {
c.Close()
}
}()
if debug {
path = debugPath
}
for i, v := range ValidThemes {
if theme == v {
SelectedTheme = i
break
}
}
if theme == "" {
theme = "default"
}
// gets a sqlite instance for the database file
if exists, _ := FileExists(path); exists {
fmt.Printf("ERROR: Database file could not be found at %s\n", path)
os.Exit(1)
}
if valid, _ := Exists(HiddenTmpDirectoryName); valid {
filepath.Walk(HiddenTmpDirectoryName, func(path string, info fs.FileInfo, err error) error {
if strings.HasPrefix(path, fmt.Sprintf("%s/.", HiddenTmpDirectoryName)) && !info.IsDir() {
os.Remove(path) // remove all temp databaess
}
return nil
})
} else {
os.Mkdir(HiddenTmpDirectoryName, 0777)
}
database.IsCSV = strings.HasSuffix(path, ".csv")
dst := path
if database.IsCSV { // convert the csv to sql, then run the sql through a database
sqlFile := strings.TrimSuffix(path, ".csv")
sqlFile = filepath.Base(sqlFile)
path = Convert(path, sqlFile, true)
csvDBFile := HiddenTmpDirectoryName + "/" + sqlFile + ".db"
os.Create(csvDBFile)
dst, _ = filepath.Abs(csvDBFile)
d, _ := sql.Open(database.DriverString, dst)
f, _ := os.Open(path)
b, _ := ioutil.ReadAll(f)
query := string(b)
_, err := d.Exec(query)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
d.Close()
os.Remove(path) // this deletes the converted .sql file
}
dst, _, _ = CopyFile(dst)
db := database.GetDatabaseForFile(dst)
defer func() {
if db == nil {
db.Close()
}
}()
// initializes the model used by bubbletea
m := GetNewModel(dst, db)
InitialModel = &m
InitialModel.InitialFileName = path
err := InitialModel.SetModel(c, db)
if err != nil {
fmt.Printf("%v", err)
os.Exit(1)
}
// creates the program
Program = tea.NewProgram(InitialModel,
tea.WithAltScreen(),
tea.WithMouseAllMotion())
if err := Program.Start(); err != nil {
fmt.Printf("ERROR: Error initializing the sqlite viewer: %v", err)
os.Exit(1)
}
}
func handleFlags() {
if path == "" && !debug {
fmt.Printf("ERROR: no path for database.\n")
flag.Usage()
os.Exit(1)
}
if help {
flag.Usage()
os.Exit(0)
}
if ascii {
Ascii = true
lipgloss.SetColorProfile(termenv.Ascii)
}
if path != "" && !IsUrl(path) {
fmt.Printf("ERROR: Invalid path %s\n", path)
flag.Usage()
os.Exit(1)
}
if databaseType != string(DatabaseMySQL) &&
databaseType != string(DatabaseSQLite) {
fmt.Printf("Invalid database driver specified: %s", databaseType)
os.Exit(1)
}
database.DriverString = databaseType
}