Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: pkg/browser
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: cli/browser
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.

Commits on Dec 6, 2020

  1. Run tests on any branch/PR

    mislav committed Dec 6, 2020
    Copy the full SHA
    e73015f View commit details
  2. Rename to cli/browser

    mislav committed Dec 6, 2020
    Copy the full SHA
    6d47120 View commit details
  3. Allow Go 1.13

    mislav committed Dec 6, 2020
    Copy the full SHA
    5af71e3 View commit details
  4. Use safeexec on Windows

    mislav committed Dec 6, 2020
    Copy the full SHA
    fa77765 View commit details
  5. Add WSL fallback for Linux

    mislav committed Dec 6, 2020
    Copy the full SHA
    2d60206 View commit details
  6. Copy the full SHA
    146a496 View commit details
  7. Appease linter in tests

    mislav committed Dec 6, 2020
    Copy the full SHA
    08e28c4 View commit details
  8. Copy the full SHA
    bdae545 View commit details
  9. Improve example

    mislav committed Dec 6, 2020
    Copy the full SHA
    b775798 View commit details
  10. Update README

    mislav committed Dec 6, 2020
    Copy the full SHA
    475b091 View commit details
  11. Wrap OpenReader errors

    mislav committed Dec 6, 2020
    Copy the full SHA
    5407605 View commit details

Commits on Mar 19, 2021

  1. Copy the full SHA
    2a4e89a View commit details
  2. Copy the full SHA
    dfaaea7 View commit details
  3. Copy the full SHA
    65237b5 View commit details
  4. Remove lies from README

    mislav committed Mar 19, 2021
    Copy the full SHA
    38f8067 View commit details

Commits on May 27, 2022

  1. Correct spelling mistake

    EdwardBetts committed May 27, 2022
    Copy the full SHA
    4901bee View commit details

Commits on May 30, 2022

  1. Merge pull request #3 from EdwardBetts/spelling

    Correct spelling mistake
    mislav authored May 30, 2022
    Copy the full SHA
    2b9fa6d View commit details

Commits on May 22, 2023

  1. Copy the full SHA
    4201402 View commit details
  2. Test in Go 1.20

    mislav committed May 22, 2023
    Copy the full SHA
    419c10c View commit details

Commits on Oct 5, 2023

  1. Update Go to 1.21, actions workflow dependencies, and golang.org/x/sy…

    …s to address security vulnerability
    samcoe committed Oct 5, 2023
    Copy the full SHA
    708f5e2 View commit details
  2. Merge pull request #10 from cli/update-dependencies

    Update Go to 1.21, actions workflow dependencies, and golang.org/x/sys to address security vulnerability
    samcoe authored Oct 5, 2023
    Copy the full SHA
    1dd80dd View commit details

Commits on Oct 6, 2023

  1. Copy the full SHA
    7b54e0a View commit details
  2. Merge pull request #11 from GreyXor/deprecated-tempfile

    Replace deprecated tempfile with createtemp
    samcoe authored Oct 6, 2023
    Copy the full SHA
    b393df5 View commit details
Showing with 61 additions and 82 deletions.
  1. +12 −10 .github/workflows/push.yml
  2. +10 −45 README.md
  3. +5 −6 browser.go
  4. +3 −2 browser_freebsd.go
  5. +1 −1 browser_linux.go
  6. +3 −2 browser_openbsd.go
  7. +3 −3 example_test.go
  8. +19 −8 examples/Open/main.go
  9. +3 −3 go.mod
  10. +2 −2 go.sum
22 changes: 12 additions & 10 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
on:
push:
branches:
- master
name: Push Event
on: [push, pull_request]

name: CI
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.15' ]
name: Go ${{ matrix.go }} test
go: [ '1.21' ]
os: [ ubuntu-latest, macos-latest, windows-latest ]
fail-fast: false

runs-on: ${{ matrix.os }}
name: Test suite

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup go
uses: actions/setup-go@v1
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- run: go test -v ./...
55 changes: 10 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,20 @@

# browser
import "github.com/pkg/browser"

Package browser provides helpers to open files, readers, and urls in a browser window.
Helpers to open URLs, readers, or files in the system default web browser.

The choice of which browser is started is entirely client dependant.
This fork adds:

- `OpenReader` error wrapping;
- `ErrNotFound` error wrapping on BSD;
- Go 1.21 support.

## Usage



## Variables
``` go
var Stderr io.Writer = os.Stderr
```
Stderr is the io.Writer to which executed commands write standard error.
import "github.com/cli/browser"

``` go
var Stdout io.Writer = os.Stdout
err = browser.OpenURL(url)
err = browser.OpenFile(path)
err = browser.OpenReader(reader)
```
Stdout is the io.Writer to which executed commands write standard output.


## func OpenFile
``` go
func OpenFile(path string) error
```
OpenFile opens new browser window for the file path.


## func OpenReader
``` go
func OpenReader(r io.Reader) error
```
OpenReader consumes the contents of r and presents the
results in a new browser window.


## func OpenURL
``` go
func OpenURL(url string) error
```
OpenURL opens a new browser window pointing to url.









- - -
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
11 changes: 5 additions & 6 deletions browser.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Package browser provides helpers to open files, readers, and urls in a browser window.
//
// The choice of which browser is started is entirely client dependant.
// The choice of which browser is started is entirely client dependent.
package browser

import (
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
@@ -30,16 +29,16 @@ func OpenFile(path string) error {
// OpenReader consumes the contents of r and presents the
// results in a new browser window.
func OpenReader(r io.Reader) error {
f, err := ioutil.TempFile("", "browser.*.html")
f, err := os.CreateTemp("", "browser.*.html")
if err != nil {
return fmt.Errorf("browser: could not create temporary file: %v", err)
return fmt.Errorf("browser: could not create temporary file: %w", err)
}
if _, err := io.Copy(f, r); err != nil {
f.Close()
return fmt.Errorf("browser: caching temporary file failed: %v", err)
return fmt.Errorf("browser: caching temporary file failed: %w", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("browser: caching temporary file failed: %v", err)
return fmt.Errorf("browser: caching temporary file failed: %w", err)
}
return OpenFile(f.Name())
}
5 changes: 3 additions & 2 deletions browser_freebsd.go
Original file line number Diff line number Diff line change
@@ -2,13 +2,14 @@ package browser

import (
"errors"
"fmt"
"os/exec"
)

func openBrowser(url string) error {
err := runCmd("xdg-open", url)
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
return errors.New("xdg-open: command not found - install xdg-utils from ports(8)")
if errors.Is(err, exec.ErrNotFound) {
return fmt.Errorf("%w - install xdg-utils from ports(8)", err)
}
return err
}
2 changes: 1 addition & 1 deletion browser_linux.go
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ import (
)

func openBrowser(url string) error {
providers := []string{"xdg-open", "x-www-browser", "www-browser"}
providers := []string{"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.
5 changes: 3 additions & 2 deletions browser_openbsd.go
Original file line number Diff line number Diff line change
@@ -2,13 +2,14 @@ package browser

import (
"errors"
"fmt"
"os/exec"
)

func openBrowser(url string) error {
err := runCmd("xdg-open", url)
if e, ok := err.(*exec.Error); ok && e.Err == exec.ErrNotFound {
return errors.New("xdg-open: command not found - install xdg-utils from ports(8)")
if errors.Is(err, exec.ErrNotFound) {
return fmt.Errorf("%w - install xdg-utils from ports(8)", err)
}
return err
}
6 changes: 3 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ package browser
import "strings"

func ExampleOpenFile() {
OpenFile("index.html")
_ = OpenFile("index.html")
}

func ExampleOpenReader() {
@@ -14,10 +14,10 @@ Perceptions of the most maddeningly untransmissible sort thronged upon us;
perceptions of infinity which at the time convulsed us with joy, yet which
are now partly lost to my memory and partly incapable of presentation to others.`
r := strings.NewReader(quote)
OpenReader(r)
_ = OpenReader(r)
}

func ExampleOpenURL() {
const url = "http://golang.org/"
OpenURL(url)
_ = OpenURL(url)
}
27 changes: 19 additions & 8 deletions examples/Open/main.go
Original file line number Diff line number Diff line change
@@ -17,12 +17,14 @@ import (
"fmt"
"log"
"os"
"path/filepath"
"strings"

"github.com/pkg/browser"
"github.com/cli/browser"
)

func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n %s [file]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Usage:\n %s {<url> | <file> | -}\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}

@@ -39,12 +41,21 @@ func check(err error) {

func main() {
args := flag.Args()
switch len(args) {
case 0:
check(browser.OpenReader(os.Stdin))
case 1:
check(browser.OpenFile(args[0]))
default:

if len(args) != 1 {
usage()
os.Exit(1)
}

if args[0] == "-" {
check(browser.OpenReader(os.Stdin))
return
}

if strings.HasPrefix(args[0], "http:") || strings.HasPrefix(args[0], "https:") {
check(browser.OpenURL(args[0]))
return
}

check(browser.OpenFile(args[0]))
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module github.com/pkg/browser
module github.com/cli/browser

go 1.14
go 1.21

require golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71
require golang.org/x/sys v0.13.0
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71 h1:X/2sJAybVknnUnV7AD2HdT6rm2p5BP6eH2j+igduWgk=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=