Skip to content

Commit

Permalink
revert readline lib because of weird behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
BedrockSquirrel committed Jun 23, 2024
1 parent aef1b46 commit fec23c4
Showing 1 changed file with 34 additions and 54 deletions.
88 changes: 34 additions & 54 deletions tools/edbconnect/main/main.go
Original file line number Diff line number Diff line change
@@ -1,86 +1,68 @@
package main

import (
"bufio"
"database/sql"
"errors"
"io"
"log"
"fmt"
"os"
"strings"

"github.com/chzyer/readline"
"github.com/ten-protocol/go-ten/go/enclave/storage/init/edgelessdb"
"github.com/ten-protocol/go-ten/integration/common/testlog"
)

func main() {
log.Println("Retrieving Edgeless DB credentials...")
fmt.Println("Retrieving Edgeless DB credentials...")
creds, found, err := edgelessdb.LoadCredentialsFromFile()
if err != nil {
log.Println("Error loading credentials from file:", err)
fmt.Println("Error loading credentials from file:", err)
panic(err)
}
if !found {
panic("No existing EDB credentials found.")
}
log.Println("Found existing EDB credentials. Creating TLS config...")
fmt.Println("Found existing EDB credentials. Creating TLS config...")
cfg, err := edgelessdb.CreateTLSCfg(creds)
if err != nil {
log.Println("Error creating TLS config from credentials:", err)
fmt.Println("Error creating TLS config from credentials:", err)
panic(err)
}
log.Println("TLS config created. Connecting to Edgeless DB...")
fmt.Println("TLS config created. Connecting to Edgeless DB...")
testlog.SetupSysOut()
db, err := edgelessdb.ConnectToEdgelessDB("obscuronode-edgelessdb", cfg, testlog.Logger())
if err != nil {
log.Println("Error connecting to Edgeless DB:", err)
fmt.Println("Error connecting to Edgeless DB:", err)
panic(err)
}
log.Println("Connected to Edgeless DB.")
fmt.Println("Connected to Edgeless DB.")

startREPL(db)

err = db.Close()
if err != nil {
log.Println("Error closing Edgeless DB connection:", err)
fmt.Println("Error closing Edgeless DB connection:", err)
panic(err)
}
}

// Starts a loop that reads user input and runs queries against the Edgeless DB until user types "exit"
func startREPL(db *sql.DB) {
rl, err := readline.NewEx(&readline.Config{
Prompt: ">>> ",
HistoryFile: "/tmp/readline.tmp",
InterruptPrompt: "^C",
EOFPrompt: "exit",
})
if err != nil {
log.Fatal(err)
}
defer rl.Close()
log.SetOutput(rl.Stderr())
for {
log.Println("\nEnter a query to run against the Edgeless DB (or type 'exit' to quit):")
query, err := rl.Readline()
if err != nil { // Handle EOF and Interrupt errors
if errors.Is(err, readline.ErrInterrupt) {
if len(query) == 0 {
break
} else {
continue
}
} else if errors.Is(err, io.EOF) {
break
}
log.Println("Error reading user input:", err)
fmt.Println("\nEnter a query to run against the Edgeless DB (or type 'exit' to quit):")
reader := bufio.NewReader(os.Stdin)
fmt.Print(">>> ") // Display the prompt

query, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading user input:", err)
continue
}
// line break for readability
log.Println()

// Trim the newline character and surrounding whitespace
query = strings.TrimSpace(query)

// line break for readability
fmt.Println()

if query == "" {
continue
}
Expand All @@ -100,28 +82,27 @@ func startREPL(db *sql.DB) {
runExec(db, query)
}
}
log.Println("Exiting...")
fmt.Println("Exiting...")
}

func runQuery(db *sql.DB, query string) {
rows, err := db.Query(query)
if err != nil {
log.Println("Error executing query:", err)
fmt.Println("Error executing query:", err)
return
}
defer rows.Close()

cols, err := rows.Columns()
if err != nil {
log.Println("Error fetching columns:", err)
fmt.Println("Error fetching columns:", err)
return
}

// Print column headers
for _, colName := range cols {
log.Printf("%s\t", colName)
fmt.Printf("%s\t", colName)
}
log.Println()

// Prepare a slice to hold the values
values := make([]interface{}, len(cols))
Expand All @@ -133,7 +114,7 @@ func runQuery(db *sql.DB, query string) {

err = rows.Scan(valuePtrs...)
if err != nil {
log.Println("Error scanning row:", err)
fmt.Println("Error scanning row:", err)
return
}

Expand All @@ -142,37 +123,36 @@ func runQuery(db *sql.DB, query string) {
// Handle NULL values and convert byte slices to strings
switch v := val.(type) {
case nil:
log.Print("NULL\t")
fmt.Print("NULL\t")
case []byte:
if isPrintableString(v) {
log.Printf("%s\t", string(v))
fmt.Printf("%s\t", string(v))
} else {
log.Printf("%x\t", v) // Print binary data as hexadecimal
fmt.Printf("%x\t", v) // Print binary data as hexadecimal
}
default:
log.Printf("%v\t", v)
fmt.Printf("%v\t", v)
}
}
log.Println()
}

if err = rows.Err(); err != nil {
log.Println("Error during row iteration:", err)
fmt.Println("Error during row iteration:", err)
}
}

func runExec(db *sql.DB, query string) {
result, err := db.Exec(query)
if err != nil {
log.Println("Error executing query against Edgeless DB:", err)
fmt.Println("Error executing query against Edgeless DB:", err)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
log.Println("Error getting number of rows affected:", err)
fmt.Println("Error getting number of rows affected:", err)
return
}
log.Println("Number of rows affected:", rowsAffected)
fmt.Println("Number of rows affected:", rowsAffected)
}

// isPrintableString checks if a byte slice contains only printable characters
Expand Down

0 comments on commit fec23c4

Please sign in to comment.