cmd/d2: Allow running subcommands on a bundled plugin
This commit is contained in:
parent
e62b3206e8
commit
6128f1102f
2 changed files with 13 additions and 2 deletions
|
|
@ -37,7 +37,7 @@ func layoutHelp(ctx context.Context, ms *xmain.State) error {
|
|||
} else if len(ms.FlagSet.Args()) == 2 {
|
||||
return longLayoutHelp(ctx, ms)
|
||||
} else {
|
||||
return xmain.UsageErrorf("too many arguments passed")
|
||||
return pluginSubcommand(ctx, ms)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -110,6 +110,17 @@ For more information on setup, please visit https://github.com/terrastruct/d2.`,
|
|||
layout, strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
func pluginSubcommand(ctx context.Context, ms *xmain.State) error {
|
||||
layout := ms.FlagSet.Arg(1)
|
||||
plugin, _, err := d2plugin.FindPlugin(ctx, layout)
|
||||
if errors.Is(err, exec.ErrNotFound) {
|
||||
return layoutNotFound(ctx, layout)
|
||||
}
|
||||
|
||||
ms.Args = ms.FlagSet.Args()[2:]
|
||||
return d2plugin.Serve(plugin)(ctx, ms)
|
||||
}
|
||||
|
||||
func humanPath(fp string) string {
|
||||
if strings.HasPrefix(fp, os.Getenv("HOME")) {
|
||||
return filepath.Join("~", strings.TrimPrefix(fp, os.Getenv("HOME")))
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
err = ms.FlagSet.Parse(ms.Args)
|
||||
|
||||
if !errors.Is(err, pflag.ErrHelp) && err != nil {
|
||||
return fmt.Errorf("failed to parse flags: %w", err)
|
||||
return xmain.UsageErrorf("failed to parse flags: %v", err)
|
||||
}
|
||||
|
||||
if len(ms.FlagSet.Args()) > 0 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue