aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2023-09-28 03:10:46 +0100
committerJoop Kiefte <ikojba@gmail.com>2023-09-28 03:10:46 +0100
commit145a0bdfa148a032a18b515e7913bb5f9f361208 (patch)
tree3befa9532feb479ab6bb1b5cc89d36fd4c6b545c
parent2f26e0bd0ec63d47425fb1a5faff06cfd33f246c (diff)
Change to REPL style interactive tool
-rw-r--r--main.go62
1 files changed, 22 insertions, 40 deletions
diff --git a/main.go b/main.go
index 013ada5..fe338b4 100644
--- a/main.go
+++ b/main.go
@@ -10,7 +10,7 @@ import (
)
func main() {
- var choice int
+ var choice string = "version"
var table string
var query string
var args []string
@@ -19,12 +19,11 @@ func main() {
if len(os.Args) > 1 {
noninteractive = true
- switch os.Args[1] {
+ choice = os.Args[1]
+ switch choice {
case "edit": // Import table
- choice = 2
table = os.Args[2]
case "run": // Run query
- choice = 1
query = strings.Join(os.Args[2:], " ")
case "help": // Print help
str := `debby is a command line tool that links your dolt database to visidata
@@ -43,7 +42,7 @@ Running debby with any other arguments will run it as a dolt command.`
fmt.Println(str)
return
default: // Run dolt command
- choice = 3
+ choice = ""
args = os.Args[1:]
}
}
@@ -54,41 +53,22 @@ Running debby with any other arguments will run it as a dolt command.`
// Execute choice
switch choice {
- case 1:
- if query == "" {
- // Request query
- fmt.Print("Enter your query: ")
- query = Readline()
- }
-
+ case "run":
// Table name will be empty. Run query
err = RunSQL(query, "")
if err != nil {
fmt.Println(err)
}
- case 2:
- if table == "" {
- // Request table name
- fmt.Print("Enter your table name: ")
- table = Readline()
- }
-
+ case "edit":
// Run query
err = RunSQL("select * from "+table+";", table)
if err != nil {
fmt.Println(err)
}
- case 3:
- if len(args) == 0 {
- // Request command. Make dolt bold
- fmt.Print("\033[1mdolt\033[0m ")
- argstr := Readline()
- args = strings.Split(argstr, " ")
- }
-
+ case "":
// Execute command
ExecuteDoltCommand(args...)
- case 4:
+ case "exit", "quit":
// Exit
return
}
@@ -97,26 +77,28 @@ Running debby with any other arguments will run it as a dolt command.`
if noninteractive {
return
}
- choice = 0
// Print menu
// List tables sequentially
fmt.Println("Tables: ", strings.Join(tables, ", "))
fmt.Println()
- fmt.Print("1. Run query ")
- fmt.Print("2. Edit table ")
- fmt.Print("3. Execute Dolt command ")
- fmt.Print("4. Exit ")
- fmt.Print("- Enter your choice: ")
+ fmt.Println("[run] a query, [edit] a table, run any dolt command, or [exit]")
+ fmt.Print("> ")
// Read user input
choicestr := Readline()
- choice = int(choicestr[0] - '0')
-
- // Reset all variables except choice
- table = ""
- query = ""
- args = []string{}
+ choices := strings.Split(choicestr, " ")
+ choice = choices[0]
+ switch choice {
+ case "run", "edit", "exit", "quit":
+ query = strings.Join(choices[1:], " ")
+ if len(choices) > 1 {
+ table = choices[1]
+ }
+ default:
+ choice = ""
+ }
+ args = choices
}
}