forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.go
142 lines (128 loc) · 3.32 KB
/
repl.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package command
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/aperturerobotics/cayley/clog"
"github.com/aperturerobotics/cayley/internal/repl"
"github.com/aperturerobotics/cayley/query"
)
const (
keyQueryTimeout = "query.timeout"
)
func getContext() (context.Context, func()) {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func() {
select {
case <-ch:
case <-ctx.Done():
}
signal.Stop(ch)
cancel()
}()
return ctx, cancel
}
func registerQueryFlags(cmd *cobra.Command) {
langs := query.Languages()
cmd.Flags().Bool("init", false, "initialize the database before using it")
cmd.Flags().String("lang", "gizmo", `query language to use ("`+strings.Join(langs, `", "`)+`")`)
cmd.Flags().DurationP("timeout", "t", 30*time.Second, "elapsed time until an individual query times out")
viper.BindPFlag(keyQueryTimeout, cmd.Flags().Lookup("timeout"))
registerLoadFlags(cmd)
}
func NewReplCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "repl",
Short: "Drop into a REPL of the given query language.",
RunE: func(cmd *cobra.Command, args []string) error {
printBackendInfo()
p := mustSetupProfile(cmd)
defer mustFinishProfile(p)
h, err := openForQueries(cmd)
if err != nil {
return err
}
defer h.Close()
ctx, cancel := getContext()
defer cancel()
timeout := viper.GetDuration("timeout")
lang, _ := cmd.Flags().GetString("lang")
return repl.Repl(ctx, h, lang, timeout)
},
}
registerQueryFlags(cmd)
return cmd
}
func NewQueryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Aliases: []string{"qu"},
Short: "Run a query in a specified database and print results.",
RunE: func(cmd *cobra.Command, args []string) error {
var querystr string
if len(args) == 0 {
bytes, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("error occured while reading from stdin : %s", err)
}
querystr = string(bytes)
} else if len(args) == 1 {
querystr = args[0]
} else {
return fmt.Errorf("query accepts only one argument, the query string or nothing for reading from stdin")
}
clog.Infof("Query:\n%s", querystr)
printBackendInfo()
p := mustSetupProfile(cmd)
defer mustFinishProfile(p)
h, err := openForQueries(cmd)
if err != nil {
return err
}
defer h.Close()
ctx, cancel := getContext()
defer cancel()
timeout := viper.GetDuration("timeout")
if timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
lang, _ := cmd.Flags().GetString("lang")
limit, err := cmd.Flags().GetInt("limit")
if err != nil {
return err
}
enc := json.NewEncoder(os.Stdout)
it, err := query.Execute(ctx, h, lang, querystr, query.Options{
Collation: query.JSON,
Limit: limit,
})
if err != nil {
return err
}
defer it.Close()
for i := 0; it.Next(ctx) && (limit <= 0 || i < limit); i++ {
res, err := it.Result(ctx)
if err != nil {
return err
}
if err = enc.Encode(res); err != nil {
return err
}
}
return it.Err()
},
}
registerQueryFlags(cmd)
cmd.Flags().IntP("limit", "n", 100, "limit a number of results")
return cmd
}