From 53c94b927f4ae987624285810f0ca8ebea2b64ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Miri=C4=87?= Date: Thu, 10 Sep 2020 16:02:40 +0200 Subject: [PATCH] Add DNSConfig.String() method Resolves https://github.com/loadimpact/k6/pull/1612#discussion_r486153764 --- cmd/options.go | 2 +- lib/options.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/cmd/options.go b/cmd/options.go index 45426308d2c..837820e8d5a 100644 --- a/cmd/options.go +++ b/cmd/options.go @@ -91,7 +91,7 @@ func optionFlagSet() *pflag.FlagSet { flags.StringSlice("tag", nil, "add a `tag` to be applied to all samples, as `[name]=[value]`") flags.String("console-output", "", "redirects the console logging to the provided output file") flags.Bool("discard-response-bodies", false, "Read but don't process or save HTTP response bodies") - flags.String("dns", lib.DefaultDNSConfigText, "DNS configuration. Possible ttl values are: 'inf' "+ + flags.String("dns", lib.DefaultDNSConfig().String(), "DNS configuration. Possible ttl values are: 'inf' "+ "for a persistent cache, '0' to disable the cache,\nor a positive duration, e.g. '1s', '1m', etc. "+ "Milliseconds are assumed if no unit is provided.\n"+ "Possible values for the strategy to use to select a single IP are: 'first', 'random' or 'round-robin'.\n") diff --git a/lib/options.go b/lib/options.go index 9d0caed04e4..42f64993eea 100644 --- a/lib/options.go +++ b/lib/options.go @@ -27,6 +27,7 @@ import ( "net" "reflect" "strconv" + "strings" "github.com/kubernetes/helm/pkg/strvals" "github.com/pkg/errors" @@ -67,8 +68,6 @@ const ( DNSRandom ) -const DefaultDNSConfigText = "ttl=inf,strategy=first" - func DefaultDNSConfig() DNSConfig { return DNSConfig{ TTL: null.NewString("inf", false), @@ -76,6 +75,13 @@ func DefaultDNSConfig() DNSConfig { } } +func (c DNSConfig) String() string { + out := make([]string, 0, 2) + out = append(out, fmt.Sprintf("ttl=%s", c.TTL.String)) + out = append(out, fmt.Sprintf("strategy=%s", c.Strategy.String())) + return strings.Join(out, ",") +} + func (c DNSConfig) MarshalJSON() ([]byte, error) { var strat string if c.Strategy.IsADNSStrategy() { @@ -99,7 +105,7 @@ func (c *DNSConfig) UnmarshalJSON(data []byte) error { } func (c *DNSConfig) UnmarshalText(text []byte) error { - if string(text) == DefaultDNSConfigText { + if string(text) == DefaultDNSConfig().String() { *c = DefaultDNSConfig() return nil }