Skip to content

refactor: Move the reusable code out of the CLI into a package - #4

Open
jocelyne-a wants to merge 1 commit into
mainfrom
importable-bridge-package
Open

refactor: Move the reusable code out of the CLI into a package#4
jocelyne-a wants to merge 1 commit into
mainfrom
importable-bridge-package

Conversation

@jocelyne-a

@jocelyne-a jocelyne-a commented Aug 17, 2026

Copy link
Copy Markdown

Move the reusable code out of the CLI into an importable package

Description

Everything in this repo lives in package main, which Go will not let another program import. The RDC device-controller integration tests need the session and tunnel logic that already works here, and the only way to reach it today is to run the CLI.

This moves the reusable parts into a bridge package and turns the one hardcoded decision — where to listen locally — into a parameter. The CLI still binds /var/run/usbmuxd and 127.0.0.1:7001 exactly as before; a caller can pass an ordinary TCP listener instead, which also means it needs no root.

Nothing is rewritten, only relocated. The files moved with git mv so history follows them, and the edits are the package declaration, exporting what now crosses a package boundary, and the listener change.

Two renames, done now because these identifiers are unexported in package main today, so renaming is free rather than breaking: runTCPServerServeRaw, and AdbConnectionBridgeRawConnectionBridge. Neither contains adb-specific logic — they copy bytes without interpreting them, which is exactly what adbUrl on Android and usbmuxdUrl on iOS both need. The per-connection log lines lose their adb prefix for the same reason: they otherwise report an "adb client" while serving usbmux. main.go keeps its own adb naming, which is accurate — runAndroid really does serve adb.

Types of Changes

  • Refactor/improvements

Tasks

  • Move the non-CLI files into bridge/ with git mv
  • Export the identifiers that cross the new package boundary
  • Replace the hardcoded listeners with ServeRaw / ServeUsbmux taking a net.Listener
  • Rename the byte relay away from adb while it is still unexported
  • Keep main.go as the CLI only
  • Confirm the CLI's output is byte-identical to main

Review

  • Tests — existing suite unchanged and passing, plus three new in bridge/serve_test.go: usbmux over a TCP listener, byte relaying in both directions, and clean shutdown on context cancel. gofmt -s, go vet ./... and go test -race -count=2 ./... all clean.
  • Documentation — no README change needed; the CLI is unaffected.
  • CHANGELOG — the repo doesn't have one.

The claim worth checking hardest is that the CLI didn't move: binaries built from main and from this branch produce byte-identical output across -h, no arguments, -version, an unknown region and an undefined flag.

Deployment Notes

None. GoReleaser still builds main: ., the Makefile and CI still go build ., and ./... picks the new package up automatically. Coverage is now reported per package because the code moved out of the root, but CI uploads it as an artifact without a threshold.

@jocelyne-a
jocelyne-a force-pushed the importable-bridge-package branch 2 times, most recently from 3cbe0e4 to b14dce9 Compare August 18, 2026 15:58
@jocelyne-a
jocelyne-a marked this pull request as ready for review August 18, 2026 17:02
Everything lived in package main, which Go will not let other programs import, so the only way to use it was to run the command; this moves the reusable parts into a bridge package and lets whoever calls it decide where to listen, so a test can serve on an ordinary TCP port instead of taking over /var/run/usbmuxd as root. The command itself behaves exactly as it did before. Two things are renamed while they are still unexported and renaming is free rather than breaking: runTCPServer becomes ServeRaw and AdbConnectionBridge becomes RawConnectionBridge, because neither contains any adb-specific logic — they copy bytes without interpreting them, which is what both adbUrl on Android and usbmuxdUrl on iOS need. The per-connection log lines lose their adb prefix for the same reason, since they otherwise report an adb client while serving usbmux. The CLI keeps its own adb naming, which is accurate: runAndroid really does serve adb.
@rillgen-saucelabs
rillgen-saucelabs force-pushed the importable-bridge-package branch from b14dce9 to e692da5 Compare August 26, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants