From e08a0fb8dce03061e04b568355acab741cc2544d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 16 Nov 2022 22:45:08 -0800 Subject: [PATCH] 2022-11-16 10:45:08PM --- ci/release/template/man/d2.1 | 12 +++++++++++- cmd/d2/main.go | 6 ++++-- cmd/d2/watch.go | 19 ++++++------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/ci/release/template/man/d2.1 b/ci/release/template/man/d2.1 index b9c680414..0583e90c3 100644 --- a/ci/release/template/man/d2.1 +++ b/ci/release/template/man/d2.1 @@ -36,12 +36,22 @@ See more docs, the source code and license at .It Fl w , -watch Ar false Watch for changes to input and live reload. Use .Ev $PORT and Ev $HOST to specify the listening address. -.Ev $D2_PORT and $D2_HOST are also accepted and take priority. Default is localhost:0 +.It Fl h , -host Ar localhost +Host listening address when used with +.Ar watch +.Ns . +.It Fl p , -port Ar 0 +Port listening address when used with +.Ar watch +.Ns . .It Fl t , -theme Ar 0 Set the diagram theme to the passed integer. For a list of available options, see +.Lk https://oss.terrastruct.com/d2 +.Ns . .It Fl l , -layout Ar dagre Set the diagram layout engine to the passed string. For a list of available options, run .Ar layout +.Ns . .It Fl b , -bundle Ar true Bundle all assets and layers into the output svg. .It Fl d , -debug diff --git a/cmd/d2/main.go b/cmd/d2/main.go index 6392541f4..cfca96ddf 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -32,10 +32,12 @@ func run(ctx context.Context, ms *xmain.State) (err error) { ctx = xmain.DiscardSlog(ctx) // These should be kept up-to-date with the d2 man page - watchFlag, err := ms.Opts.Bool("D2_WATCH", "watch", "w", false, "watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.\n$D2_HOST and $D2_PORT are also accepted and take priority (default localhost:0, which is will open on a randomly available local port).") + watchFlag, err := ms.Opts.Bool("D2_WATCH", "watch", "w", false, "watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.\n(default localhost:0, which is will open on a randomly available local port).") if err != nil { return xmain.UsageErrorf(err.Error()) } + hostFlag := ms.Opts.String("HOST", "host", "h", "localhost", "host listening address when used with watch") + portFlag := ms.Opts.String("PORT", "port", "p", "0", "port listening address when used with watch") bundleFlag, err := ms.Opts.Bool("D2_BUNDLE", "bundle", "b", true, "bundle all assets and layers into the output svg.") if err != nil { return xmain.UsageErrorf(err.Error()) @@ -130,7 +132,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { return xmain.UsageErrorf("-w[atch] cannot be combined with reading input from stdin") } ms.Env.Setenv("LOG_TIMESTAMPS", "1") - w, err := newWatcher(ctx, ms, plugin, *themeFlag, inputPath, outputPath) + w, err := newWatcher(ctx, ms, plugin, *themeFlag, *hostFlag, *portFlag, inputPath, outputPath) if err != nil { return err } diff --git a/cmd/d2/watch.go b/cmd/d2/watch.go index 18dd9ea3c..a5d22c42c 100644 --- a/cmd/d2/watch.go +++ b/cmd/d2/watch.go @@ -43,6 +43,8 @@ type watcher struct { ms *xmain.State layoutPlugin d2plugin.Plugin themeID int64 + host string + port string inputPath string outputPath string @@ -69,7 +71,7 @@ type compileResult struct { SVG string `json:"svg"` } -func newWatcher(ctx context.Context, ms *xmain.State, layoutPlugin d2plugin.Plugin, themeID int64, inputPath, outputPath string) (*watcher, error) { +func newWatcher(ctx context.Context, ms *xmain.State, layoutPlugin d2plugin.Plugin, themeID int64, host, port, inputPath, outputPath string) (*watcher, error) { ctx, cancel := context.WithCancel(ctx) w := &watcher{ @@ -80,6 +82,8 @@ func newWatcher(ctx context.Context, ms *xmain.State, layoutPlugin d2plugin.Plug ms: ms, layoutPlugin: layoutPlugin, themeID: themeID, + host: host, + port: port, inputPath: inputPath, outputPath: outputPath, @@ -353,18 +357,7 @@ func (w *watcher) compileLoop(ctx context.Context) error { } func (w *watcher) listen() error { - host := "localhost" - port := "0" - hostEnv := w.ms.Env.Getenv("HOST") - if hostEnv != "" { - host = hostEnv - } - portEnv := w.ms.Env.Getenv("PORT") - if portEnv != "" { - port = portEnv - } - - l, err := net.Listen("tcp", net.JoinHostPort(host, port)) + l, err := net.Listen("tcp", net.JoinHostPort(w.host, w.port)) if err != nil { return err }