pax_global_header00006660000000000000000000000064145075445360014527gustar00rootroot0000000000000052 comment=1dd80dd27d1a55cc7aadace58781405509bf53a2 browser-1.3.0/000077500000000000000000000000001450754453600132135ustar00rootroot00000000000000browser-1.3.0/.github/000077500000000000000000000000001450754453600145535ustar00rootroot00000000000000browser-1.3.0/.github/workflows/000077500000000000000000000000001450754453600166105ustar00rootroot00000000000000browser-1.3.0/.github/workflows/push.yml000066400000000000000000000006451450754453600203170ustar00rootroot00000000000000on: [push, pull_request] name: CI jobs: test: strategy: matrix: go: [ '1.21' ] os: [ ubuntu-latest, macos-latest, windows-latest ] fail-fast: false runs-on: ${{ matrix.os }} name: Test suite steps: - uses: actions/checkout@v4 - name: Setup go uses: actions/setup-go@v4 with: go-version: ${{ matrix.go }} - run: go test -v ./... browser-1.3.0/LICENSE000066400000000000000000000024401450754453600142200ustar00rootroot00000000000000Copyright (c) 2014, Dave Cheney All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. browser-1.3.0/README.md000066400000000000000000000005221450754453600144710ustar00rootroot00000000000000 # browser Helpers to open URLs, readers, or files in the system default web browser. This fork adds: - `OpenReader` error wrapping; - `ErrNotFound` error wrapping on BSD; - Go 1.21 support. ## Usage ``` go import "github.com/cli/browser" err = browser.OpenURL(url) err = browser.OpenFile(path) err = browser.OpenReader(reader) ``` browser-1.3.0/browser.go000066400000000000000000000027001450754453600152240ustar00rootroot00000000000000// Package browser provides helpers to open files, readers, and urls in a browser window. // // The choice of which browser is started is entirely client dependent. package browser import ( "fmt" "io" "io/ioutil" "os" "os/exec" "path/filepath" ) // Stdout is the io.Writer to which executed commands write standard output. var Stdout io.Writer = os.Stdout // Stderr is the io.Writer to which executed commands write standard error. var Stderr io.Writer = os.Stderr // OpenFile opens new browser window for the file path. func OpenFile(path string) error { path, err := filepath.Abs(path) if err != nil { return err } return OpenURL("file://" + path) } // 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") if err != nil { 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: %w", err) } if err := f.Close(); err != nil { return fmt.Errorf("browser: caching temporary file failed: %w", err) } return OpenFile(f.Name()) } // OpenURL opens a new browser window pointing to url. func OpenURL(url string) error { return openBrowser(url) } func runCmd(prog string, args ...string) error { cmd := exec.Command(prog, args...) cmd.Stdout = Stdout cmd.Stderr = Stderr return cmd.Run() } browser-1.3.0/browser_darwin.go000066400000000000000000000001241450754453600165660ustar00rootroot00000000000000package browser func openBrowser(url string) error { return runCmd("open", url) } browser-1.3.0/browser_freebsd.go000066400000000000000000000003671450754453600167250ustar00rootroot00000000000000package browser import ( "errors" "fmt" "os/exec" ) func openBrowser(url string) error { err := runCmd("xdg-open", url) if errors.Is(err, exec.ErrNotFound) { return fmt.Errorf("%w - install xdg-utils from ports(8)", err) } return err } browser-1.3.0/browser_linux.go000066400000000000000000000010761450754453600164500ustar00rootroot00000000000000package browser import ( "os/exec" "strings" ) func openBrowser(url string) error { 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. // Look for one that exists and run it for _, provider := range providers { if _, err := exec.LookPath(provider); err == nil { return runCmd(provider, url) } } return &exec.Error{Name: strings.Join(providers, ","), Err: exec.ErrNotFound} } browser-1.3.0/browser_netbsd.go000066400000000000000000000004401450754453600165620ustar00rootroot00000000000000package browser import ( "errors" "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 pkgsrc(7)") } return err } browser-1.3.0/browser_openbsd.go000066400000000000000000000003671450754453600167450ustar00rootroot00000000000000package browser import ( "errors" "fmt" "os/exec" ) func openBrowser(url string) error { err := runCmd("xdg-open", url) if errors.Is(err, exec.ErrNotFound) { return fmt.Errorf("%w - install xdg-utils from ports(8)", err) } return err } browser-1.3.0/browser_unsupported.go000066400000000000000000000003451450754453600176770ustar00rootroot00000000000000// +build !linux,!windows,!darwin,!openbsd,!freebsd,!netbsd package browser import ( "fmt" "runtime" ) func openBrowser(url string) error { return fmt.Errorf("openBrowser: unsupported operating system: %v", runtime.GOOS) } browser-1.3.0/browser_windows.go000066400000000000000000000003001450754453600167700ustar00rootroot00000000000000package browser import "golang.org/x/sys/windows" func openBrowser(url string) error { return windows.ShellExecute(0, nil, windows.StringToUTF16Ptr(url), nil, nil, windows.SW_SHOWNORMAL) } browser-1.3.0/example_test.go000066400000000000000000000012411450754453600162320ustar00rootroot00000000000000package browser import "strings" func ExampleOpenFile() { _ = OpenFile("index.html") } func ExampleOpenReader() { // https://github.com/rust-lang/rust/issues/13871 const quote = `There was a night when winds from unknown spaces whirled us irresistibly into limitless vacum beyond all thought and entity. 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) } func ExampleOpenURL() { const url = "http://golang.org/" _ = OpenURL(url) } browser-1.3.0/examples/000077500000000000000000000000001450754453600150315ustar00rootroot00000000000000browser-1.3.0/examples/Open/000077500000000000000000000000001450754453600157325ustar00rootroot00000000000000browser-1.3.0/examples/Open/main.go000066400000000000000000000016761450754453600172170ustar00rootroot00000000000000// Open is a simple example of the github.com/pkg/browser package. // // Usage: // // # Open a file in a browser window // Open $FILE // // # Open a URL in a browser window // Open $URL // // # Open the contents of stdin in a browser window // cat $SOMEFILE | Open package main import ( "flag" "fmt" "log" "os" "path/filepath" "strings" "github.com/cli/browser" ) func usage() { fmt.Fprintf(os.Stderr, "Usage:\n %s { | | -}\n", filepath.Base(os.Args[0])) flag.PrintDefaults() } func init() { flag.Usage = usage flag.Parse() } func check(err error) { if err != nil { log.Fatal(err) } } func main() { args := flag.Args() 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])) } browser-1.3.0/go.mod000066400000000000000000000001111450754453600143120ustar00rootroot00000000000000module github.com/cli/browser go 1.21 require golang.org/x/sys v0.13.0 browser-1.3.0/go.sum000066400000000000000000000002311450754453600143420ustar00rootroot00000000000000golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=