(ctx context.Context, h *graph.Handle, queryLanguage string, timeout time.Duration)
| 88 | ) |
| 89 | |
| 90 | func Repl(ctx context.Context, h *graph.Handle, queryLanguage string, timeout time.Duration) error { |
| 91 | if queryLanguage == "" { |
| 92 | queryLanguage = defaultLanguage |
| 93 | } |
| 94 | l := query.GetLanguage(queryLanguage) |
| 95 | if l == nil || l.REPL == nil { |
| 96 | return fmt.Errorf("unsupported query language: %q", queryLanguage) |
| 97 | } |
| 98 | ses := l.REPL(h.QuadStore) |
| 99 | |
| 100 | term, err := terminal(history) |
| 101 | if os.IsNotExist(err) { |
| 102 | fmt.Printf("creating new history file: %q\n", history) |
| 103 | } |
| 104 | defer persist(term, history) |
| 105 | |
| 106 | var ( |
| 107 | prompt = ps1 |
| 108 | |
| 109 | code string |
| 110 | ) |
| 111 | |
| 112 | newCtx := func() (context.Context, func()) { return ctx, func() {} } |
| 113 | if timeout > 0 { |
| 114 | newCtx = func() (context.Context, func()) { return context.WithTimeout(ctx, timeout) } |
| 115 | } |
| 116 | |
| 117 | for { |
| 118 | select { |
| 119 | case <-ctx.Done(): |
| 120 | return ctx.Err() |
| 121 | default: |
| 122 | } |
| 123 | if len(code) == 0 { |
| 124 | prompt = ps1 |
| 125 | } else { |
| 126 | prompt = ps2 |
| 127 | } |
| 128 | line, err := term.Prompt(prompt) |
| 129 | if err != nil { |
| 130 | if err == io.EOF { |
| 131 | fmt.Println() |
| 132 | return nil |
| 133 | } |
| 134 | return err |
| 135 | } |
| 136 | |
| 137 | term.AppendHistory(line) |
| 138 | |
| 139 | line = strings.TrimSpace(line) |
| 140 | if len(line) == 0 || line[0] == '#' { |
| 141 | continue |
| 142 | } |
| 143 | |
| 144 | if code == "" { |
| 145 | cmd, args := splitLine(line) |
| 146 | |
| 147 | switch cmd { |
no test coverage detected