From 6f0be3b4e48dc77823435e6fd45c1e66a7824899 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Thu, 5 Dec 2019 21:46:31 -0800 Subject: [PATCH 1/4] Adds visible error handling for bad exits and improves exit code use --- client.go | 12 ++++++------ cui/cui.go | 9 +++++++-- main.go | 7 ++++--- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/client.go b/client.go index 1704ad6..6ae0f40 100644 --- a/client.go +++ b/client.go @@ -47,8 +47,8 @@ func (c *client) GetSizeOnce() { cmd.Stdin = os.Stdin out, err := cmd.Output() if err != nil { - fmt.Println("Fatal error: Unable to retrieve terminal size") - os.Exit(5) + cui.ExitMessage = "Fatal error: Unable to retrieve terminal size" + cui.Exit(5) } var h, w int _, _ = fmt.Sscan(string(out), &h, &w) @@ -66,8 +66,8 @@ func (c *client) GetSize() { cmd.Stdin = os.Stdin out, err := cmd.Output() if err != nil { - fmt.Println("Fatal error: Unable to retrieve terminal size") - os.Exit(5) + cui.ExitMessage = "Fatal error: Unable to retrieve terminal size" + cui.Exit(5) } var h, w int _, _ = fmt.Sscan(string(out), &h, &w) @@ -157,7 +157,7 @@ func (c *client) TakeControlInput() { c.Scroll(-1) case 'q', 'Q': // quit bombadillo - cui.Exit() + cui.Exit(0) case 'g': // scroll to top c.ClearMessage() @@ -278,7 +278,7 @@ func (c *client) simpleCommand(action string) { action = strings.ToUpper(action) switch action { case "Q", "QUIT": - cui.Exit() + cui.Exit(0) case "H", "HOME": if c.Options["homeurl"] != "unset" { go c.Visit(c.Options["homeurl"]) diff --git a/cui/cui.go b/cui/cui.go index 85b14cc..3565461 100644 --- a/cui/cui.go +++ b/cui/cui.go @@ -26,6 +26,8 @@ var Shapes = map[string]string{ "abr": "▟", } +var ExitMessage string + func MoveCursorTo(row, col int) { fmt.Printf("\033[%d;%dH", row, col) } @@ -44,9 +46,12 @@ func moveCursorToward(dir string, amount int) { } // Exit performs cleanup operations before exiting the application -func Exit() { +func Exit(exitCode int) { CleanupTerm() - os.Exit(0) + if ExitMessage != "" { + fmt.Print(ExitMessage, "\n") + } + os.Exit(exitCode) } // InitTerm sets the terminal modes appropriate for Bombadillo diff --git a/main.go b/main.go index bc986c0..205f772 100644 --- a/main.go +++ b/main.go @@ -165,7 +165,7 @@ func handleSignals(c <-chan os.Signal) { cui.InitTerm() bombadillo.Draw() case syscall.SIGINT: - cui.Exit() + cui.Exit(130) } } } @@ -197,11 +197,12 @@ func main() { args := flag.Args() cui.InitTerm() - defer cui.Exit() + defer cui.Exit(0) err := initClient() if err != nil { // if we can't initialize we should bail out - panic(err) + cui.ExitMessage = err.Error() + cui.Exit(1) } // watch for signals, send them to be handled From 8b004df1d5d6b1697fa244914fb2c117cfd437de Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Thu, 5 Dec 2019 21:55:07 -0800 Subject: [PATCH 2/4] Better specific errors on bootup --- main.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 205f772..3a6908f 100644 --- a/main.go +++ b/main.go @@ -93,10 +93,11 @@ func lowerCaseOpt(opt, val string) string { } } -func loadConfig() error { +func loadConfig() { err := os.MkdirAll(bombadillo.Options["configlocation"], 0755) if err != nil { - return fmt.Errorf("Error creating configlocation: %s", err.Error()) + cui.ExitMessage = fmt.Sprintf("Error creating configlocation: %s", err.Error()) + cui.Exit(3) } fp := filepath.Join(bombadillo.Options["configlocation"], ".bombadillo.ini") @@ -104,7 +105,8 @@ func loadConfig() error { if err != nil { err = saveConfig() if err != nil { - return err + cui.ExitMessage = fmt.Sprintf("Error saving config during bootup: %s", err.Error()) + cui.Exit(4) } } @@ -138,17 +140,14 @@ func loadConfig() error { for _, v := range settings.Certs { bombadillo.Certs.Add(v.Key, v.Value) } - - return nil } -func initClient() error { +func initClient() { bombadillo = MakeClient(" ((( Bombadillo ))) ") - err := loadConfig() + loadConfig() if bombadillo.Options["tlscertificate"] != "" && bombadillo.Options["tlskey"] != "" { bombadillo.Certs.LoadCertificate(bombadillo.Options["tlscertificate"], bombadillo.Options["tlskey"]) } - return err } // In the event of specific signals, ensure the display is shown correctly. @@ -198,12 +197,7 @@ func main() { cui.InitTerm() defer cui.Exit(0) - err := initClient() - if err != nil { - // if we can't initialize we should bail out - cui.ExitMessage = err.Error() - cui.Exit(1) - } + initClient() // watch for signals, send them to be handled c := make(chan os.Signal, 1) From fde463be0ffe437853e5c9af38398682b025e140 Mon Sep 17 00:00:00 2001 From: sloumdrone Date: Thu, 5 Dec 2019 22:01:22 -0800 Subject: [PATCH 3/4] Removes unnecessary variable from exit routine --- client.go | 10 ++++------ cui/cui.go | 8 +++----- main.go | 12 ++++++------ 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index 6ae0f40..7a24c94 100644 --- a/client.go +++ b/client.go @@ -47,8 +47,7 @@ func (c *client) GetSizeOnce() { cmd.Stdin = os.Stdin out, err := cmd.Output() if err != nil { - cui.ExitMessage = "Fatal error: Unable to retrieve terminal size" - cui.Exit(5) + cui.Exit(5, "Fatal error: Unable to retrieve terminal size") } var h, w int _, _ = fmt.Sscan(string(out), &h, &w) @@ -66,8 +65,7 @@ func (c *client) GetSize() { cmd.Stdin = os.Stdin out, err := cmd.Output() if err != nil { - cui.ExitMessage = "Fatal error: Unable to retrieve terminal size" - cui.Exit(5) + cui.Exit(5, "Fatal error: Unable to retrieve terminal size") } var h, w int _, _ = fmt.Sscan(string(out), &h, &w) @@ -157,7 +155,7 @@ func (c *client) TakeControlInput() { c.Scroll(-1) case 'q', 'Q': // quit bombadillo - cui.Exit(0) + cui.Exit(0, "") case 'g': // scroll to top c.ClearMessage() @@ -278,7 +276,7 @@ func (c *client) simpleCommand(action string) { action = strings.ToUpper(action) switch action { case "Q", "QUIT": - cui.Exit(0) + cui.Exit(0, "") case "H", "HOME": if c.Options["homeurl"] != "unset" { go c.Visit(c.Options["homeurl"]) diff --git a/cui/cui.go b/cui/cui.go index 3565461..b6ca90b 100644 --- a/cui/cui.go +++ b/cui/cui.go @@ -26,8 +26,6 @@ var Shapes = map[string]string{ "abr": "▟", } -var ExitMessage string - func MoveCursorTo(row, col int) { fmt.Printf("\033[%d;%dH", row, col) } @@ -46,10 +44,10 @@ func moveCursorToward(dir string, amount int) { } // Exit performs cleanup operations before exiting the application -func Exit(exitCode int) { +func Exit(exitCode int, msg string) { CleanupTerm() - if ExitMessage != "" { - fmt.Print(ExitMessage, "\n") + if msg != "" { + fmt.Print(msg, "\n") } os.Exit(exitCode) } diff --git a/main.go b/main.go index 3a6908f..abaf0eb 100644 --- a/main.go +++ b/main.go @@ -96,8 +96,8 @@ func lowerCaseOpt(opt, val string) string { func loadConfig() { err := os.MkdirAll(bombadillo.Options["configlocation"], 0755) if err != nil { - cui.ExitMessage = fmt.Sprintf("Error creating configlocation: %s", err.Error()) - cui.Exit(3) + exitMsg := fmt.Sprintf("Error creating configlocation: %s", err.Error()) + cui.Exit(3, exitMsg) } fp := filepath.Join(bombadillo.Options["configlocation"], ".bombadillo.ini") @@ -105,8 +105,8 @@ func loadConfig() { if err != nil { err = saveConfig() if err != nil { - cui.ExitMessage = fmt.Sprintf("Error saving config during bootup: %s", err.Error()) - cui.Exit(4) + exitMsg := fmt.Sprintf("Error saving config during bootup: %s", err.Error()) + cui.Exit(4, exitMsg) } } @@ -164,7 +164,7 @@ func handleSignals(c <-chan os.Signal) { cui.InitTerm() bombadillo.Draw() case syscall.SIGINT: - cui.Exit(130) + cui.Exit(130, "") } } } @@ -196,7 +196,7 @@ func main() { args := flag.Args() cui.InitTerm() - defer cui.Exit(0) + defer cui.Exit(0, "") initClient() // watch for signals, send them to be handled From 817affcf1497136172dec325a422032245054a85 Mon Sep 17 00:00:00 2001 From: asdf Date: Sat, 7 Dec 2019 11:14:16 +1100 Subject: [PATCH 4/4] Amended error message wording --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index abaf0eb..3b6e8ba 100644 --- a/main.go +++ b/main.go @@ -96,7 +96,7 @@ func lowerCaseOpt(opt, val string) string { func loadConfig() { err := os.MkdirAll(bombadillo.Options["configlocation"], 0755) if err != nil { - exitMsg := fmt.Sprintf("Error creating configlocation: %s", err.Error()) + exitMsg := fmt.Sprintf("Error creating 'configlocation' directory: %s", err.Error()) cui.Exit(3, exitMsg) } @@ -105,7 +105,7 @@ func loadConfig() { if err != nil { err = saveConfig() if err != nil { - exitMsg := fmt.Sprintf("Error saving config during bootup: %s", err.Error()) + exitMsg := fmt.Sprintf("Error writing config file during bootup: %s", err.Error()) cui.Exit(4, exitMsg) } }