Skip to content

Commit

Permalink
Add basic node config
Browse files Browse the repository at this point in the history
  • Loading branch information
mpetrun5 committed Oct 19, 2023
1 parent 9312701 commit 1594a8a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
package config

import "github.com/kelseyhightower/envconfig"

const PREFIX = "SPECTRE"

type Config struct {
Observability
}

type Observability struct {
LogLevel string `default:"debug" split_words:"true"`
LogFile string `default:"out.log" split_words:"true"`
}

// LoadConfig loads config from the environment and validates the fields
func LoadConfig() (*Config, error) {
var c Config
err := envconfig.Process(PREFIX, &c)
if err != nil {
return nil, err
}

return &c, nil
}
44 changes: 44 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package config_test

import (
"os"
"testing"

"github.com/stretchr/testify/suite"
"github.com/sygmaprotocol/spectre-node/config"
)

type ConfigTestSuite struct {
suite.Suite
}

func TestRunConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}

func (s *ConfigTestSuite) Test_LoadConfig_DefaultValues() {
c, err := config.LoadConfig()

s.Nil(err)
s.Equal(c, &config.Config{
Observability: config.Observability{
LogLevel: "debug",
LogFile: "out.log",
},
})
}

func (s *ConfigTestSuite) Test_LoadEVMConfig_SuccessfulLoad() {
os.Setenv("SPECTRE_LOG_LEVEL", "info")
os.Setenv("SPECTRE_LOG_FILE", "out2.log")

c, err := config.LoadConfig()

s.Nil(err)
s.Equal(c, &config.Config{
Observability: config.Observability{
LogLevel: "info",
LogFile: "out2.log",
},
})
}

0 comments on commit 1594a8a

Please sign in to comment.