ipn: fix formatting of ExitNodeIP in MaskedPrefs

%#v on a netaddr.IP showed the netaddr.IP innards.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
(cherry picked from commit 751c42c097)
This commit is contained in:
Brad Fitzpatrick 2021-10-13 14:48:47 -07:00
parent d76672b5e5
commit 6421ee22f6
2 changed files with 36 additions and 6 deletions

View File

@ -234,6 +234,20 @@ func (m *MaskedPrefs) Pretty() string {
mt := mv.Type() mt := mv.Type()
mpv := reflect.ValueOf(&m.Prefs).Elem() mpv := reflect.ValueOf(&m.Prefs).Elem()
first := true 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++ { for i := 1; i < mt.NumField(); i++ {
name := mt.Field(i).Name name := mt.Field(i).Name
if mv.Field(i).Bool() { if mv.Field(i).Bool() {
@ -241,9 +255,10 @@ func (m *MaskedPrefs) Pretty() string {
sb.WriteString(" ") sb.WriteString(" ")
} }
first = false first = false
fmt.Fprintf(&sb, "%s=%#v", f := mpv.Field(i - 1)
fmt.Fprintf(&sb, format(f),
strings.TrimSuffix(name, "Set"), strings.TrimSuffix(name, "Set"),
mpv.Field(i-1).Interface()) f.Interface())
} }
} }
sb.WriteString("}") sb.WriteString("}")

View File

@ -615,12 +615,27 @@ func TestMaskedPrefsPretty(t *testing.T) {
OperatorUser: "galaxybrain", OperatorUser: "galaxybrain",
AllowSingleHosts: true, AllowSingleHosts: true,
RouteAll: false, RouteAll: false,
ExitNodeID: "foo",
AdvertiseTags: []string{"tag:foo", "tag:bar"},
NetfilterMode: preftype.NetfilterNoDivert,
}, },
RouteAllSet: true, RouteAllSet: true,
HostnameSet: true, HostnameSet: true,
OperatorUserSet: 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 { for i, tt := range tests {