mirror of
https://github.com/eosswedenorg/thalos
synced 2026-11-28 08:16:46 +01:00
fix(config): propagate config setup errors
This commit is contained in:
parent
7378614f7d
commit
86e3d82c91
3 changed files with 24 additions and 2 deletions
|
|
@ -234,7 +234,7 @@ func GetConfig(flags *pflag.FlagSet) (*config.Config, error) {
|
|||
// If start-block is provided, we should set no-state-cache to true.
|
||||
if startBlock := flags.Lookup("start-block"); startBlock != nil && startBlock.Changed {
|
||||
if err := flags.Set("no-state-cache", "true"); err != nil {
|
||||
return cfg, nil
|
||||
return nil, fmt.Errorf("failed to set no-state-cache flag: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import (
|
|||
|
||||
type Builder struct {
|
||||
in io.Reader
|
||||
err error
|
||||
flags *pflag.FlagSet
|
||||
binds map[string]string
|
||||
}
|
||||
|
|
@ -69,13 +70,19 @@ func NewBuilder() *Builder {
|
|||
|
||||
// Set the config file to read
|
||||
func (b *Builder) SetConfigFile(filename string) *Builder {
|
||||
file, _ := os.Open(filename)
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
b.err = err
|
||||
return b
|
||||
}
|
||||
b.err = nil
|
||||
return b.SetSource(file)
|
||||
}
|
||||
|
||||
// Set the source to read
|
||||
func (b *Builder) SetSource(in io.Reader) *Builder {
|
||||
b.in = in
|
||||
b.err = nil
|
||||
return b
|
||||
}
|
||||
|
||||
|
|
@ -93,6 +100,10 @@ func (b *Builder) AddFlag(flag *pflag.Flag) *Builder {
|
|||
|
||||
// Build the config object from file, cli-flags
|
||||
func (b *Builder) Build() (*Config, error) {
|
||||
if b.err != nil {
|
||||
return nil, b.err
|
||||
}
|
||||
|
||||
if b.in == nil {
|
||||
return nil, errors.New("config not set")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package config
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -167,6 +168,16 @@ func TestBuilder_NilSource(t *testing.T) {
|
|||
require.EqualError(t, err, "config not set")
|
||||
}
|
||||
|
||||
func TestBuilder_SetConfigFile_FileNotFound(t *testing.T) {
|
||||
missingFile := filepath.Join(t.TempDir(), "missing.yml")
|
||||
|
||||
cfg, err := NewBuilder().SetConfigFile(missingFile).Build()
|
||||
|
||||
require.Nil(t, cfg)
|
||||
require.Error(t, err)
|
||||
require.ErrorContains(t, err, missingFile)
|
||||
}
|
||||
|
||||
func TestBuilder_Flags(t *testing.T) {
|
||||
flags := GetFlags()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue