Skip to content

Commit 849b5a8

Browse files
author
Alban Siffer
committed
turn config file not found error into simple warning
1 parent a29438c commit 849b5a8

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

config/config.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package config
66
import (
77
js "encoding/json"
88
"fmt"
9+
"io/fs"
910
"os"
1011
"path/filepath"
1112
"strings"
@@ -373,7 +374,12 @@ func LoadFromCli(c *cli.Context) error {
373374
if len(p) > 0 {
374375
// load config file arguments
375376
if err := LoadConfig(p); err != nil {
376-
return err
377+
switch err.(type) {
378+
case *ConfigNotFoundError:
379+
configLogger.Warn().Msgf(err.Error())
380+
default:
381+
return err
382+
}
377383
}
378384
}
379385
// now load the cli arguments (override the config file)
@@ -398,7 +404,12 @@ func LoadConfig(filename string) error {
398404
}
399405
// load config (it overrides default config)
400406
if err := konf.Load(file.Provider(filename), parser); err != nil {
401-
return fmt.Errorf("error loading config file %s: %v", filename, err)
407+
switch e := err.(type) {
408+
case *fs.PathError:
409+
return &ConfigNotFoundError{e.Path}
410+
default:
411+
return fmt.Errorf("error loading config file %s: %v", filename, err)
412+
}
402413
}
403414
return nil
404415
}

config/error.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package config
2+
3+
import "fmt"
4+
5+
// ConfigNotFoundError is a basic error that is triggered
6+
// when the given config file does not exist
7+
type ConfigNotFoundError struct {
8+
Path string
9+
}
10+
11+
func (err *ConfigNotFoundError) Error() string {
12+
return fmt.Sprintf("config file '%s' has not been found", err.Path)
13+
}

0 commit comments

Comments
 (0)