Update help, manpages, and cleanup

This commit is contained in:
Bernard Xie 2022-11-17 14:13:23 -08:00
parent d332b2e7d6
commit 530c70445a
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C
4 changed files with 15 additions and 24 deletions

View file

@ -9,7 +9,7 @@
.Op Fl -watch Ar false
.Op Fl -theme Em 0
.Ar file.d2
.Op Ar file.svg
.Op Ar file.svg|file.png
.Nm d2
.Op Fl -watch Ar false
.Op Fl -theme Em 0
@ -23,6 +23,8 @@ compiles and renders
.Ar file.d2
to
.Ar file.svg
or
.Ar png
.Ns .
.Pp
Pass - to have

View file

@ -15,9 +15,9 @@ import (
func help(ms *xmain.State) {
fmt.Fprintf(ms.Stdout, `Usage:
%s [--watch=false] [--theme=0] file.d2 [file.svg]
%s [--watch=false] [--theme=0] file.d2 [file.svg|file.png]
%[1]s compiles and renders file.d2 to file.svg
%[1]s compiles and renders file.d2 to file.svg or png
Use - to have d2 read from stdin or write to stdout.
Flags:

View file

@ -34,7 +34,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
watchFlag := ms.FlagSet.BoolP("watch", "w", false, "watch for changes to input and live reload. Use $PORT and $HOST to specify the listening address.\n$D2_PORT and $D2_HOST are also accepted and take priority. Default is localhost:0")
themeFlag := ms.FlagSet.Int64P("theme", "t", 0, "set the diagram theme. For a list of available options, see https://oss.terrastruct.com/d2")
bundleFlag := ms.FlagSet.BoolP("bundle", "b", true, "bundle all assets and layers into the output svg")
bundleFlag := ms.FlagSet.BoolP("bundle", "b", true, "when outputting SVG, bundle all assets and layers into the output file")
versionFlag := ms.FlagSet.BoolP("version", "v", false, "get the version")
debugFlag := ms.FlagSet.BoolP("debug", "d", false, "print debug logs")
err = ms.FlagSet.Parse(ms.Args)
@ -119,12 +119,8 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
if err != nil {
return err
}
defer func() error {
err = pw.Cleanup(*watchFlag)
if err != nil {
return err
}
return nil
defer func() {
err = pw.Cleanup()
}()
}

View file

@ -23,9 +23,6 @@ type Playwright struct {
}
func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
if err = pw.BrowserContext.Close(); err != nil {
return Playwright{}, err
}
if err = pw.Browser.Close(); err != nil {
return Playwright{}, err
}
@ -49,12 +46,7 @@ func (pw *Playwright) RestartBrowser() (newPW Playwright, err error) {
}, nil
}
func (pw *Playwright) Cleanup(isWatch bool) (err error) {
if !isWatch {
if err = pw.BrowserContext.Close(); err != nil {
return err
}
}
func (pw *Playwright) Cleanup() (err error) {
if err = pw.Browser.Close(); err != nil {
return err
}
@ -116,8 +108,7 @@ var genPNGScript string
func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) {
if page == nil {
ms.Log.Error.Printf("Playwright was not initialized properly for PNG export")
return nil, fmt.Errorf("Playwright page is nil")
return nil, fmt.Errorf("Playwright was not initialized properly for PNG export")
}
encodedSVG := base64.StdEncoding.EncodeToString(svg)
@ -129,8 +120,10 @@ func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage [
pngString := fmt.Sprintf("%v", pngInterface)
pngPrefix := "data:image/png;base64,"
if !strings.HasPrefix(pngString, pngPrefix) {
ms.Log.Error.Printf("failed to convert D2 file to PNG")
return nil, fmt.Errorf("Playwright export generated invalid png")
if len(pngString) > 20 {
pngString = pngString[0:20] + "..."
}
return nil, fmt.Errorf("invalid PNG: %v\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", pngString)
}
splicedPNGString := pngString[len(pngPrefix):]
return base64.StdEncoding.DecodeString(splicedPNGString)