aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2023-09-28 01:34:07 +0100
committerJoop Kiefte <ikojba@gmail.com>2023-09-28 01:34:07 +0100
commit217b093c56f89d19ec806a6956bb2c8c4fd850b0 (patch)
treedb6fc89b57195ad059d59b8168d574afe53f9e8e
parent8b0ce4bdb99f7069d47d01cbc215c0257f827275 (diff)
goimports and fixing build (scan might be wrong now)
-rw-r--r--main.go100
1 files changed, 70 insertions, 30 deletions
diff --git a/main.go b/main.go
index 2aa9d63..d1ccf4a 100644
--- a/main.go
+++ b/main.go
@@ -12,19 +12,44 @@ import (
var once = sync.Once{}
func main() {
+ var choice int
+ var table string
+ var query string
+ var args []string
+
+ if len(os.Args) > 1 {
+ switch os.Args[1] {
+ case "ls": // List tables
+ choice = 1
+ case "edit": // Import table
+ choice = 3
+ case "run": // Run query
+ choice = 2
+ query = strings.Join(os.Args[2:], " ")
+ case "help": // Print help
+ str := `debby is a command line tool that links your dolt database to visidata
+
+Usage:
+ debby [command] [arguments]
+
+The commands are:
+ ls List tables
+ edit Import table
+ run Run query
+ help Print help
+
+Runnning debby without any arguments will start debby in interactive mode.
+Running debby with any other arguments will run it as a dolt command.`
+
+ fmt.Println(str)
+ return
+ }
+ } else {
+ choice = 0
+ }
+
// Menu loop. 1. Read tale names 2. Run query 3. Run and save query 4. Execute Dolt command 5. Exit
for {
- // Print menu
- fmt.Println("1. Read table names")
- fmt.Println("2. Run query")
- fmt.Println("3. Edit table")
- fmt.Println("4. Execute Dolt command")
- fmt.Println("5. Exit")
- fmt.Print("Enter your choice: ")
-
- // Read user input
- var choice int
- fmt.Scanln(&choice)
// Execute choice
switch choice {
@@ -32,36 +57,53 @@ func main() {
// Read table names
ReadTableNames()
case 2:
- // Request query
- fmt.Print("Enter your query: ")
- var query string
- fmt.Scanln(&query)
-
+ if query == "" {
+ // Request query
+ fmt.Print("Enter your query: ")
+ fmt.Scanln(&query)
+ }
+
// Table name will be empty. Run query
RunSQL(query, "")
case 3:
- // Request table name
- fmt.Print("Enter your table name: ")
- var table string
- fmt.Scanln(&table)
+ if table == "" {
+ // Request table name
+ fmt.Print("Enter your table name: ")
+ fmt.Scanln(&table)
+ }
// Run query
- RunSQL("select * from " + table + ";", table)
+ RunSQL("select * from "+table+";", table)
case 4:
- // Request command
- fmt.Print("dolt ")
- var args string
- fmt.Scanln(&args)
+ if len(args) == 0 {
+ // Request command
+ fmt.Print("dolt ")
+ fmt.Scanln(&args)
+ }
// Execute command
- ExecuteDoltCommand(args)
+ ExecuteDoltCommand(args...)
case 5:
// Exit
return
default:
- // Invalid choice
+ // Print menu
+ fmt.Println("1. Read table names")
+ fmt.Println("2. Run query")
+ fmt.Println("3. Edit table")
+ fmt.Println("4. Execute Dolt command")
+ fmt.Println("5. Exit")
+ fmt.Print("Enter your choice: ")
+
+ // Read user input
+ fmt.Scanln(&choice)
fmt.Println("Invalid choice")
}
+
+ // Reset all variables except choice
+ table = ""
+ query = ""
+ args = []string{}
}
}
@@ -106,7 +148,7 @@ func RunSQL(query, table string) error {
if err != nil {
return err
}
-
+
// Open the file in visidata
OpenVisidata(f.Name(), "-f", "csv")
@@ -122,7 +164,6 @@ func RunSQL(query, table string) error {
return nil
}
-
func OpenVisidata(args ...string) {
vdcmd := os.Getenv("EDITOR")
once.Do(func() {
@@ -145,4 +186,3 @@ func OpenVisidata(args ...string) {
cmd.Stdin = os.Stdin
cmd.Run()
}
-