aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go41
1 files changed, 24 insertions, 17 deletions
diff --git a/main.go b/main.go
index 786c106..19a6fb6 100644
--- a/main.go
+++ b/main.go
@@ -21,13 +21,11 @@ func main() {
if len(os.Args) > 1 {
noninteractive = true
switch os.Args[1] {
- case "ls": // List tables
- choice = 1
case "edit": // Import table
- choice = 3
+ choice = 2
table = os.Args[2]
case "run": // Run query
- choice = 2
+ 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
@@ -36,7 +34,6 @@ Usage:
debby [command] [arguments]
The commands are:
- ls List tables
edit Import table
run Run query
help Print help
@@ -49,15 +46,13 @@ Running debby with any other arguments will run it as a dolt command.`
}
}
- // Menu loop. 1. Read tale names 2. Run query 3. Run and save query 4. Execute Dolt command 5. Exit
+ // Menu loop. 1. Run query 2. Edit a table 3. Execute Dolt command 4. Exit
for {
+ tables := ReadTableNames()
// Execute choice
switch choice {
case 1:
- // Read table names
- ReadTableNames()
- case 2:
if query == "" {
// Request query
fmt.Print("Enter your query: ")
@@ -66,8 +61,13 @@ Running debby with any other arguments will run it as a dolt command.`
// Table name will be empty. Run query
RunSQL(query, "")
- case 3:
+ case 2:
if table == "" {
+ // List tables sequentially
+ fmt.Print("Available tables: ")
+ for _, t := range tables {
+ fmt.Printf(" [ %s ]", t)
+ }
// Request table name
fmt.Print("Enter your table name: ")
fmt.Scanln(&table)
@@ -75,7 +75,7 @@ Running debby with any other arguments will run it as a dolt command.`
// Run query
RunSQL("select * from "+table+";", table)
- case 4:
+ case 3:
if len(args) == 0 {
// Request command. Make dolt bold
fmt.Print("\033[1mdolt\033[0m ")
@@ -86,7 +86,7 @@ Running debby with any other arguments will run it as a dolt command.`
// Execute command
ExecuteDoltCommand(args...)
- case 5:
+ case 4:
// Exit
return
}
@@ -138,13 +138,20 @@ func SaveToTable(table, file string) error {
return cmd.Run()
}
-func ReadTableNames() {
- // Run dolt ls
+func ReadTableNames() []string {
+ // Run dolt ls, ignore the first line and strip spaces for each following line
cmd := exec.Command("dolt", "ls")
- cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
- cmd.Stdin = os.Stdin
- cmd.Run()
+ out, err := cmd.Output()
+ if err != nil {
+ return []string{}
+ }
+ lines := strings.Split(string(out), "\n")
+ lines = lines[1:]
+ for i, line := range lines {
+ lines[i] = strings.TrimSpace(line)
+ }
+ return lines
}
func RunSQL(query, table string) error {