From 311c076e5526aa8cb86abf1d8635c184d7dc4f53 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Tue, 14 Aug 2018 16:24:00 +0800 Subject: [PATCH] use defer to make sure terminal.Restore to run --- client.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index db4c022..b490fac 100644 --- a/client.go +++ b/client.go @@ -165,27 +165,27 @@ func (cc *Client) Shell() error { // this make CTRL+C works log.Debugf("turn terminal mode to raw") + oldState, _ := terminal.MakeRaw(0) + defer func() { + log.Debugf("restore terminal mode") + terminal.Restore(0, oldState) + }() + w, h, _ := terminal.GetSize(0) log.Debugf("request pty") if err := session.RequestPty("xterm", h, w, modes); err != nil { log.Errorf("request pty error: %s", err.Error()) - log.Debugf("restore terminal mode") - terminal.Restore(0, oldState) return err } log.Debugf("request shell") if err := session.Shell(); err != nil { log.Errorf("start shell error: %s", err.Error()) - log.Debugf("restore terminal mode") - terminal.Restore(0, oldState) return err } session.Wait() log.Debugf("session closed") - terminal.Restore(0, oldState) - log.Debugf("restore terminal mode") return nil }