Skip to content

Commit 829d33d

Browse files
committed
Display remote address only, enable port with -p flag
Also add "conn" and "conns" as aliases
1 parent 61539cf commit 829d33d

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

cmd/cmd.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,21 @@ func ServerCmd() *cobra.Command {
3030

3131
func ConnectionsCmd() *cobra.Command {
3232
c := &cobra.Command{
33-
Use: "connections",
34-
Short: "Show active connections",
35-
Args: cobra.NoArgs,
33+
Use: "connections",
34+
Aliases: []string{"conn", "conns"},
35+
Short: "Show active connections",
36+
Args: cobra.NoArgs,
3637
}
38+
var withPort bool
3739
config := server.DefaultConfig()
3840
config.InstallAdminFlags(c.Flags())
41+
c.Flags().BoolVarP(&withPort, "port", "p", false, "Show remote port in output")
3942
c.RunE = func(cmd *cobra.Command, _ []string) error {
4043
infos, err := server.QueryConnections(config)
4144
if err != nil {
4245
return fmt.Errorf("query connections: %w", err)
4346
}
44-
return server.PrintConnections(os.Stdout, infos)
47+
return server.PrintConnections(os.Stdout, infos, withPort)
4548
}
4649
return c
4750
}

pkg/server/server.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ func QueryConnections(config Config) ([]ConnectionInfo, error) {
361361
return infos, nil
362362
}
363363

364-
func PrintConnections(w io.Writer, infos []ConnectionInfo) error {
364+
func PrintConnections(w io.Writer, infos []ConnectionInfo, withPort bool) error {
365365
table := tablewriter.NewTable(
366366
w,
367367
tablewriter.WithRendition(tw.Rendition{
@@ -386,13 +386,21 @@ func PrintConnections(w io.Writer, infos []ConnectionInfo) error {
386386
)
387387
table.Header("Index", "Remote", "Path", "Status", "Connected")
388388
for _, info := range infos {
389+
remote := info.RemoteAddr
390+
if remote == "" {
391+
remote = UnknownFallback
392+
}
393+
if withPort {
394+
remote = assembleRemote(info.RemoteAddr, info.RemotePort)
395+
}
396+
389397
status := "Active"
390398
if info.QueuePos > 0 {
391399
status = fmt.Sprintf("Queued (%d)", info.QueuePos)
392400
}
393401
if err := table.Append([]string{
394402
strconv.FormatUint(info.Index, 10),
395-
assembleRemote(info.RemoteAddr, info.RemotePort),
403+
remote,
396404
strings.TrimSuffix(info.Path, "/git-upload-pack"),
397405
status,
398406
info.Connected.Format(time.DateTime),

pkg/server/server_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package server
22

33
import (
4+
"bytes"
5+
"strings"
46
"testing"
57
"time"
68

@@ -27,3 +29,34 @@ func TestFormatTelegrafLine(t *testing.T) {
2729
line := formatTelegrafLine("host name,prod=1", 10, 15, time.Unix(0, 1778837100123456789))
2830
assert.Equal(t, "git-queue,host=host\\ name\\,prod\\=1 active=10i,queued=15i 1778837100123456789\n", line)
2931
}
32+
33+
func TestPrintConnections_DefaultRemoteWithoutPort(t *testing.T) {
34+
var b bytes.Buffer
35+
err := PrintConnections(&b, []ConnectionInfo{{
36+
Index: 1,
37+
RemoteAddr: "192.0.2.12",
38+
RemotePort: "443",
39+
Path: "/repo.git/git-upload-pack",
40+
Connected: time.Date(2026, 5, 16, 12, 0, 0, 0, time.UTC),
41+
}}, false)
42+
require.NoError(t, err)
43+
44+
out := b.String()
45+
assert.Contains(t, out, "192.0.2.12")
46+
assert.NotContains(t, out, "192.0.2.12:443")
47+
}
48+
49+
func TestPrintConnections_WithPort(t *testing.T) {
50+
var b bytes.Buffer
51+
err := PrintConnections(&b, []ConnectionInfo{{
52+
Index: 1,
53+
RemoteAddr: "192.0.2.12",
54+
RemotePort: "443",
55+
Path: "/repo.git/git-upload-pack",
56+
Connected: time.Date(2026, 5, 16, 12, 0, 0, 0, time.UTC),
57+
}}, true)
58+
require.NoError(t, err)
59+
60+
out := b.String()
61+
assert.True(t, strings.Contains(out, "192.0.2.12:443") || strings.Contains(out, "[192.0.2.12]:443"))
62+
}

0 commit comments

Comments
 (0)