Skip to content

provides utilities for interacting with users' browsers

License

Notifications You must be signed in to change notification settings

abc-inc/browser

Repository files navigation

Introduction

browser is a dependency-free library for opening URLs (or files) in the most appropriate browser or assigned application, respectively. Based on the operating system and available executables, it tries to open URLs in the following sequence:

  1. command defined by the environment variable $BROWSER (if set)
  2. operating system specific launcher
    • macOS: /usr/bin/open
    • Windows: cmd /c start
    • otherwise: xdg-open (if environment variable $DISPLAY is set)
  3. one of the following binaries from $PATH
    • chrome
    • google-chrome
    • chromium
    • firefox

Use browser as library

Installation

go get -d -u github.com/abc-inc/browser

Usage

import "github.com/abc-inc/browser"

...

if ok := browser.Open(url); !ok {
    // something bad happened
}

Use browser as standalone application

Installation

go get -u github.com/abc-inc/browser

Examples

# open a URL in the default browser
browser http://localhost/

# open a local file in the default browser
browser file:///C:/Temp/report.html

# macOS: use Firefox even if another default web browser is set
BROWSER="open -a Firefox" browser http://localhost/file.zip

# download a file with wget or curl - whatever is available
BROWSER="wget:curl -o index.html" browser http://localhost/

Since browser leverages operating system specific launchers, it can also be used to open other files and directories:

# play audio file using the media player associated with the file type
browser file:///home/me/Music/track.mp3

# open a folder in Visual Studio Code or vi (must be installed separately)
BROWSER=code:vi browser file:///Users/me/dev/project

Similar Projects

Feature abc-inc/browser pkg/browser hashicorp/terraform
Windows/Linux/macOS/OpenBSD ☑️ ☑️ ☑️
FreeBSD/NetBSD (xdg-open) ☑️
WSL support ☑️
$BROWSER support ☑️
open from io.Reader ☑️
mock support ☑️
custom Cmd (Args, Dir, Env, etc.) ☑️