cmd/tailscale/cli: treat nil and non-nil zero length slices as equiv prefs

Updates #1725

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1729/head
Brad Fitzpatrick 4 years ago
parent eb6115e295
commit 4948ff6ecb

@ -8,6 +8,7 @@ import (
"flag" "flag"
"testing" "testing"
"inet.af/netaddr"
"tailscale.com/ipn" "tailscale.com/ipn"
) )
@ -102,6 +103,19 @@ func TestCheckForAccidentalSettingReverts(t *testing.T) {
}, },
want: "", want: "",
}, },
{
name: "empty_slice_equals_nil_slice",
flagSet: f("hostname"),
curPrefs: &ipn.Prefs{
AdvertiseRoutes: []netaddr.IPPrefix{},
},
mp: &ipn.MaskedPrefs{
Prefs: ipn.Prefs{
AdvertiseRoutes: nil,
},
},
want: "",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

@ -501,8 +501,16 @@ func checkForAccidentalSettingReverts(flagSet map[string]bool, curPrefs *ipn.Pre
continue continue
} }
// Get explicit value and implicit value // Get explicit value and implicit value
evi, ivi := ev.Field(i).Interface(), iv.Field(i).Interface() ex, im := ev.Field(i), iv.Field(i)
if reflect.DeepEqual(evi, ivi) { switch ex.Kind() {
case reflect.String, reflect.Slice:
if ex.Kind() == reflect.Slice && ex.Len() == 0 && im.Len() == 0 {
// Treat nil and non-nil empty slices as equivalent.
continue
}
}
exi, imi := ex.Interface(), im.Interface()
if reflect.DeepEqual(exi, imi) {
continue continue
} }
switch flagName { switch flagName {
@ -515,7 +523,7 @@ func checkForAccidentalSettingReverts(flagSet map[string]bool, curPrefs *ipn.Pre
} }
default: default:
errs = append(errs, fmt.Errorf("'tailscale up' without --reset requires all preferences with changing values to be explicitly mentioned; --%s is not specified but its default value of %v differs from current value %v", errs = append(errs, fmt.Errorf("'tailscale up' without --reset requires all preferences with changing values to be explicitly mentioned; --%s is not specified but its default value of %v differs from current value %v",
flagName, fmtSettingVal(ivi), fmtSettingVal(evi))) flagName, fmtSettingVal(imi), fmtSettingVal(exi)))
} }
} }
return multierror.New(errs) return multierror.New(errs)

Loading…
Cancel
Save