2022-11-16 10:45:08PM

This commit is contained in:
Alexander Wang 2022-11-16 22:45:08 -08:00
parent 052b461b03
commit e08a0fb8dc
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 21 additions and 16 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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
}