From 5bc8b4dadb781d812c80496f0740182b6c2d9186 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 6 Oct 2021 06:29:32 +0300 Subject: [PATCH] os: implement support for VOPEN_URI_CMD env override for os.open_uri --- vlib/os/open_uri_default.c.v | 44 +++++++++++++++++------------------- vlib/os/open_uri_windows.c.v | 8 +++++++ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/vlib/os/open_uri_default.c.v b/vlib/os/open_uri_default.c.v index ebada52b24..6505e03c23 100644 --- a/vlib/os/open_uri_default.c.v +++ b/vlib/os/open_uri_default.c.v @@ -1,32 +1,30 @@ module os pub fn open_uri(uri string) ? { - $if macos { - result := execute('open "$uri"') - if result.exit_code != 0 { - return error('unable to open url: $result.output') - } - } $else $if freebsd || openbsd { - result := execute('xdg-open "$uri"') - if result.exit_code != 0 { - return error('unable to open url: $result.output') - } - } $else $if linux { - providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview'] - - // There are multiple possible providers to open a browser on linux - // One of them is xdg-open, another is x-www-browser, then there's www-browser, etc. - // Look for one that exists and run it - for provider in providers { - if exists_in_system_path(provider) { - result := execute('$provider "$uri"') - if result.exit_code != 0 { - return error('unable to open url: $result.output') + mut vopen_uri_cmd := getenv('VOPEN_URI_CMD') + if vopen_uri_cmd == '' { + $if macos { + vopen_uri_cmd = 'open' + } $else $if freebsd || openbsd { + vopen_uri_cmd = 'xdg-open' + } $else $if linux { + providers := ['xdg-open', 'x-www-browser', 'www-browser', 'wslview'] + // There are multiple possible providers to open a browser on linux + // One of them is xdg-open, another is x-www-browser, then there's www-browser, etc. + // Look for one that exists and run it + for provider in providers { + if exists_in_system_path(provider) { + vopen_uri_cmd = provider + break } - break } } - } $else { + } + if vopen_uri_cmd == '' { return error('unsupported platform') } + result := execute('$vopen_uri_cmd "$uri"') + if result.exit_code != 0 { + return error('unable to open url: $result.output') + } } diff --git a/vlib/os/open_uri_windows.c.v b/vlib/os/open_uri_windows.c.v index 4b3d3accb1..296c83df1b 100644 --- a/vlib/os/open_uri_windows.c.v +++ b/vlib/os/open_uri_windows.c.v @@ -5,6 +5,14 @@ import dl type ShellExecuteWin = fn (voidptr, &u16, &u16, &u16, &u16, int) pub fn open_uri(uri string) ? { + mut vopen_uri_cmd := getenv('VOPEN_URI_CMD') + if vopen_uri_cmd != '' { + result := execute('$vopen_uri_cmd "$uri"') + if result.exit_code != 0 { + return error('unable to open url: $result.output') + } + return + } handle := dl.open_opt('shell32', dl.rtld_now) ? // https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew func := ShellExecuteWin(dl.sym_opt(handle, 'ShellExecuteW') ?)