Skip to content

Commit

Permalink
Merge pull request #107 from hikhvar/make-config-serializable
Browse files Browse the repository at this point in the history
Make configuration serializable
  • Loading branch information
czerwonk committed Mar 26, 2024
2 parents 1f1d542 + b7c708a commit 64b4119
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
10 changes: 10 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func FromYAML(r io.Reader) (*Config, error) {
return c, nil
}

// ToYAML encodes the given configuration to the writer as YAML
func ToYAML(w io.Writer, cfg *Config) error {
err := yaml.NewEncoder(w).Encode(cfg)
if err != nil {
return fmt.Errorf("failed to encode YAML: %w", err)
}

return nil
}

func (cfg *Config) TargetConfigByAddr(addr string) TargetConfig {
for _, t := range cfg.Targets {
if t.Addr == addr {
Expand Down
33 changes: 33 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package config

import (
"bytes"
"os"
"reflect"
"testing"
Expand Down Expand Up @@ -65,3 +66,35 @@ func TestParseConfig(t *testing.T) {
t.Errorf("expected options.disable-ipv6 to be %v, got %v", expected, c.Options.DisableIPv6)
}
}

func TestRoundtrip(t *testing.T) {
f, err := os.Open("testdata/config_test.yml")
if err != nil {
t.Error("failed to open file", err)
t.FailNow()
}

c, err := FromYAML(f)
if err != nil {
t.Error("failed to read file", err)
t.FailNow()
}

buf := bytes.NewBuffer(nil)
err = ToYAML(buf, c)
if err != nil {
t.Error("failed to encode config", err)
t.FailNow()
}

after, err := FromYAML(buf)
if err != nil {
t.Error("failed to read config again", err)
t.FailNow()
}

if !reflect.DeepEqual(c, after) {
t.Error("config after Decode(Encode(cfg)) didn't match")
t.FailNow()
}
}
4 changes: 4 additions & 0 deletions config/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (d *duration) UnmarshalYAML(unmashal func(interface{}) error) error {
return nil
}

func (d duration) MarshalYAML() (interface{}, error) {
return d.Duration().String(), nil
}

// Duration is a convenience getter.
func (d duration) Duration() time.Duration {
return time.Duration(d)
Expand Down
9 changes: 9 additions & 0 deletions config/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ func (d *TargetConfig) UnmarshalYAML(unmashal func(interface{}) error) error {

return nil
}

func (d TargetConfig) MarshalYAML() (interface{}, error) {
if d.Labels == nil {
return d.Addr, nil
}
ret := make(map[string]map[string]string)
ret[d.Addr] = d.Labels
return ret, nil
}
2 changes: 1 addition & 1 deletion target.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (t *target) addOrUpdateMonitor(monitor *mon.Monitor, opts targetOpts) error

addrs, err := t.resolver.LookupIPAddr(context.Background(), t.host)
if err != nil {
return fmt.Errorf("error resolving target: %w", err)
return fmt.Errorf("error resolving target '%s': %w", t.host, err)
}

var sanitizedAddrs []net.IPAddr
Expand Down

0 comments on commit 64b4119

Please sign in to comment.