From 6421ee22f62abf968f8ef57d69258d6e89ac4612 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 13 Oct 2021 14:48:47 -0700 Subject: [PATCH] ipn: fix formatting of ExitNodeIP in MaskedPrefs %#v on a netaddr.IP showed the netaddr.IP innards. Signed-off-by: Brad Fitzpatrick (cherry picked from commit 751c42c0972967e20d5a04953a0ecf390e5cb292) --- ipn/prefs.go | 19 +++++++++++++++++-- ipn/prefs_test.go | 23 +++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/ipn/prefs.go b/ipn/prefs.go index fe93c0efd..0d341ae1a 100644 --- a/ipn/prefs.go +++ b/ipn/prefs.go @@ -234,6 +234,20 @@ func (m *MaskedPrefs) Pretty() string { mt := mv.Type() mpv := reflect.ValueOf(&m.Prefs).Elem() first := true + + format := func(v reflect.Value) string { + switch v.Type().Kind() { + case reflect.String: + return "%s=%q" + case reflect.Slice: + // []string + if v.Type().Elem().Kind() == reflect.String { + return "%s=%q" + } + } + return "%s=%v" + } + for i := 1; i < mt.NumField(); i++ { name := mt.Field(i).Name if mv.Field(i).Bool() { @@ -241,9 +255,10 @@ func (m *MaskedPrefs) Pretty() string { sb.WriteString(" ") } first = false - fmt.Fprintf(&sb, "%s=%#v", + f := mpv.Field(i - 1) + fmt.Fprintf(&sb, format(f), strings.TrimSuffix(name, "Set"), - mpv.Field(i-1).Interface()) + f.Interface()) } } sb.WriteString("}") diff --git a/ipn/prefs_test.go b/ipn/prefs_test.go index 1f24fc7a6..41b16b60f 100644 --- a/ipn/prefs_test.go +++ b/ipn/prefs_test.go @@ -615,12 +615,27 @@ func TestMaskedPrefsPretty(t *testing.T) { OperatorUser: "galaxybrain", AllowSingleHosts: true, RouteAll: false, + ExitNodeID: "foo", + AdvertiseTags: []string{"tag:foo", "tag:bar"}, + NetfilterMode: preftype.NetfilterNoDivert, }, - RouteAllSet: true, - HostnameSet: true, - OperatorUserSet: true, + RouteAllSet: true, + HostnameSet: true, + OperatorUserSet: true, + ExitNodeIDSet: true, + AdvertiseTagsSet: true, + NetfilterModeSet: true, }, - want: `MaskedPrefs{RouteAll=false Hostname="bar" OperatorUser="galaxybrain"}`, + want: `MaskedPrefs{RouteAll=false ExitNodeID="foo" AdvertiseTags=["tag:foo" "tag:bar"] Hostname="bar" NetfilterMode=nodivert OperatorUser="galaxybrain"}`, + }, + { + m: &MaskedPrefs{ + Prefs: Prefs{ + ExitNodeIP: netaddr.IPv4(100, 102, 104, 105), + }, + ExitNodeIPSet: true, + }, + want: `MaskedPrefs{ExitNodeIP=100.102.104.105}`, }, } for i, tt := range tests {