diff --git a/cmd/thalos/server.go b/cmd/thalos/server.go index 79a404f..fb608cd 100644 --- a/cmd/thalos/server.go +++ b/cmd/thalos/server.go @@ -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) } } diff --git a/internal/config/builder.go b/internal/config/builder.go index baa1364..abfaa82 100644 --- a/internal/config/builder.go +++ b/internal/config/builder.go @@ -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") } diff --git a/internal/config/builder_test.go b/internal/config/builder_test.go index 6fbe3b7..15c1d13 100644 --- a/internal/config/builder_test.go +++ b/internal/config/builder_test.go @@ -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()