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>
pull/3075/head
Brad Fitzpatrick 3 years ago
parent 9ab8492694
commit 751c42c097

@ -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("}")

@ -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 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{RouteAll=false Hostname="bar" OperatorUser="galaxybrain"}`,
want: `MaskedPrefs{ExitNodeIP=100.102.104.105}`,
},
}
for i, tt := range tests {

Loading…
Cancel
Save