all: use cmpx.Or where it made sense

I left a few out where writing it explicitly was better
for various reasons.

Updates #8296

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/8309/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent 699996ad6c
commit eefee6f149

@ -125,6 +125,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
W tailscale.com/util/clientmetric from tailscale.com/net/tshttpproxy W tailscale.com/util/clientmetric from tailscale.com/net/tshttpproxy
tailscale.com/util/cloudenv from tailscale.com/hostinfo+ tailscale.com/util/cloudenv from tailscale.com/hostinfo+
W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy
tailscale.com/util/cmpx from tailscale.com/cmd/derper+
L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics
tailscale.com/util/dnsname from tailscale.com/hostinfo+ tailscale.com/util/dnsname from tailscale.com/hostinfo+
tailscale.com/util/httpm from tailscale.com/client/tailscale tailscale.com/util/httpm from tailscale.com/client/tailscale

@ -33,6 +33,7 @@ import (
"tailscale.com/net/stun" "tailscale.com/net/stun"
"tailscale.com/tsweb" "tailscale.com/tsweb"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/util/cmpx"
) )
var ( var (
@ -436,11 +437,7 @@ func defaultMeshPSKFile() string {
} }
func rateLimitedListenAndServeTLS(srv *http.Server) error { func rateLimitedListenAndServeTLS(srv *http.Server) error {
addr := srv.Addr ln, err := net.Listen("tcp", cmpx.Or(srv.Addr, ":https"))
if addr == "" {
addr = ":https"
}
ln, err := net.Listen("tcp", addr)
if err != nil { if err != nil {
return err return err
} }

@ -16,6 +16,7 @@ import (
"golang.org/x/oauth2/clientcredentials" "golang.org/x/oauth2/clientcredentials"
"tailscale.com/client/tailscale" "tailscale.com/client/tailscale"
"tailscale.com/util/cmpx"
) )
func main() { func main() {
@ -39,10 +40,7 @@ func main() {
log.Fatal("at least one tag must be specified") log.Fatal("at least one tag must be specified")
} }
baseURL := os.Getenv("TS_BASE_URL") baseURL := cmpx.Or(os.Getenv("TS_BASE_URL"), "https://api.tailscale.com")
if baseURL == "" {
baseURL = "https://api.tailscale.com"
}
credentials := clientcredentials.Config{ credentials := clientcredentials.Config{
ClientID: clientID, ClientID: clientID,

@ -22,6 +22,7 @@ import (
"tailscale.com/tstest" "tailscale.com/tstest"
"tailscale.com/types/persist" "tailscale.com/types/persist"
"tailscale.com/types/preftype" "tailscale.com/types/preftype"
"tailscale.com/util/cmpx"
"tailscale.com/version/distro" "tailscale.com/version/distro"
) )
@ -719,10 +720,7 @@ func TestPrefsFromUpArgs(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var warnBuf tstest.MemLogger var warnBuf tstest.MemLogger
goos := tt.goos goos := cmpx.Or(tt.goos, "linux")
if goos == "" {
goos = "linux"
}
st := tt.st st := tt.st
if st == nil { if st == nil {
st = new(ipnstate.Status) st = new(ipnstate.Status)

@ -29,6 +29,7 @@ import (
"tailscale.com/ipn" "tailscale.com/ipn"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/util/cmpx"
"tailscale.com/util/groupmember" "tailscale.com/util/groupmember"
"tailscale.com/version/distro" "tailscale.com/version/distro"
) )
@ -155,10 +156,7 @@ func runWeb(ctx context.Context, args []string) error {
// urlOfListenAddr parses a given listen address into a formatted URL // urlOfListenAddr parses a given listen address into a formatted URL
func urlOfListenAddr(addr string) string { func urlOfListenAddr(addr string) string {
host, port, _ := net.SplitHostPort(addr) host, port, _ := net.SplitHostPort(addr)
if host == "" { return fmt.Sprintf("http://%s", net.JoinHostPort(cmpx.Or(host, "127.0.0.1"), port))
host = "127.0.0.1"
}
return fmt.Sprintf("http://%s", net.JoinHostPort(host, port))
} }
// authorize returns the name of the user accessing the web UI after verifying // authorize returns the name of the user accessing the web UI after verifying

@ -114,6 +114,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
tailscale.com/util/clientmetric from tailscale.com/net/netcheck+ tailscale.com/util/clientmetric from tailscale.com/net/netcheck+
tailscale.com/util/cloudenv from tailscale.com/net/dnscache+ tailscale.com/util/cloudenv from tailscale.com/net/dnscache+
W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy W tailscale.com/util/cmpver from tailscale.com/net/tshttpproxy
tailscale.com/util/cmpx from tailscale.com/cmd/tailscale/cli+
L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics
tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+ tailscale.com/util/dnsname from tailscale.com/cmd/tailscale/cli+
tailscale.com/util/groupmember from tailscale.com/cmd/tailscale/cli tailscale.com/util/groupmember from tailscale.com/cmd/tailscale/cli

@ -308,6 +308,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
tailscale.com/util/clientmetric from tailscale.com/control/controlclient+ tailscale.com/util/clientmetric from tailscale.com/control/controlclient+
tailscale.com/util/cloudenv from tailscale.com/net/dns/resolver+ tailscale.com/util/cloudenv from tailscale.com/net/dns/resolver+
LW tailscale.com/util/cmpver from tailscale.com/net/dns+ LW tailscale.com/util/cmpver from tailscale.com/net/dns+
tailscale.com/util/cmpx from tailscale.com/derp/derphttp+
💣 tailscale.com/util/deephash from tailscale.com/ipn/ipnlocal+ 💣 tailscale.com/util/deephash from tailscale.com/ipn/ipnlocal+
L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics+ L 💣 tailscale.com/util/dirwalk from tailscale.com/metrics+
tailscale.com/util/dnsname from tailscale.com/hostinfo+ tailscale.com/util/dnsname from tailscale.com/hostinfo+

@ -40,6 +40,7 @@ import (
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/types/key" "tailscale.com/types/key"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/cmpx"
) )
// Client is a DERP-over-HTTP client. // Client is a DERP-over-HTTP client.
@ -654,10 +655,7 @@ func (c *Client) dialNode(ctx context.Context, n *tailcfg.DERPNode) (net.Conn, e
// Start v4 dial // Start v4 dial
} }
} }
dst := dstPrimary dst := cmpx.Or(dstPrimary, n.HostName)
if dst == "" {
dst = n.HostName
}
port := "443" port := "443"
if n.DERPPort != 0 { if n.DERPPort != 0 {
port = fmt.Sprint(n.DERPPort) port = fmt.Sprint(n.DERPPort)

@ -16,6 +16,7 @@ import (
"tailscale.com/types/dnstype" "tailscale.com/types/dnstype"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
"tailscale.com/util/cloudenv" "tailscale.com/util/cloudenv"
"tailscale.com/util/cmpx"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
) )
@ -308,10 +309,7 @@ func TestDNSConfigForNetmap(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
verOS := tt.os verOS := cmpx.Or(tt.os, "linux")
if verOS == "" {
verOS = "linux"
}
var log tstest.MemLogger var log tstest.MemLogger
got := dnsConfigForNetmap(tt.nm, tt.prefs.View(), log.Logf, verOS) got := dnsConfigForNetmap(tt.nm, tt.prefs.View(), log.Logf, verOS)
if !reflect.DeepEqual(got, tt.want) { if !reflect.DeepEqual(got, tt.want) {

@ -71,6 +71,7 @@ import (
"tailscale.com/types/preftype" "tailscale.com/types/preftype"
"tailscale.com/types/ptr" "tailscale.com/types/ptr"
"tailscale.com/types/views" "tailscale.com/types/views"
"tailscale.com/util/cmpx"
"tailscale.com/util/deephash" "tailscale.com/util/deephash"
"tailscale.com/util/dnsname" "tailscale.com/util/dnsname"
"tailscale.com/util/mak" "tailscale.com/util/mak"
@ -3932,10 +3933,7 @@ func (b *LocalBackend) setNetMapLocked(nm *netmap.NetworkMap) {
b.dialer.SetNetMap(nm) b.dialer.SetNetMap(nm)
var login string var login string
if nm != nil { if nm != nil {
login = nm.UserProfiles[nm.User].LoginName login = cmpx.Or(nm.UserProfiles[nm.User].LoginName, "<missing-profile>")
if login == "" {
login = "<missing-profile>"
}
} }
b.netMap = nm b.netMap = nm
if login != b.activeLogin { if login != b.activeLogin {

@ -16,6 +16,7 @@ import (
"testing" "testing"
"tailscale.com/ipn" "tailscale.com/ipn"
"tailscale.com/util/cmpx"
) )
func TestExpandProxyArg(t *testing.T) { func TestExpandProxyArg(t *testing.T) {
@ -140,10 +141,7 @@ func TestGetServeHandler(t *testing.T) {
}, },
TLS: &tls.ConnectionState{ServerName: serverName}, TLS: &tls.ConnectionState{ServerName: serverName},
} }
port := tt.port port := cmpx.Or(tt.port, 443)
if port == 0 {
port = 443
}
req = req.WithContext(context.WithValue(req.Context(), serveHTTPContextKey{}, &serveHTTPContext{ req = req.WithContext(context.WithValue(req.Context(), serveHTTPContextKey{}, &serveHTTPContext{
DestPort: port, DestPort: port,
})) }))

@ -13,6 +13,7 @@ import (
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
"golang.org/x/net/dns/dnsmessage" "golang.org/x/net/dns/dnsmessage"
"tailscale.com/util/cmpx"
) )
// MessageCache is a cache that works at the DNS message layer, // MessageCache is a cache that works at the DNS message layer,
@ -59,10 +60,7 @@ func (c *MessageCache) Flush() {
// pruneLocked prunes down the cache size to the configured (or // pruneLocked prunes down the cache size to the configured (or
// default) max size. // default) max size.
func (c *MessageCache) pruneLocked() { func (c *MessageCache) pruneLocked() {
max := c.cacheSizeSet max := cmpx.Or(c.cacheSizeSet, 500)
if max == 0 {
max = 500
}
for c.cache.Len() > max { for c.cache.Len() > max {
c.cache.RemoveOldest() c.cache.RemoveOldest()
} }

@ -42,6 +42,7 @@ import (
"tailscale.com/types/opt" "tailscale.com/types/opt"
"tailscale.com/types/ptr" "tailscale.com/types/ptr"
"tailscale.com/util/clientmetric" "tailscale.com/util/clientmetric"
"tailscale.com/util/cmpx"
"tailscale.com/util/mak" "tailscale.com/util/mak"
) )
@ -450,10 +451,9 @@ func makeProbePlan(dm *tailcfg.DERPMap, ifState *interfaces.State, last *Report)
do6 = false do6 = false
} }
n := reg.Nodes[try%len(reg.Nodes)] n := reg.Nodes[try%len(reg.Nodes)]
prevLatency := last.RegionLatency[reg.RegionID] * 120 / 100 prevLatency := cmpx.Or(
if prevLatency == 0 { last.RegionLatency[reg.RegionID]*120/100,
prevLatency = defaultActiveRetransmitTime defaultActiveRetransmitTime)
}
delay := time.Duration(try) * prevLatency delay := time.Duration(try) * prevLatency
if try > 1 { if try > 1 {
delay += time.Duration(try) * 50 * time.Millisecond delay += time.Duration(try) * 50 * time.Millisecond
@ -1589,10 +1589,7 @@ func (rs *reportState) runProbe(ctx context.Context, dm *tailcfg.DERPMap, probe
// proto is 4 or 6 // proto is 4 or 6
// If it returns nil, the node is skipped. // If it returns nil, the node is skipped.
func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeProto) (ap netip.AddrPort) { func (c *Client) nodeAddr(ctx context.Context, n *tailcfg.DERPNode, proto probeProto) (ap netip.AddrPort) {
port := n.STUNPort port := cmpx.Or(n.STUNPort, 3478)
if port == 0 {
port = 3478
}
if port < 0 || port > 1<<16-1 { if port < 0 || port > 1<<16-1 {
return return
} }

@ -27,6 +27,7 @@ import (
"tailscale.com/net/tsaddr" "tailscale.com/net/tsaddr"
"tailscale.com/tsweb/varz" "tailscale.com/tsweb/varz"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/cmpx"
"tailscale.com/util/vizerror" "tailscale.com/util/vizerror"
) )
@ -144,10 +145,7 @@ func (h Port80Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Redirect authorized user to the debug handler. // Redirect authorized user to the debug handler.
path = "/debug/" path = "/debug/"
} }
host := h.FQDN host := cmpx.Or(h.FQDN, r.Host)
if host == "" {
host = r.Host
}
target := "https://" + host + path target := "https://" + host + path
http.Redirect(w, r, target, http.StatusFound) http.Redirect(w, r, target, http.StatusFound)
} }

@ -18,6 +18,7 @@ import (
"time" "time"
"tailscale.com/metrics" "tailscale.com/metrics"
"tailscale.com/util/cmpx"
"tailscale.com/version" "tailscale.com/version"
) )
@ -96,16 +97,10 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
switch v := kv.Value.(type) { switch v := kv.Value.(type) {
case *expvar.Int: case *expvar.Int:
if typ == "" { fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, cmpx.Or(typ, "counter"), name, v.Value())
typ = "counter"
}
fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value())
return return
case *expvar.Float: case *expvar.Float:
if typ == "" { fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, cmpx.Or(typ, "gauge"), name, v.Value())
typ = "gauge"
}
fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value())
return return
case *metrics.Set: case *metrics.Set:
v.Do(func(kv expvar.KeyValue) { v.Do(func(kv expvar.KeyValue) {

@ -16,6 +16,7 @@ import (
"github.com/josharian/native" "github.com/josharian/native"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/cmpx"
) )
// DebugNetfilter prints debug information about netfilter rules to the // DebugNetfilter prints debug information about netfilter rules to the
@ -52,27 +53,18 @@ func DebugNetfilter(logf logger.Logf) error {
for _, ex := range rule.Exprs { for _, ex := range rule.Exprs {
switch v := ex.(type) { switch v := ex.(type) {
case *expr.Meta: case *expr.Meta:
key := metaKeyNames[v.Key] key := cmpx.Or(metaKeyNames[v.Key], "UNKNOWN")
if key == "" {
key = "UNKNOWN"
}
logf("netfilter: Meta: key=%s source_register=%v register=%d", key, v.SourceRegister, v.Register) logf("netfilter: Meta: key=%s source_register=%v register=%d", key, v.SourceRegister, v.Register)
case *expr.Cmp: case *expr.Cmp:
op := cmpOpNames[v.Op] op := cmpx.Or(cmpOpNames[v.Op], "UNKNOWN")
if op == "" {
op = "UNKNOWN"
}
logf("netfilter: Cmp: op=%s register=%d data=%s", op, v.Register, formatMaybePrintable(v.Data)) logf("netfilter: Cmp: op=%s register=%d data=%s", op, v.Register, formatMaybePrintable(v.Data))
case *expr.Counter: case *expr.Counter:
// don't print // don't print
case *expr.Verdict: case *expr.Verdict:
kind := verdictNames[v.Kind] kind := cmpx.Or(verdictNames[v.Kind], "UNKNOWN")
if kind == "" {
kind = "UNKNOWN"
}
logf("netfilter: Verdict: kind=%s data=%s", kind, v.Chain) logf("netfilter: Verdict: kind=%s data=%s", kind, v.Chain)
case *expr.Target: case *expr.Target:

Loading…
Cancel
Save