From efb2fefb3d0668d5859e8e62103ea99acdc6195c Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Wed, 23 Aug 2023 16:10:27 +0900 Subject: [PATCH] feat: ADD WSL route in auto open feature (#48) --- cmd/browser.go | 39 ++++++++++++++++++++++- cmd/browser_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 cmd/browser_test.go diff --git a/cmd/browser.go b/cmd/browser.go index 7cea62b..5f8aee5 100644 --- a/cmd/browser.go +++ b/cmd/browser.go @@ -1,11 +1,43 @@ package cmd import ( + "os" "os/exec" "runtime" + "strings" "time" ) +type FileReader interface { + ReadFile(string) (string, error) +} + +type ProcVersionReader struct{} + +func (r ProcVersionReader) ReadFile(filename string) (string, error) { + data, err := os.ReadFile(filename) + if err != nil { + return "", err + } + return string(data), nil +} + +func isContainWSL(data string) bool { + return strings.Contains(data, "WSL") +} + +func isWSLWithReader(reader FileReader) bool { + data, err := reader.ReadFile("/proc/version") + if err != nil { + return false + } + return isContainWSL(data) +} + +func isWSL() bool { + return isWSLWithReader(ProcVersionReader{}) +} + func openBrowser(url string) error { <-time.After(100 * time.Millisecond) var args []string @@ -17,7 +49,12 @@ func openBrowser(url string) error { case "darwin": cmd = "open" default: // "linux", "freebsd", "openbsd", "netbsd" - cmd = "xdg-open" + if isWSL() { + cmd = "cmd.exe" + args = []string{"/c", "start"} + } else { + cmd = "xdg-open" + } } args = append(args, url) return exec.Command(cmd, args...).Start() diff --git a/cmd/browser_test.go b/cmd/browser_test.go new file mode 100644 index 0000000..be10f8d --- /dev/null +++ b/cmd/browser_test.go @@ -0,0 +1,76 @@ +package cmd + +import ( + "errors" + "testing" +) + +type MockFileReader struct { + content string + err error +} + +func (m MockFileReader) ReadFile(path string) (string, error) { + return m.content, m.err +} + +func TestIsContainWSL(t *testing.T) { + tests := []struct { + name string + data string + expected bool + }{ + { + name: "WSL Data", + data: "Linux version 4.19.128-microsoft-standard (WSL2)", + expected: true, + }, + { + name: "Non-WSL Data", + data: "Linux version 4.15.0-72-generic", + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := isContainWSL(tt.data) + if result != tt.expected { + t.Errorf("expected %v, got %v", tt.expected, result) + } + }) + } +} + +func TestIsWSL(t *testing.T) { + tests := []struct { + name string + reader FileReader + expected bool + }{ + { + name: "WSL Data", + reader: MockFileReader{content: "Linux version 4.19.128-microsoft-standard (WSL2)"}, + expected: true, + }, + { + name: "Non-WSL Data", + reader: MockFileReader{content: "Linux version 4.15.0-72-generic"}, + expected: false, + }, + { + name: "Read error", + reader: MockFileReader{err: errors.New("read error")}, + expected: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := isWSLWithReader(tt.reader) + if result != tt.expected { + t.Errorf("expected %v, got %v", tt.expected, result) + } + }) + } +}