Skip to content

Commit

Permalink
Merge pull request #20 from moul/fix-18
Browse files Browse the repository at this point in the history
Fix #18
  • Loading branch information
moul committed Oct 13, 2015
2 parents ba7cead + 66d2445 commit b251ae3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ $ brew install https://raw.githubusercontent.com/moul/ssh2docker/master/contrib/

### master (unreleased)

* Add debug mode (`--debug`/`-D`) ([#18](https://github.com/moul/gotty-client/issues/18))
* Improve error message when connecting by checking HTTP status code
* Fix arguments passing ([#16](https://github.com/moul/gotty-client/issues/16))

[full commits list](https://github.com/moul/gotty-client/compare/v1.1.0...master)
Expand Down
17 changes: 16 additions & 1 deletion cmd/gotty-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions gotty-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -108,6 +109,13 @@ func (c *Client) GetAuthToken() (string, error) {
return "", err
}

switch resp.StatusCode {
case 200:
// Everything is OK
default:
return "", fmt.Errorf("unknown status code: %d (%s)", resp.StatusCode, http.StatusText(resp.StatusCode))
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand All @@ -130,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
Expand All @@ -170,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)
}
Expand Down Expand Up @@ -279,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:
Expand Down

0 comments on commit b251ae3

Please sign in to comment.