Skip to content

Commit

Permalink
fix: retaina and restore terminal mode on Windows (fixes #4251)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammed90 committed Oct 3, 2021
1 parent cbb045a commit 80a14ff
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cmd/cleanup_other.go
@@ -0,0 +1,6 @@
//go:build !windows
// +build !windows

package caddycmd

func cleanup() error { return nil }
24 changes: 24 additions & 0 deletions cmd/cleanup_windows.go
@@ -0,0 +1,24 @@
//go:build windows
// +build windows

package caddycmd

import "golang.org/x/sys/windows"

// Retain the original mode of the console to restore later.
// See: https://github.com/caddyserver/caddy/issues/4251
// The evaluation of this takes place before calling init()
// ref: https://golang.org/ref/spec#Order_of_evaluation
var inMode, outMode uint32
var _ = windows.GetConsoleMode(windows.Stdin, &inMode)
var _ = windows.GetConsoleMode(windows.Stdout, &outMode)

func cleanup() error {
if err := windows.SetConsoleMode(windows.Stdin, inMode); err != nil {
return err
}
if err := windows.SetConsoleMode(windows.Stdout, outMode); err != nil {
return err
}
return nil
}
4 changes: 3 additions & 1 deletion cmd/main.go
Expand Up @@ -85,7 +85,9 @@ func Main() {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", subcommand.Name, err)
}

if err := cleanup(); err != nil {
fmt.Fprintf(os.Stderr, "error restoring console to functional state: %v\n", err)
}
os.Exit(exitCode)
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -27,6 +27,7 @@ require (
go.uber.org/zap v1.19.0
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/net v0.0.0-20210614182718-04defd469f4e
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
google.golang.org/genproto v0.0.0-20210604141403-392c879c8b08
google.golang.org/protobuf v1.27.1
Expand Down

0 comments on commit 80a14ff

Please sign in to comment.