watch: Move xbrowser to util-go and fix

Closes #175
Closes #205
This commit is contained in:
Anmol Sethi 2022-12-01 11:01:37 -08:00
parent a05ba67a36
commit 6139c35af2
No known key found for this signature in database
GPG key ID: 25BC68888A99A8BA
5 changed files with 12 additions and 31 deletions

View file

@ -29,6 +29,8 @@
So you can run `go install oss.terrastruct.com/d2@latest` to install from source
now.
[#290](https://github.com/terrastruct/d2/pull/290)
- `BROWSER=0` now works to disable opening a browser on `--watch`.
[#311](https://github.com/terrastruct/d2/pull/311)
#### Bugfixes 🔴
@ -42,3 +44,6 @@
[#224](https://github.com/terrastruct/d2/pull/224)
- Avoid logging benign file watching errors.
[#293](https://github.com/terrastruct/d2/pull/293)
- `$BROWSER` now works to open a custom browser correctly.
For example, to open Firefox on macOS: `BROWSER='open -aFirefox'`
[#311](https://github.com/terrastruct/d2/pull/311)

4
go.mod generated
View file

@ -10,7 +10,6 @@ require (
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/lucasb-eyer/go-colorful v1.2.0
github.com/mazznoer/csscolorparser v0.1.3
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/playwright-community/playwright-go v0.2000.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
@ -24,7 +23,7 @@ require (
nhooyr.io/websocket v1.8.7
oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541
oss.terrastruct.com/util-go v0.0.0-20221201184644-70a234206a9a
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557
oss.terrastruct.com/xjson v0.0.0-20221018000420-4986731c4c4a
@ -50,6 +49,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect

4
go.sum generated
View file

@ -802,8 +802,8 @@ oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c h1:C1DDLzj2NrVi1YJ
oss.terrastruct.com/cmdlog v0.0.0-20221201100934-012c01b3431c/go.mod h1:C8u/lYTvQWc1xC7rHpgFfpScfQC4NMeGGMmlKVZZUXM=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541 h1:I9B1O1IJ6spivIQxbFRZmbhAwVeLwrcQRR1JbYUOvrI=
oss.terrastruct.com/diff v1.0.2-0.20221116222035-8bf4dd3ab541/go.mod h1:ags2QDy/T6jr69hT6bpmAmhr2H98n9o8Atf3QlUJPiU=
oss.terrastruct.com/util-go v0.0.0-20221201184644-70a234206a9a h1:4+7aYDQhnJa+lM0VpQ9xn4EsyBc5T09ry7d2YXnVekM=
oss.terrastruct.com/util-go v0.0.0-20221201184644-70a234206a9a/go.mod h1:l3FJb1Ne4egFfJRbdLJAk4hLZ61nr9kMm0thv2+SvCw=
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe h1:1CTXmBqea1vbVhYsyZ3NiCYUFZgURIQF/ItjrdlhwlE=
oss.terrastruct.com/util-go v0.0.0-20221201185848-8cc30ca56bbe/go.mod h1:bL3CBn27CtTm++1iqRh2p6f8AIWeTdtlTN199Kg9JYM=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f h1:7voRCwKM7TZkTo9u7hj+uV/zXoVB8czWrTq6MVIh3dg=
oss.terrastruct.com/xcontext v0.0.0-20221018000442-50fdafb12f4f/go.mod h1:Y0coTLsWwX0q3a+/Ndq797t+vWyxm42T49Ik3bzaDKY=
oss.terrastruct.com/xdefer v0.0.0-20221017222355-6f3b6e4d1557 h1:rPbhJbN1q7B4tnppSPoAMwq0t6Pk5SrQDQ5S6uoNNHg=

View file

@ -1,25 +0,0 @@
package xbrowser
import (
"context"
"fmt"
"os/exec"
"github.com/pkg/browser"
"oss.terrastruct.com/xos"
)
func OpenURL(ctx context.Context, env *xos.Env, url string) error {
browserEnv := env.Getenv("BROWSER")
if browserEnv != "" {
browserSh := fmt.Sprintf("%s '$1'", browserEnv)
cmd := exec.CommandContext(ctx, "sh", "-c", browserSh, "--", url)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to run %v (out: %q): %w", cmd.Args, out, err)
}
return nil
}
return browser.OpenURL(url)
}

View file

@ -19,9 +19,10 @@ import (
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
"oss.terrastruct.com/util-go/xbrowser"
"oss.terrastruct.com/d2/d2plugin"
"oss.terrastruct.com/d2/lib/png"
"oss.terrastruct.com/d2/lib/xbrowser"
"oss.terrastruct.com/d2/lib/xhttp"
"oss.terrastruct.com/d2/lib/xmain"
)
@ -372,7 +373,7 @@ func (w *watcher) compileLoop(ctx context.Context) error {
if firstCompile {
firstCompile = false
url := fmt.Sprintf("http://%s", w.l.Addr())
err = xbrowser.OpenURL(ctx, w.ms.Env, url)
err = xbrowser.Open(ctx, w.ms.Env, url)
if err != nil {
w.ms.Log.Warn.Printf("failed to open browser to %v: %v", url, err)
}