aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoop Kiefte <ikojba@gmail.com>2023-10-06 01:38:19 +0100
committerJoop Kiefte <ikojba@gmail.com>2023-10-06 01:38:19 +0100
commit7c7af391621823742015d038001d0840f3d149e4 (patch)
tree59418bb2b15b85a2be75d8ee680c14d2611d88ca
parent36f3620b3d5418a9a0560a728102712e29a1bc4e (diff)
Make run implicit if dolt command doesn't exist
-rw-r--r--main.go28
1 files changed, 24 insertions, 4 deletions
diff --git a/main.go b/main.go
index 81fe85f..9ee50a3 100644
--- a/main.go
+++ b/main.go
@@ -77,8 +77,13 @@ Running debby with any other arguments will run it as a dolt command.`
fmt.Println(err)
}
case "":
- // Execute command
- ExecuteDoltCommand(args...)
+ // Execute command or if it's not a valid command, run as SQL
+ if !ExecuteDoltCommand(args...) {
+ err = RunSQL(strings.Join(args, " "), "")
+ if err != nil {
+ fmt.Println(err)
+ }
+ }
case "exit", "quit":
// Exit
return
@@ -123,7 +128,8 @@ func Readline() string {
return rtext
}
-func ExecuteDoltCommand(args ...string) {
+// Return false if the comand is not a real Dolt command
+func ExecuteDoltCommand(args ...string) bool {
var nargs []string
var join bool
// If an arg starts with a quote, start joining everything to the last element until an arg ends with a quote
@@ -148,7 +154,11 @@ func ExecuteDoltCommand(args ...string) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
- cmd.Run()
+ err := cmd.Run()
+ if err != nil {
+ return false
+ }
+ return true
}
func SaveToTable(table, file string) error {
@@ -260,12 +270,22 @@ func RunSQL(query, table string) error {
defer os.Remove(f.Name())
// Run the query and write the output to the file
+ // If the output is empty, return nil
cmd := exec.Command("dolt", "sql", "-q", query, "-r", "csv")
cmd.Stdout = f
+ cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return err
}
+ f.Close()
+ fi, err := os.Stat(f.Name())
+ if err != nil {
+ return err
+ }
+ if fi.Size() == 0 {
+ return nil
+ }
// Open the file in visidata
err = OpenVisidata(f.Name(), "-f", "csv")