From e69260feb18e9f55e5ad7906a71f9f1171c8fd18 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Tue, 13 Oct 2015 17:02:38 +0200 Subject: [PATCH] Add debug mode (fix #18) --- cmd/gotty-client/main.go | 17 ++++++++++++++++- gotty-client.go | 10 +++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/gotty-client/main.go b/cmd/gotty-client/main.go index 290918e..5aeaf39 100644 --- a/cmd/gotty-client/main.go +++ b/cmd/gotty-client/main.go @@ -20,6 +20,14 @@ func main() { app.Usage = "GoTTY client for your terminal" app.ArgsUsage = "GOTTY_URL" + app.Flags = []cli.Flag{ + cli.BoolFlag{ + Name: "debug, D", + Usage: "Enable debug mode", + EnvVar: "GOTTY_CLIENT_DEBUG", + }, + } + app.Action = Action app.Run(os.Args) @@ -30,9 +38,16 @@ func Action(c *cli.Context) { logrus.Fatalf("usage: gotty-client [GoTTY URL]") } - url := c.Args()[0] + // setting up logrus + logrus.SetOutput(os.Stderr) + if c.Bool("debug") { + logrus.SetLevel(logrus.DebugLevel) + } else { + logrus.SetLevel(logrus.InfoLevel) + } // create Client + url := c.Args()[0] client, err := gottyclient.NewClient(url) if err != nil { logrus.Fatalf("Cannot create client: %v", err) diff --git a/gotty-client.go b/gotty-client.go index f3e80f1..7981f92 100644 --- a/gotty-client.go +++ b/gotty-client.go @@ -100,6 +100,7 @@ func (c *Client) GetAuthToken() (string, error) { return "", err } + logrus.Debugf("Fetching auth token auth-token: %q", target.String()) req, err := http.NewRequest("GET", target.String(), nil) req.Header = *header client := http.Client{} @@ -137,34 +138,36 @@ func (c *Client) Connect() error { if err != nil { return err } + logrus.Debugf("Auth-token: %q", authToken) // Open WebSocket connection target, header, err := GetWebsocketURL(c.URL) if err != nil { return err } + logrus.Debugf("Connecting to websocket: %q", target.String()) conn, _, err := c.Dialer.Dial(target.String(), *header) if err != nil { return err } c.Conn = conn + // Pass arguments and auth-token query, err := GetURLQuery(c.URL) if err != nil { return err } - var querySingle querySingleType = querySingleType{ Arguments: "?" + query.Encode(), AuthToken: authToken, } - json, err := json.Marshal(querySingle) if err != nil { logrus.Errorf("Failed to parse init message %v", err) return err } // Send Json + logrus.Debugf("Sending arguments and auth-token") err = c.write(json) if err != nil { return err @@ -177,6 +180,7 @@ func (c *Client) Connect() error { func (c *Client) pingLoop() { for { + logrus.Debugf("Sending ping") c.write([]byte("1")) time.Sleep(30 * time.Second) } @@ -286,7 +290,7 @@ func (c *Client) readLoop(done chan bool) { newTitle := string(data[1:]) fmt.Printf("\033]0;%s\007", newTitle) case '3': // json prefs - logrus.Debugf("Unhandled protocol message: json pref: %s", string(data)) + logrus.Debugf("Unhandled protocol message: json pref: %s", string(data[1:])) case '4': // autoreconnect logrus.Debugf("Unhandled protocol message: autoreconnect: %s", string(data)) default: