refactor: add plugin flags to category (#42)

This commit is contained in:
Robert Kaussow 2022-05-28 17:13:28 +02:00 committed by GitHub
parent d910fb4ca1
commit 6fe43d2f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 16 deletions

View File

@ -41,8 +41,6 @@ steps:
<!-- spellchecker-enable --> <!-- spellchecker-enable -->
<!-- prettier-ignore-end --> <!-- prettier-ignore-end -->
### Examples
## Build ## Build
Build the binary with the following command: Build the binary with the following command:

View File

@ -12,31 +12,35 @@ import (
) )
// settingsFlags has the cli.Flags for the plugin.Settings. // settingsFlags has the cli.Flags for the plugin.Settings.
func settingsFlags(settings *plugin.Settings) []cli.Flag { func settingsFlags(settings *plugin.Settings, category string) []cli.Flag {
return []cli.Flag{ return []cli.Flag{
&cli.StringFlag{ &cli.StringFlag{
Name: "username", Name: "username",
EnvVars: []string{"PLUGIN_USERNAME", "MATRIX_USERNAME"}, EnvVars: []string{"PLUGIN_USERNAME", "MATRIX_USERNAME"},
Usage: "sets username for authentication", Usage: "sets username for authentication",
Destination: &settings.Username, Destination: &settings.Username,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "password", Name: "password",
EnvVars: []string{"PLUGIN_PASSWORD", "MATRIX_PASSWORD"}, EnvVars: []string{"PLUGIN_PASSWORD", "MATRIX_PASSWORD"},
Usage: "sets password for authentication", Usage: "sets password for authentication",
Destination: &settings.Password, Destination: &settings.Password,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "userid", Name: "userid",
EnvVars: []string{"PLUGIN_USERID,PLUGIN_USER_ID", "MATRIX_USERID", "MATRIX_USER_ID"}, EnvVars: []string{"PLUGIN_USERID,PLUGIN_USER_ID", "MATRIX_USERID", "MATRIX_USER_ID"},
Usage: "sets userid for authentication", Usage: "sets userid for authentication",
Destination: &settings.UserID, Destination: &settings.UserID,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "accesstoken", Name: "accesstoken",
EnvVars: []string{"PLUGIN_ACCESSTOKEN,PLUGIN_ACCESS_TOKEN", "MATRIX_ACCESSTOKEN", "MATRIX_ACCESS_TOKEN"}, EnvVars: []string{"PLUGIN_ACCESSTOKEN,PLUGIN_ACCESS_TOKEN", "MATRIX_ACCESSTOKEN", "MATRIX_ACCESS_TOKEN"},
Usage: "sets access token for authentication", Usage: "sets access token for authentication",
Destination: &settings.AccessToken, Destination: &settings.AccessToken,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "homeserver", Name: "homeserver",
@ -44,12 +48,14 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Usage: "sets matrix home server url to use", Usage: "sets matrix home server url to use",
Value: "https://matrix.org", Value: "https://matrix.org",
Destination: &settings.Homeserver, Destination: &settings.Homeserver,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "roomid", Name: "roomid",
EnvVars: []string{"PLUGIN_ROOMID", "MATRIX_ROOMID"}, EnvVars: []string{"PLUGIN_ROOMID", "MATRIX_ROOMID"},
Usage: "sets roomid to send messages to", Usage: "sets roomid to send messages to",
Destination: &settings.RoomID, Destination: &settings.RoomID,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "template", Name: "template",
@ -57,6 +63,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Usage: "sets message template", Usage: "sets message template",
Value: "Build {{ build.Status }} [{{ repo.Owner }}/{{ repo.Name }}#{{ truncate commit.SHA 8 }}]({{ build.Link }}) ({{ build.Branch }}) by {{ commit.Author }}", Value: "Build {{ build.Status }} [{{ repo.Owner }}/{{ repo.Name }}#{{ truncate commit.SHA 8 }}]({{ build.Link }}) ({{ build.Branch }}) by {{ commit.Author }}",
Destination: &settings.Template, Destination: &settings.Template,
Category: category,
}, },
} }
} }

View File

@ -12,7 +12,7 @@ import (
"github.com/drone-plugins/drone-matrix/plugin" "github.com/drone-plugins/drone-matrix/plugin"
"github.com/joho/godotenv" "github.com/joho/godotenv"
"github.com/thegeeklab/drone-plugin-lib/errors" "github.com/sirupsen/logrus"
"github.com/thegeeklab/drone-plugin-lib/urfave" "github.com/thegeeklab/drone-plugin-lib/urfave"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
) )
@ -37,12 +37,12 @@ func main() {
Name: "drone-matrix", Name: "drone-matrix",
Usage: "build notifications for matrix", Usage: "build notifications for matrix",
Version: BuildVersion, Version: BuildVersion,
Flags: append(settingsFlags(settings), urfave.Flags()...), Flags: append(settingsFlags(settings, "Plugin Flags"), urfave.Flags()...),
Action: run(settings), Action: run(settings),
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
errors.HandleExit(err) logrus.Fatal(err)
} }
} }
@ -57,19 +57,11 @@ func run(settings *plugin.Settings) cli.ActionFunc {
) )
if err := plugin.Validate(); err != nil { if err := plugin.Validate(); err != nil {
if e, ok := err.(errors.ExitCoder); ok { return fmt.Errorf("validation failed: %w", err)
return e
}
return errors.ExitMessagef("validation failed: %w", err)
} }
if err := plugin.Execute(); err != nil { if err := plugin.Execute(); err != nil {
if e, ok := err.(errors.ExitCoder); ok { return fmt.Errorf("execution failed: %w", err)
return e
}
return errors.ExitMessagef("execution failed: %w", err)
} }
return nil return nil