From eba687fbbecfd641e00bf544db1c2942667cb3b2 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Thu, 19 Oct 2023 16:06:11 -0700 Subject: [PATCH] don't use xmain.State in imgbundler library --- d2cli/main.go | 22 ++++++++++++--------- lib/imgbundler/imgbundler.go | 32 +++++++++++++++---------------- lib/imgbundler/imgbundler_test.go | 22 ++++++++++----------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/d2cli/main.go b/d2cli/main.go index 56aed09e9..7963f9693 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -748,10 +748,11 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts return svg, err } - svg, bundleErr := imgbundler.BundleLocal(ctx, ms, svg) + cacheImages := ms.Env.Getenv("IMG_CACHE") == "1" + svg, bundleErr := imgbundler.BundleLocal(ctx, svg, cacheImages) if bundle { var bundleErr2 error - svg, bundleErr2 = imgbundler.BundleRemote(ctx, ms, svg) + svg, bundleErr2 = imgbundler.BundleRemote(ctx, svg, cacheImages) bundleErr = multierr.Combine(bundleErr, bundleErr2) } if forceAppendix && !toPNG { @@ -764,7 +765,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts if !bundle { var bundleErr2 error - svg, bundleErr2 = imgbundler.BundleRemote(ctx, ms, svg) + svg, bundleErr2 = imgbundler.BundleRemote(ctx, svg, cacheImages) bundleErr = multierr.Combine(bundleErr, bundleErr2) } @@ -833,8 +834,9 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opt return svg, err } - svg, bundleErr := imgbundler.BundleLocal(ctx, ms, svg) - svg, bundleErr2 := imgbundler.BundleRemote(ctx, ms, svg) + cacheImages := ms.Env.Getenv("IMG_CACHE") == "1" + svg, bundleErr := imgbundler.BundleLocal(ctx, svg, cacheImages) + svg, bundleErr2 := imgbundler.BundleRemote(ctx, svg, cacheImages) bundleErr = multierr.Combine(bundleErr, bundleErr2) if bundleErr != nil { return svg, bundleErr @@ -933,8 +935,9 @@ func renderPPTX(ctx context.Context, ms *xmain.State, presentation *pptx.Present return nil, err } - svg, bundleErr := imgbundler.BundleLocal(ctx, ms, svg) - svg, bundleErr2 := imgbundler.BundleRemote(ctx, ms, svg) + cacheImages := ms.Env.Getenv("IMG_CACHE") == "1" + svg, bundleErr := imgbundler.BundleLocal(ctx, svg, cacheImages) + svg, bundleErr2 := imgbundler.BundleRemote(ctx, svg, cacheImages) bundleErr = multierr.Combine(bundleErr, bundleErr2) if bundleErr != nil { return nil, bundleErr @@ -1178,8 +1181,9 @@ func renderPNGsForGIF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plug return nil, nil, err } - svg, bundleErr := imgbundler.BundleLocal(ctx, ms, svg) - svg, bundleErr2 := imgbundler.BundleRemote(ctx, ms, svg) + cacheImages := ms.Env.Getenv("IMG_CACHE") == "1" + svg, bundleErr := imgbundler.BundleLocal(ctx, svg, cacheImages) + svg, bundleErr2 := imgbundler.BundleRemote(ctx, svg, cacheImages) bundleErr = multierr.Combine(bundleErr, bundleErr2) if bundleErr != nil { return nil, nil, bundleErr diff --git a/lib/imgbundler/imgbundler.go b/lib/imgbundler/imgbundler.go index 526f3bf8d..8324ce205 100644 --- a/lib/imgbundler/imgbundler.go +++ b/lib/imgbundler/imgbundler.go @@ -19,8 +19,8 @@ import ( "golang.org/x/xerrors" + "oss.terrastruct.com/d2/lib/log" "oss.terrastruct.com/util-go/xdefer" - "oss.terrastruct.com/util-go/xmain" ) var imgCache sync.Map @@ -29,12 +29,12 @@ const maxImageSize int64 = 1 << 25 // 33_554_432 var imageRegex = regexp.MustCompile(` 0 { @@ -150,8 +150,8 @@ func runWorkers(ctx context.Context, ms *xmain.State, svg []byte, imgs [][][]byt } } -func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([]byte, error) { - if ms.Env.Getenv("IMG_CACHE") == "1" { +func worker(ctx context.Context, href []byte, isRemote, cacheImages bool) ([]byte, error) { + if cacheImages { if hit, ok := imgCache.Load(string(href)); ok { return hit.([]byte), nil } @@ -160,10 +160,10 @@ func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([ var mimeType string var err error if isRemote { - ms.Log.Debug.Printf("fetching %s remotely", string(href)) + log.Debug(ctx, fmt.Sprintf("fetching %s remotely", string(href))) buf, mimeType, err = httpGet(ctx, html.UnescapeString(string(href))) } else { - ms.Log.Debug.Printf("reading %s from disk", string(href)) + log.Debug(ctx, fmt.Sprintf("reading %s from disk", string(href))) buf, err = os.ReadFile(html.UnescapeString(string(href))) } if err != nil { @@ -177,7 +177,7 @@ func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([ b64 := base64.StdEncoding.EncodeToString(buf) out := []byte(fmt.Sprintf(`