-
Notifications
You must be signed in to change notification settings - Fork 10
/
query.go
66 lines (52 loc) · 1.5 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package commands
import (
"fmt"
"strings"
"github.com/AbGuthrie/goquery/v2/config"
"github.com/AbGuthrie/goquery/v2/hosts"
"github.com/AbGuthrie/goquery/v2/models"
"github.com/AbGuthrie/goquery/v2/utils"
prompt "github.com/c-bata/go-prompt"
)
func query(api models.GoQueryAPI, config *config.Config, cmdline string) error {
host, err := hosts.GetCurrentHost()
if err != nil {
return fmt.Errorf("No host is currently connected: %s", err)
}
args := strings.Split(cmdline, " ") // Separate command and arguments
if len(args) == 1 {
return fmt.Errorf("A query to run must be provided")
}
// TODO This needs to support Unicode/Runes
commandStripped := cmdline[strings.Index(cmdline, " ")+1:]
results, err := utils.ScheduleQueryAndWait(api, host.UUID, commandStripped)
if err != nil {
return err
}
utils.PrettyPrintQueryResults(results, config.PrintMode)
return nil
}
func queryHelp() string {
return "Schedule a query on a host and wait for results"
}
func querySuggest(cmdline string) []prompt.Suggest {
parts := strings.Split(cmdline, " ")
// The cmdline doesn't have enough components
if len(parts) < 2 {
return []prompt.Suggest{}
}
// If they've anything other than "from "
if parts[len(parts)-2] != "from" {
return []prompt.Suggest{}
}
prompts := []prompt.Suggest{}
// There is no connected host
host, err := hosts.GetCurrentHost()
if err != nil {
return prompts
}
for _, table := range host.Tables {
prompts = append(prompts, prompt.Suggest{table, ""})
}
return prompts
}