Skip to content

Commit 8305873

Browse files
committed
cli: move addrWithDefaultHost to package addr
Release note: None
1 parent 509f8c8 commit 8305873

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

pkg/cli/rpc_client.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ package cli
1212

1313
import (
1414
"context"
15-
"net"
1615

1716
"github.com/cockroachdb/cockroach/pkg/roachpb"
1817
"github.com/cockroachdb/cockroach/pkg/rpc"
1918
"github.com/cockroachdb/cockroach/pkg/server"
2019
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
2120
"github.com/cockroachdb/cockroach/pkg/util/hlc"
21+
"github.com/cockroachdb/cockroach/pkg/util/netutil/addr"
2222
"github.com/cockroachdb/cockroach/pkg/util/stop"
2323
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
2424
"github.com/cockroachdb/cockroach/pkg/util/tracing"
@@ -56,7 +56,7 @@ func getClientGRPCConn(
5656
if cfg.TestingKnobs.Server != nil {
5757
rpcContext.Knobs = cfg.TestingKnobs.Server.(*server.TestingKnobs).ContextTestingKnobs
5858
}
59-
addr, err := addrWithDefaultHost(cfg.AdvertiseAddr)
59+
addr, err := addr.AddrWithDefaultLocalhost(cfg.AdvertiseAddr)
6060
if err != nil {
6161
stopper.Stop(ctx)
6262
return nil, nil, nil, err
@@ -79,17 +79,6 @@ func getClientGRPCConn(
7979
return conn, clock, closer, nil
8080
}
8181

82-
func addrWithDefaultHost(addr string) (string, error) {
83-
host, port, err := net.SplitHostPort(addr)
84-
if err != nil {
85-
return "", err
86-
}
87-
if host == "" {
88-
host = "localhost"
89-
}
90-
return net.JoinHostPort(host, port), nil
91-
}
92-
9382
// getAdminClient returns an AdminClient and a closure that must be invoked
9483
// to free associated resources.
9584
func getAdminClient(ctx context.Context, cfg server.Config) (serverpb.AdminClient, func(), error) {

pkg/cli/start_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/cockroachdb/cockroach/pkg/testutils"
2424
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
2525
"github.com/cockroachdb/cockroach/pkg/util/log"
26+
"github.com/cockroachdb/cockroach/pkg/util/netutil/addr"
2627
"github.com/cockroachdb/pebble/vfs"
2728
"github.com/stretchr/testify/require"
2829
)
@@ -206,7 +207,7 @@ func TestAddrWithDefaultHost(t *testing.T) {
206207
}
207208

208209
for _, test := range testData {
209-
addr, err := addrWithDefaultHost(test.inAddr)
210+
addr, err := addr.AddrWithDefaultLocalhost(test.inAddr)
210211
if err != nil {
211212
t.Error(err)
212213
} else if addr != test.outAddr {

pkg/util/netutil/addr/addr.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ import (
1818
"github.com/spf13/pflag"
1919
)
2020

21+
// AddrWithDefaultLocalhost returns addr with the host set
22+
// to localhost if it is empty.
23+
func AddrWithDefaultLocalhost(addr string) (string, error) {
24+
host, port, err := net.SplitHostPort(addr)
25+
if err != nil {
26+
return "", err
27+
}
28+
if host == "" {
29+
host = "localhost"
30+
}
31+
return net.JoinHostPort(host, port), nil
32+
}
33+
2134
// SplitHostPort is like net.SplitHostPort however it supports
2235
// addresses without a port number. In that case, the provided port
2336
// number is used.

0 commit comments

Comments
 (0)