Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Commit

Permalink
Refactor error handling in main.go
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbassi committed Dec 11, 2018
1 parent f5ad5e8 commit ce4f867
Showing 1 changed file with 41 additions and 22 deletions.
63 changes: 41 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -45,7 +46,7 @@ var (
help *w.HelpMenu
)

func cliArguments() {
func cliArguments() error {
usage := `
Usage: gotop [options]
Expand All @@ -68,11 +69,13 @@ Colorschemes:

args, err := docopt.ParseArgs(usage, os.Args[1:], version)
if err != nil {
panic(err)
return err
}

if val, _ := args["--color"]; val != nil {
handleColorscheme(val.(string))
if err := handleColorscheme(val.(string)); err != nil {
return err
}
}
averageLoad, _ = args["--averagecpu"].(bool)
percpuLoad, _ = args["--percpu"].(bool)
Expand All @@ -85,17 +88,19 @@ Colorschemes:
rateStr, _ := args["--rate"].(string)
rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil {
stderrLogger.Fatalf("error: invalid rate parameter")
return fmt.Errorf("invalid rate parameter")
}
if rate < 1 {
interval = time.Second * time.Duration(1/rate)
} else {
interval = time.Second / time.Duration(rate)
}
fahrenheit, _ = args["--fahrenheit"].(bool)

return nil
}

func handleColorscheme(cs string) {
func handleColorscheme(cs string) error {
switch cs {
case "default":
colorscheme = colorschemes.Default
Expand All @@ -106,8 +111,12 @@ func handleColorscheme(cs string) {
case "default-dark":
colorscheme = colorschemes.DefaultDark
default:
colorscheme = getCustomColorscheme(cs)
if colorscheme, err := getCustomColorscheme(cs); err != nil {
colorscheme = colorscheme
return err
}
}
return nil
}

func getConfigDir() string {
Expand All @@ -119,18 +128,18 @@ func getConfigDir() string {
}

// getCustomColorscheme tries to read a custom json colorscheme from {configDir}/{name}.json
func getCustomColorscheme(name string) colorschemes.Colorscheme {
func getCustomColorscheme(name string) (colorschemes.Colorscheme, error) {
var colorscheme colorschemes.Colorscheme
filePath := filepath.Join(configDir, name+".json")
dat, err := ioutil.ReadFile(filePath)
if err != nil {
stderrLogger.Fatalf("error: colorscheme not recognized")
return colorscheme, fmt.Errorf("colorscheme file not found")
}
var colorscheme colorschemes.Colorscheme
err = json.Unmarshal(dat, &colorscheme)
if err != nil {
stderrLogger.Fatalf("error: could not parse colorscheme")
return colorscheme, fmt.Errorf("could not parse colorscheme file")
}
return colorscheme
return colorscheme, nil
}

func setupGrid() {
Expand Down Expand Up @@ -340,40 +349,50 @@ func eventLoop() {
}
}

func logging() *os.File {
func logging() (*os.File, error) {
// make the config directory
err := os.MkdirAll(configDir, 0755)
if err != nil {
stderrLogger.Fatalf("failed to make the configuration directory: %v", err)
if err := os.MkdirAll(configDir, 0755); err != nil {
return nil, fmt.Errorf("failed to make the configuration directory: %v", err)
}
// open the log file
lf, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
if err != nil {
stderrLogger.Fatalf("failed to open log file: %v", err)
return nil, fmt.Errorf("failed to open log file: %v", err)
}

// log time, filename, and line number
log.SetFlags(log.Ltime | log.Lshortfile)
// log to file
log.SetOutput(lf)

return lf
return lf, nil
}

func main() {
lf := logging()
lf, err := logging()
if err != nil {
stderrLogger.Fatalf("failed to setup logging: %v", err)
}
defer lf.Close()
cliArguments()
syscall.Dup2(int(lf.Fd()), 2) // redirect stderr to logfile
termuiColors() // need to do this before initializing widgets so that they can inherit the colors

if err := cliArguments(); err != nil {
stderrLogger.Fatalf("failed to parse cli args: %v", err)
}

termuiColors() // need to do this before initializing widgets so that they can inherit the colors
initWidgets()
widgetColors()
help = w.NewHelpMenu()

if err := ui.Init(); err != nil {
panic(err)
stderrLogger.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()

syscall.Dup2(int(lf.Fd()), 2) // redirect stderr to logfile

setupGrid()
ui.Render(ui.Body)

eventLoop()
}

0 comments on commit ce4f867

Please sign in to comment.