-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
62 lines (49 loc) · 1.06 KB
/
main.go
File metadata and controls
62 lines (49 loc) · 1.06 KB
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
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"os/signal"
"github.com/Dojo456/simple-sql-db/engine"
)
func main() {
ctx := context.Background()
scanner := bufio.NewScanner(os.Stdin)
sqlEngine, err := engine.New(ctx)
if err != nil {
log.Fatalln(fmt.Errorf("could not intialize SQL Engine: %w", err))
}
defer cleanup(sqlEngine)
go func() {
sigchan := make(chan os.Signal)
signal.Notify(sigchan, os.Interrupt)
<-sigchan
}()
for {
fmt.Println("\nenter command:")
// Scan() will return false on errors
if !scanner.Scan() {
fmt.Println(fmt.Errorf("error reading input: %w", scanner.Err()))
return
}
input := scanner.Text()
cmd, err := sqlEngine.Process(ctx, input)
if err != nil {
fmt.Println(fmt.Errorf("\nerror executing command: %w", err))
continue
}
fmt.Printf("\n%v\n", cmd)
}
}
func cleanup(e *engine.SQLEngine) {
fmt.Println("\ngracefully shutting down")
err := e.Cleanup()
if err != nil {
fmt.Println("could not perform clean shutdown", err)
} else {
fmt.Println("shutdown complete")
}
os.Exit(0)
}