Skip to content

Commit

Permalink
Fix bug to allow for multiple plugins (open-policy-agent#657)
Browse files Browse the repository at this point in the history
Signed-off-by: John Reese <[email protected]>
  • Loading branch information
jpreese authored Jan 4, 2022
1 parent 748e2e2 commit 06793a4
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions internal/commands/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,33 @@ func NewDefaultCommand() *cobra.Command {
cmd.AddCommand(NewPluginCommand(ctx))
cmd.AddCommand(NewFormatCommand(ctx))

pluginCmds, err := loadPlugins(ctx)
plugins, err := plugin.FindAll()
if err != nil {
logger.Fatalf("error loading plugins: %v", err)
logger.Fatalf("find all plugins: %s", err)
}

for p := range plugins {
cmd.AddCommand(newCommandFromPlugin(ctx, plugins[p]))
}

cmd.AddCommand(pluginCmds...)
return &cmd
}

func loadPlugins(ctx context.Context) ([]*cobra.Command, error) {
plugins, err := plugin.FindAll()
if err != nil {
return nil, fmt.Errorf("find plugins: %v", err)
}
func newCommandFromPlugin(ctx context.Context, p *plugin.Plugin) *cobra.Command {
pluginCommand := cobra.Command{
Use: p.Name,
Short: p.Usage,
Long: p.Description,
RunE: func(cmd *cobra.Command, args []string) error {
if err := p.Exec(ctx, args); err != nil {
return fmt.Errorf("execute plugin: %v", err)
}

var cmds []*cobra.Command
for _, plugin := range plugins {
cmd := cobra.Command{
Use: plugin.Name,
Short: plugin.Usage,
Long: plugin.Description,
RunE: func(cmd *cobra.Command, args []string) error {
if err := plugin.Exec(ctx, args); err != nil {
return fmt.Errorf("execute plugin: %v", err)
}

return nil
},
DisableFlagParsing: true,
}
return nil
},

cmds = append(cmds, &cmd)
DisableFlagParsing: true,
}

return cmds, nil
return &pluginCommand
}

0 comments on commit 06793a4

Please sign in to comment.