Merge pull request #52 from terrastruct/plugin-cmd-886f
cmd/d2: Allow running subcommands on a bundled plugin
This commit is contained in:
commit
b29829881d
2 changed files with 17 additions and 4 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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,10 +85,12 @@ func longLayoutHelp(ctx context.Context, ms *xmain.State) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if !strings.HasSuffix(pluginInfo.LongHelp, "\n") {
|
||||
pluginInfo.LongHelp += "\n"
|
||||
}
|
||||
fmt.Fprintf(ms.Stdout, `%s (%s):
|
||||
|
||||
%s
|
||||
`, pluginInfo.Name, pluginLocation, pluginInfo.LongHelp)
|
||||
%s`, pluginInfo.Name, pluginLocation, pluginInfo.LongHelp)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -110,6 +112,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