summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHubert Hirtz <hubert@hirtz.pm>2021-04-30 10:17:16 +0200
committerHubert Hirtz <hubert@hirtz.pm>2021-04-30 10:17:16 +0200
commite6df23b3f95433141ecacd0219230e7fa1034ef9 (patch)
tree3ab68eb42c29df4ab7c23093a514e5c0dcaf7d7c
parentUse path.Join to construct config file path (diff)
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.
-rw-r--r--cmd/senpai/main.go9
-rw-r--r--config.go10
2 files changed, 13 insertions, 6 deletions
diff --git a/cmd/senpai/main.go b/cmd/senpai/main.go
index d428ccf..c2992b0 100644
--- a/cmd/senpai/main.go
+++ b/cmd/senpai/main.go
@@ -2,7 +2,7 @@ package main
import (
"flag"
- "log"
+ "fmt"
"math/rand"
"os"
"path"
@@ -28,21 +28,22 @@ func main() {
if configPath == "" {
configDir, err := os.UserConfigDir()
if err != nil {
- log.Panicln(err)
+ panic(err)
}
configPath = path.Join(configDir, "senpai", "senpai.yaml")
}
cfg, err := senpai.LoadConfigFile(configPath)
if err != nil {
- log.Panicln(err)
+ fmt.Printf("failed to load the required configuraiton file at %q: %s\n", configPath, err)
+ os.Exit(1)
}
cfg.Debug = cfg.Debug || debug
app, err := senpai.NewApp(cfg)
if err != nil {
- log.Panicln(err)
+ panic(err)
}
defer app.Close()
diff --git a/config.go b/config.go
index fcd4cc3..1d8d79a 100644
--- a/config.go
+++ b/config.go
@@ -2,6 +2,7 @@ package senpai
import (
"errors"
+ "fmt"
"io/ioutil"
"gopkg.in/yaml.v2"
@@ -28,6 +29,9 @@ type Config struct {
func ParseConfig(buf []byte) (cfg Config, err error) {
err = yaml.Unmarshal(buf, &cfg)
+ if err != nil {
+ return cfg, err
+ }
if cfg.Addr == "" {
return cfg, errors.New("addr is required")
}
@@ -54,10 +58,12 @@ func LoadConfigFile(filename string) (cfg Config, err error) {
buf, err = ioutil.ReadFile(filename)
if err != nil {
- return
+ return cfg, fmt.Errorf("failed to read the file: %s", err)
}
cfg, err = ParseConfig(buf)
-
+ if err != nil {
+ return cfg, fmt.Errorf("invalid content found in the file: %s", err)
+ }
return
}