Skip to content

refactor: give each way of opening a URL a name - #63

Merged
kukv merged 1 commit into
mainfrom
refactor/browser-openers
Sep 7, 2026
Merged

refactor: give each way of opening a URL a name#63
kukv merged 1 commit into
mainfrom
refactor/browser-openers

Conversation

@kukv

@kukv kukv commented Sep 7, 2026

Copy link
Copy Markdown
Owner

なぜ

internal/browsercommand() が、プラットフォームごとの argv を switch の各 case に直書きしていた。octoscope が起動しうるプログラムの一覧が 1 つの関数の分岐に散っていて、何が試されるのかが読み取りにくい。

何をしたか

opener interface を切り、ブラウザの起動方法ごとに型を用意した。nil が「自分の出番ではない」を意味する。

type opener interface {
	command(env environment) []string
}

var openers = []opener{
	userNamed{},       // GH_BROWSER / BROWSER
	windowsHandler{},  // rundll32 url.dll,FileProtocolHandler
	macOpen{},         // open
	wslBridge{},       // powershell.exe -NoProfile -Command Start-Process
	xdgOpen{},         // xdg-open
}

環境変数は名前付き定数にまとめ、goos / wsl / browser を持つ environment 値として呼び出し時に 1 度だけ組み立てる。取得タイミングは変えていない(init には移していない)。このパッケージがマシンから何を読むかがファイル冒頭で一覧できる。

トレードオフ

分岐は消えていない。置き場所が変わっただけ。 case goos == "windows": が各型の if env.goos != osWindows { return nil } になり、実装は 25 行から約 95 行に増えた。得たのは「コマンドに名前が付く」ことと「一覧が 1 箇所にまとまる」こと。

順序について

switch の上から下への読み順が持っていた優先順位が、スライスの並びという暗黙の前提になった。壊れうる箇所なので、テストで両方向を固定した。

既存テストは goos: linux でしか「ユーザー指定のブラウザが勝つ」を見ておらず、userNamed{} を後ろに下げても通ってしまう穴があった(Linux では他の opener が nil を返すため順序が結果に出ない)。Windows と macOS のケースを追加した。

壊した内容 結果
wslBridgexdgOpen を入れ替え FAIL
userNamed を後ろへ FAIL(追加前は PASS)
定数 GH_BROWSER / BROWSER を入れ替え FAIL

確認

  • make check 終了コード 0(tidy / lint 0 issues / fmt / 883 tests)
  • 挙動は変えていないため TUI の目視確認は行っていない

🤖 Generated with Claude Code

The switch in command() held the argv for every platform inline, so the
list of programs octoscope may run was spread across the branches of one
function. Each is now a type with a name, and the whole list reads off
the openers slice.

The dispatch did not go away, it moved: what was a case is now an early
return inside each opener. What ordering used to get from the switch
reading top to bottom it now gets from the slice, so the two places the
order is load-bearing are pinned by tests -- WSL before plain Linux, and
a browser the user named before every platform default. The latter was
untested on Windows and macOS, where alone it can be observed.

The environment variables are read through named constants and gathered
into one environment value at the call, so what this package takes from
the machine is visible at the top of the file instead of at three
scattered os.Getenv calls.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown

Code Metrics Report

Coverage Test Execution Time
87.2% 41s

Code coverage of files in pull request scope (55.8%, patch 80.0%)

Files Coverage Patch Coverage
internal/browser/browser.go 55.8% 80.0%

Reported by octocov

@kukv
kukv merged commit e87e469 into main Sep 7, 2026
3 checks passed
@kukv
kukv deleted the refactor/browser-openers branch September 7, 2026 21:51
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.

1 participant