Merge pull request #572 from alixander/serve-layout-with-opts

Serve layout with opts
This commit is contained in:
Alexander Wang 2022-12-30 17:35:45 -08:00 committed by GitHub
commit 8a50c92788
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 28 deletions

View file

@ -82,6 +82,8 @@ func (p *execPlugin) HydrateOpts(opts []byte) error {
allString[k] = vt allString[k] = vt
case int64: case int64:
allString[k] = strconv.Itoa(int(vt)) allString[k] = strconv.Itoa(int(vt))
case float64:
allString[k] = strconv.Itoa(int(vt))
} }
} }

View file

@ -7,6 +7,7 @@ package d2plugin
import ( import (
"context" "context"
"encoding/json"
"os/exec" "os/exec"
"oss.terrastruct.com/util-go/xexec" "oss.terrastruct.com/util-go/xexec"
@ -166,3 +167,28 @@ func ListPluginFlags(ctx context.Context) ([]PluginSpecificFlag, error) {
return out, nil return out, nil
} }
func HydratePluginOpts(ctx context.Context, ms *xmain.State, plugin Plugin) error {
opts := make(map[string]interface{})
flags, err := plugin.Flags(ctx)
if err != nil {
return err
}
for _, f := range flags {
switch f.Type {
case "string":
val, _ := ms.Opts.Flags.GetString(f.Name)
opts[f.Tag] = val
case "int64":
val, _ := ms.Opts.Flags.GetInt64(f.Name)
opts[f.Tag] = val
}
}
b, err := json.Marshal(opts)
if err != nil {
return err
}
return plugin.HydrateOpts(b)
}

View file

@ -41,6 +41,11 @@ func Serve(p Plugin) xmain.RunFunc {
return xmain.UsageErrorf("expected first argument to be subcmd name") return xmain.UsageErrorf("expected first argument to be subcmd name")
} }
err = HydratePluginOpts(ctx, ms, p)
if err != nil {
return err
}
subcmd := ms.Opts.Flags.Arg(0) subcmd := ms.Opts.Flags.Arg(0)
switch subcmd { switch subcmd {
case "info": case "info":

29
main.go
View file

@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -150,7 +149,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
return err return err
} }
err = parseLayoutOpts(ctx, ms, plugin) err = d2plugin.HydratePluginOpts(ctx, ms, plugin)
if err != nil { if err != nil {
return err return err
} }
@ -314,29 +313,3 @@ func populateLayoutOpts(ctx context.Context, ms *xmain.State) error {
return nil return nil
} }
func parseLayoutOpts(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin) error {
opts := make(map[string]interface{})
flags, err := plugin.Flags(ctx)
if err != nil {
return err
}
for _, f := range flags {
switch f.Type {
case "string":
val, _ := ms.Opts.Flags.GetString(f.Name)
opts[f.Tag] = val
case "int64":
val, _ := ms.Opts.Flags.GetInt64(f.Name)
opts[f.Tag] = val
}
}
b, err := json.Marshal(opts)
if err != nil {
return err
}
err = plugin.HydrateOpts(b)
return err
}