-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
164 lines (137 loc) · 3.27 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
package main
import (
"database/sql"
"flag"
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
_ "modernc.org/sqlite"
"os"
"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 (
path string
databaseType string
theme string
help bool
ascii bool
)
func main() {
debug := debugPath != ""
// if not debug, then this section parses and validates cmd line arguments
if !debug {
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 {
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 {
os.RemoveAll(HiddenTmpDirectoryName)
}
os.Mkdir(HiddenTmpDirectoryName, 0777)
// steps
// make a copy of the database file, load this
dst, _, _ := CopyFile(path)
// keep a track of the original file name
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 := SetModel(InitialModel, 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 == "" {
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
}