* Make the log formatter configurable The change lets us configure the deamon and the plugins to output logs in the JSON format, which is easier to parse/filter by log processing tools. * Add validation for the log-format flag
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package libplugin
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
type PluginTemplate struct {
|
|
Name string
|
|
Usage string
|
|
Flags []cli.Flag
|
|
LogFormatter logrus.Formatter
|
|
CreateConfig func(c *cli.Context) (*SshPiperPluginConfig, error)
|
|
}
|
|
|
|
func CreateAndRunPluginTemplate(t *PluginTemplate) {
|
|
app := &cli.App{
|
|
Name: t.Name,
|
|
Usage: t.Usage,
|
|
Flags: t.Flags,
|
|
HideHelpCommand: true,
|
|
HideHelp: true,
|
|
Writer: os.Stderr,
|
|
ErrWriter: os.Stderr,
|
|
Action: func(c *cli.Context) error {
|
|
if t == nil {
|
|
return fmt.Errorf("plugin template is nil")
|
|
}
|
|
|
|
if t.CreateConfig == nil {
|
|
return fmt.Errorf("plugin template create config is nil")
|
|
}
|
|
|
|
config, err := t.CreateConfig(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p, err := NewFromStdio(*config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ConfigStdioLogrus(p, t.LogFormatter, nil)
|
|
return p.Serve()
|
|
},
|
|
}
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
fmt.Fprintf(os.Stderr, "cannot start plugin: %v\n", err)
|
|
}
|
|
}
|