commit e6df23b
Hubert Hirtz
·
2021-04-30 08:17:16 +0000 UTC
parent 43bc9ca
Better error reporting about configuration file - Better errors in config.go - Do not print useless timestamps in cmd/senpai/main.go - Let os.UserConfigDir() and senpai.NewApp() call panic on error since they both should not fail.
2 files changed,
+13,
-6
+5,
-4
1@@ -2,7 +2,7 @@ package main
2
3 import (
4 "flag"
5- "log"
6+ "fmt"
7 "math/rand"
8 "os"
9 "path"
10@@ -28,21 +28,22 @@ func main() {
11 if configPath == "" {
12 configDir, err := os.UserConfigDir()
13 if err != nil {
14- log.Panicln(err)
15+ panic(err)
16 }
17 configPath = path.Join(configDir, "senpai", "senpai.yaml")
18 }
19
20 cfg, err := senpai.LoadConfigFile(configPath)
21 if err != nil {
22- log.Panicln(err)
23+ fmt.Printf("failed to load the required configuraiton file at %q: %s\n", configPath, err)
24+ os.Exit(1)
25 }
26
27 cfg.Debug = cfg.Debug || debug
28
29 app, err := senpai.NewApp(cfg)
30 if err != nil {
31- log.Panicln(err)
32+ panic(err)
33 }
34 defer app.Close()
35
+8,
-2
1@@ -2,6 +2,7 @@ package senpai
2
3 import (
4 "errors"
5+ "fmt"
6 "io/ioutil"
7
8 "gopkg.in/yaml.v2"
9@@ -28,6 +29,9 @@ type Config struct {
10
11 func ParseConfig(buf []byte) (cfg Config, err error) {
12 err = yaml.Unmarshal(buf, &cfg)
13+ if err != nil {
14+ return cfg, err
15+ }
16 if cfg.Addr == "" {
17 return cfg, errors.New("addr is required")
18 }
19@@ -54,10 +58,12 @@ func LoadConfigFile(filename string) (cfg Config, err error) {
20
21 buf, err = ioutil.ReadFile(filename)
22 if err != nil {
23- return
24+ return cfg, fmt.Errorf("failed to read the file: %s", err)
25 }
26
27 cfg, err = ParseConfig(buf)
28-
29+ if err != nil {
30+ return cfg, fmt.Errorf("invalid content found in the file: %s", err)
31+ }
32 return
33 }