Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make configuration serializable #107

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading