diff --git a/client.go b/client.go index 1704ad6..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 { - fmt.Println("Fatal error: Unable to retrieve terminal size") - os.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 { - fmt.Println("Fatal error: Unable to retrieve terminal size") - os.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() + 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() + 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..b6ca90b 100644 --- a/cui/cui.go +++ b/cui/cui.go @@ -44,9 +44,12 @@ func moveCursorToward(dir string, amount int) { } // Exit performs cleanup operations before exiting the application -func Exit() { +func Exit(exitCode int, msg string) { CleanupTerm() - os.Exit(0) + if msg != "" { + fmt.Print(msg, "\n") + } + os.Exit(exitCode) } // InitTerm sets the terminal modes appropriate for Bombadillo diff --git a/main.go b/main.go index bc986c0..3b6e8ba 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()) + exitMsg := fmt.Sprintf("Error creating 'configlocation' directory: %s", err.Error()) + cui.Exit(3, exitMsg) } 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 + exitMsg := fmt.Sprintf("Error writing config file during bootup: %s", err.Error()) + cui.Exit(4, exitMsg) } } @@ -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. @@ -165,7 +164,7 @@ func handleSignals(c <-chan os.Signal) { cui.InitTerm() bombadillo.Draw() case syscall.SIGINT: - cui.Exit() + cui.Exit(130, "") } } } @@ -197,12 +196,8 @@ func main() { args := flag.Args() cui.InitTerm() - defer cui.Exit() - err := initClient() - if err != nil { - // if we can't initialize we should bail out - panic(err) - } + defer cui.Exit(0, "") + initClient() // watch for signals, send them to be handled c := make(chan os.Signal, 1)