Skip to content

Commit

Permalink
Merge pull request #10 from athos/feature/debug-option
Browse files Browse the repository at this point in the history
`--debug` option for printing debug info
  • Loading branch information
athos authored Jun 27, 2022
2 parents d8e7bd0 + 110d1ef commit ad925c5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Flags:
-m, --main=NAMESPACE Call the -main function for a namespace.
--init-ns=NAMESPACE Initialize REPL with the specified namespace. Defaults to "user".
-C, --color=auto When to use colors. Possible values: always, auto, none. Defaults to auto.
--debug Print debug information
--version Show application version.
Args:
Expand Down
1 change: 1 addition & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (
OutputHandler interface {
Out(s string)
Err(s string)
Debug(s string)
}
)

Expand Down
2 changes: 2 additions & 0 deletions client/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func (m *MockServer) Err(s string) {
m.errs = append(m.errs, s)
}

func (m *MockServer) Debug(s string) {}

func (m *MockServer) HandleErr(err error) {
m.handledErr = err
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/trench/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var unixUrlRegex = regexp.MustCompile(`^nrepl\+unix:(.+)$`)

type setupHelper struct {
errHandler client.ErrorHandler
debug bool
}

func readPortFromFile(protocol, portFile string) (int, bool, error) {
Expand Down Expand Up @@ -50,6 +51,7 @@ func (h setupHelper) nReplFactory(connBuilder client.ConnBuilder, initNS string)
InitNS: initNS,
OutputHandler: outHandler,
ErrorHandler: h.errHandler,
Debug: h.debug,
})
if err != nil {
h.errHandler.HandleErr(err)
Expand All @@ -65,6 +67,7 @@ func (h setupHelper) pReplFactory(connBuilder client.ConnBuilder, initNS string)
InitNS: initNS,
OutputHandler: outHandler,
ErrorHandler: h.errHandler,
Debug: h.debug,
})
if err != nil {
h.errHandler.HandleErr(err)
Expand Down
4 changes: 3 additions & 1 deletion cmd/trench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type cmdArgs struct {
mainNS *string
initNS *string
colorOption *string
debug *bool
args *[]string
}

Expand Down Expand Up @@ -66,6 +67,7 @@ var args = cmdArgs{
mainNS: kingpin.Flag("main", "Call the -main function for a namespace.").Short('m').PlaceHolder("NAMESPACE").String(),
initNS: kingpin.Flag("init-ns", "Initialize REPL with the specified namespace. Defaults to \"user\".").PlaceHolder("NAMESPACE").String(),
colorOption: kingpin.Flag("color", "When to use colors. Possible values: always, auto, none. Defaults to auto.").Default(COLOR_AUTO).Short('C').Enum(COLOR_NONE, COLOR_AUTO, COLOR_ALWAYS),
debug: kingpin.Flag("debug", "Print debug information.").Bool(),
args: kingpin.Arg("args", "Arguments to pass to -main. These will be ignored unless -m is specified.").Strings(),
}

Expand Down Expand Up @@ -99,7 +101,7 @@ func main() {

printer := repl.NewPrinter(colorized(*args.colorOption))
errHandler := errorHandler{printer}
helper := setupHelper{errHandler}
helper := setupHelper{errHandler, *args.debug}
protocol, connBuilder := helper.resolveConnection(&args)
initFile := strings.TrimSpace(*args.init)
filename := strings.TrimSpace(*args.file)
Expand Down
7 changes: 6 additions & 1 deletion nrepl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
OutputHandler client.OutputHandler
ErrorHandler client.ErrorHandler
ConnBuilder client.ConnBuilder
Debug bool
idGenerator func() string
}
)
Expand All @@ -49,7 +50,7 @@ func NewClient(opts *Opts) (*Client, error) {
pending: map[string]chan client.EvalResult{},
idGenerator: opts.idGenerator,
}
conn, err := Connect(&ConnOpts{opts.ConnBuilder})
conn, err := Connect(&ConnOpts{opts.ConnBuilder, opts.Debug, c})
if err != nil {
return nil, err
}
Expand All @@ -76,6 +77,10 @@ func (c *Client) Close() error {
return nil
}

func (c *Client) HandleDebugMessage(s string) {
c.outputHandler.Debug(s)
}

func (c *Client) CurrentNS() string {
return c.ns
}
Expand Down
37 changes: 31 additions & 6 deletions nrepl/nrepl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ type (
Handler func(Response)
ErrHandler func(error)

DebugHandler interface {
HandleDebugMessage(string)
}
DebugHandlerFunc func(string)

Conn struct {
socket net.Conn
encoder *bencode.Encoder
decoder *bencode.Decoder
socket net.Conn
encoder *bencode.Encoder
decoder *bencode.Decoder
debug bool
debugHandler DebugHandler
}

ConnOpts struct {
ConnBuilder client.ConnBuilder
Debug bool
DebugHandler
}

SessionInfo struct {
Expand All @@ -33,20 +42,33 @@ type (
}
)

func (fn DebugHandlerFunc) HandleDebugMessage(s string) {
fn(s)
}

func Connect(opts *ConnOpts) (conn *Conn, err error) {
connBuilder := opts.ConnBuilder
socket, err := connBuilder.Connect()
if err != nil {
return
}
debugHandler := opts.DebugHandler
if debugHandler == nil {
debugHandler = DebugHandlerFunc(func(_ string) {})
}
return &Conn{
socket: socket,
encoder: bencode.NewEncoder(socket),
decoder: bencode.NewDecoder(socket),
socket: socket,
encoder: bencode.NewEncoder(socket),
decoder: bencode.NewDecoder(socket),
debug: opts.Debug,
debugHandler: debugHandler,
}, nil
}

func (conn *Conn) Send(req client.Request) error {
if conn.debug {
conn.debugHandler.HandleDebugMessage(fmt.Sprintf("[DEBUG:SEND] %q\n", req))
}
return conn.encoder.Encode(map[string]bencode.Datum(req.(Request)))
}

Expand All @@ -58,6 +80,9 @@ func (conn *Conn) Recv() (client.Response, error) {
}
return nil, err
}
if conn.debug {
conn.debugHandler.HandleDebugMessage(fmt.Sprintf("[DEBUG:RECV] %q\n", datum))
}
dict, ok := datum.(map[string]bencode.Datum)
if !ok {
return nil, errors.New("response must be a dictionary")
Expand Down
19 changes: 17 additions & 2 deletions prepl/prepl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net"
"strings"
"sync"

"github.com/athos/trenchman/client"
Expand All @@ -31,16 +32,23 @@ type (
ns string
returnCh chan client.EvalResult
done chan struct{}
debug bool
}

Opts struct {
InitNS string
OutputHandler client.OutputHandler
ErrorHandler client.ErrorHandler
ConnBuilder client.ConnBuilder
Debug bool
}
)

func (resp *Response) String() string {
bs, _ := edn.Marshal(resp)
return string(bs)
}

func NewClient(opts *Opts) (*Client, error) {
connBuilder := opts.ConnBuilder
socket, err := connBuilder.Connect()
Expand All @@ -59,6 +67,7 @@ func NewClient(opts *Opts) (*Client, error) {
errHandler: opts.ErrorHandler,
ns: initNS,
done: make(chan struct{}),
debug: opts.Debug,
}
if err := c.Send("(set! *print-namespace-maps* false)\n"); err != nil {
return nil, err
Expand Down Expand Up @@ -88,7 +97,11 @@ func (c *Client) Close() error {
}

func (c *Client) Send(code client.Request) error {
if _, err := c.writer.WriteString(code.(string)); err != nil {
msg := code.(string)
if c.debug {
c.outputHandler.Debug(fmt.Sprintf("[DEBUG:SEND] %q\n", strings.TrimSpace(msg)))
}
if _, err := c.writer.WriteString(msg); err != nil {
return err
}
return c.writer.Flush()
Expand All @@ -102,12 +115,14 @@ func (c *Client) Recv() (client.Response, error) {
}
return nil, err
}
if c.debug {
c.outputHandler.Debug(fmt.Sprintf("[DEBUG:RECV] %s\n", resp.String()))
}
return client.Response(&resp), nil
}

func (c *Client) HandleResp(response client.Response) {
resp := response.(*Response)
// fmt.Printf("resp: %v\n", resp)
switch resp.Tag.String() {
case ":ret":
c.handleResult(resp)
Expand Down
4 changes: 4 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (r *Repl) Err(s string) {
r.printer.With(color.FgRed).Fprint(r.err, s)
}

func (r *Repl) Debug(s string) {
r.printer.With(color.FgHiBlue).Fprint(r.err, s)
}

func (r *Repl) handleResults(ch <-chan client.EvalResult, hidesResult bool) {
for {
select {
Expand Down

0 comments on commit ad925c5

Please sign in to comment.