Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmasek committed Dec 29, 2024
1 parent 0280c80 commit 8db9eee
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
File renamed without changes.
15 changes: 14 additions & 1 deletion monitor/config.go → config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor
package config

import (
_ "embed"
Expand All @@ -11,8 +11,13 @@ import (
"strings"

"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

type Config struct {
settings map[string]interface{}
}

//go:embed config.default.yaml
var DEFAULT_CONFIG []byte

Expand Down Expand Up @@ -101,6 +106,14 @@ func setupConfig(config *viper.Viper) (*viper.Viper, error) {
if err != nil {
return nil, err
}
data, err := os.ReadFile(config.ConfigFileUsed())
if err != nil {
return nil, err
}
config2 := &Config{settings: make(map[string]interface{})}
err = yaml.Unmarshal(data, config2.settings)
log.Println(">>>>", config2, "<<<<")
fmt.Printf("Config: %+v\n", config2.settings)

// TODO: refactor
// why does not Go have sets? :/
Expand Down
2 changes: 1 addition & 1 deletion monitor/config_test.go → config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor
package config

import (
"os"
Expand Down
2 changes: 1 addition & 1 deletion monitor/service_config.go → config/service_config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor
package config

import (
"fmt"
Expand Down
11 changes: 10 additions & 1 deletion monitor/service_config_test.go → config/service_config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package monitor
package config

import (
"path/filepath"
Expand Down Expand Up @@ -31,4 +31,13 @@ func TestLoadExampleConfig(t *testing.T) {
assert.Nil(t, services["example-basic-web"].BodyContent)

assert.Equal(t, services["example-temp-disable"].Enabled, false)

// Service without config should still be included.
// Viper does not handle this well:
// - https://github.com/spf13/viper/issues/406
// Possible alternatives:
// - https://github.com/knadh/koanf
// - roll own config
require.Contains(t, services, "beacon-periodic-checker")
assert.Equal(t, services["beacon-periodic-checker"].Enabled, true)
}

0 comments on commit 8db9eee

Please sign in to comment.