From 833427895add3093db53c8eb9612e76b13f4e3ab Mon Sep 17 00:00:00 2001 From: olebeck <31539311+olebeck@users.noreply.github.com> Date: Thu, 8 Sep 2022 10:39:46 +0200 Subject: [PATCH] add press enter to exit --- cmd/bedrocktool/main.go | 24 +++++++++++++++++------- subcommands/world/world.go | 2 +- utils/utils.go | 3 ++- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/bedrocktool/main.go b/cmd/bedrocktool/main.go index 4f34e8b..22d0c30 100644 --- a/cmd/bedrocktool/main.go +++ b/cmd/bedrocktool/main.go @@ -20,12 +20,11 @@ import ( "github.com/sirupsen/logrus" ) -func exit() { - logrus.Info("\nExiting\n") - for i := len(utils.G_exit) - 1; i >= 0; i-- { // go through cleanup functions reversed - utils.G_exit[i]() +func cleanup() { + logrus.Info("\nCleaning up\n") + for i := len(utils.G_cleanup_funcs) - 1; i >= 0; i-- { // go through cleanup functions reversed + utils.G_cleanup_funcs[i]() } - os.Exit(0) } func main() { @@ -43,6 +42,10 @@ func main() { println("https://github.com/bedrock-tool/bedrocktool/issues") println("And attach the error info, describe what you did to get this error.") println("Thanks!\n") + if utils.G_interactive { + input := bufio.NewScanner(os.Stdin) + input.Scan() + } os.Exit(1) } }() @@ -91,6 +94,7 @@ func main() { r, _ := regexp.Compile(`[\n\r]`) target = string(r.ReplaceAll([]byte(target), []byte(""))) os.Args = append(os.Args, target) + utils.G_interactive = true } } } @@ -107,11 +111,17 @@ func main() { go func() { <-sigs cancel() - exit() + cleanup() }() ret := subcommands.Execute(ctx) - exit() + cleanup() + + if utils.G_interactive { + logrus.Info("Press Enter to exit.") + input := bufio.NewScanner(os.Stdin) + input.Scan() + } os.Exit(int(ret)) } diff --git a/subcommands/world/world.go b/subcommands/world/world.go index b37cfa3..ee4f6b2 100644 --- a/subcommands/world/world.go +++ b/subcommands/world/world.go @@ -478,7 +478,7 @@ func (w *WorldState) OnConnect(proxy *utils.ProxyContext) { w.proxy.SendMessage("use /setname \nto set the world name") - utils.G_exit = append(utils.G_exit, func() { + utils.G_cleanup_funcs = append(utils.G_cleanup_funcs, func() { w.SaveAndReset() }) diff --git a/utils/utils.go b/utils/utils.go index f909df6..fdf1ad8 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -33,7 +33,8 @@ const SERVER_ADDRESS_HELP = `accepted server address formats: var ( G_debug bool G_preload_packs bool - G_exit []func() = []func(){} + G_interactive bool + G_cleanup_funcs []func() = []func(){} ) var A string