0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-22 11:10:39 +00:00

refactor: add plugin flags to category (#16)

This commit is contained in:
Robert Kaussow 2022-05-29 12:37:09 +02:00 committed by GitHub
parent 46cf242810
commit 2c1fe02de9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 43 deletions

View File

@ -6,37 +6,45 @@ 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: "endpoint", Name: "endpoint",
Usage: "endpoint for the s3 connection", Usage: "endpoint for the s3 connection",
EnvVars: []string{"PLUGIN_ENDPOINT", "S3_ENDPOINT"}, EnvVars: []string{"PLUGIN_ENDPOINT", "S3_ENDPOINT"},
Destination: &settings.Endpoint, Destination: &settings.Endpoint,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "access-key", Name: "access-key",
Usage: "s3 access key", Usage: "s3 access key",
EnvVars: []string{"PLUGIN_ACCESS_KEY", "S3_ACCESS_KEY"}, EnvVars: []string{"PLUGIN_ACCESS_KEY", "S3_ACCESS_KEY"},
Destination: &settings.AccessKey, Destination: &settings.AccessKey,
Required: true,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "secret-key", Name: "secret-key",
Usage: "s3 secret key", Usage: "s3 secret key",
EnvVars: []string{"PLUGIN_SECRET_KEY", "S3_SECRET_KEY"}, EnvVars: []string{"PLUGIN_SECRET_KEY", "S3_SECRET_KEY"},
Destination: &settings.SecretKey, Destination: &settings.SecretKey,
Required: true,
Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "path-style", Name: "path-style",
Usage: "use path style for bucket paths", Usage: "use path style for bucket paths",
EnvVars: []string{"PLUGIN_PATH_STYLE"}, EnvVars: []string{"PLUGIN_PATH_STYLE"},
Destination: &settings.PathStyle, Destination: &settings.PathStyle,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "bucket", Name: "bucket",
Usage: "name of the bucket", Usage: "name of the bucket",
EnvVars: []string{"PLUGIN_BUCKET"}, EnvVars: []string{"PLUGIN_BUCKET"},
Destination: &settings.Bucket, Destination: &settings.Bucket,
Required: true,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "region", Name: "region",
@ -44,6 +52,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Value: "us-east-1", Value: "us-east-1",
EnvVars: []string{"PLUGIN_REGION"}, EnvVars: []string{"PLUGIN_REGION"},
Destination: &settings.Region, Destination: &settings.Region,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "source", Name: "source",
@ -51,6 +60,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Value: ".", Value: ".",
EnvVars: []string{"PLUGIN_SOURCE"}, EnvVars: []string{"PLUGIN_SOURCE"},
Destination: &settings.Source, Destination: &settings.Source,
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "target", Name: "target",
@ -58,60 +68,70 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Value: "/", Value: "/",
EnvVars: []string{"PLUGIN_TARGET"}, EnvVars: []string{"PLUGIN_TARGET"},
Destination: &settings.Target, Destination: &settings.Target,
Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "delete", Name: "delete",
Usage: "delete locally removed files from the target", Usage: "delete locally removed files from the target",
EnvVars: []string{"PLUGIN_DELETE"}, EnvVars: []string{"PLUGIN_DELETE"},
Destination: &settings.Delete, Destination: &settings.Delete,
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "access", Name: "access",
Usage: "access control settings", Usage: "access control settings",
EnvVars: []string{"PLUGIN_ACCESS", "PLUGIN_ACL"}, EnvVars: []string{"PLUGIN_ACCESS", "PLUGIN_ACL"},
Value: &StringMapFlag{}, Value: &StringMapFlag{},
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "content-type", Name: "content-type",
Usage: "content-type settings for uploads", Usage: "content-type settings for uploads",
EnvVars: []string{"PLUGIN_CONTENT_TYPE"}, EnvVars: []string{"PLUGIN_CONTENT_TYPE"},
Value: &StringMapFlag{}, Value: &StringMapFlag{},
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "content-encoding", Name: "content-encoding",
Usage: "content-encoding settings for uploads", Usage: "content-encoding settings for uploads",
EnvVars: []string{"PLUGIN_CONTENT_ENCODING"}, EnvVars: []string{"PLUGIN_CONTENT_ENCODING"},
Value: &StringMapFlag{}, Value: &StringMapFlag{},
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "cache-control", Name: "cache-control",
Usage: "cache-control settings for uploads", Usage: "cache-control settings for uploads",
EnvVars: []string{"PLUGIN_CACHE_CONTROL"}, EnvVars: []string{"PLUGIN_CACHE_CONTROL"},
Value: &StringMapFlag{}, Value: &StringMapFlag{},
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "metadata", Name: "metadata",
Usage: "additional metadata for uploads", Usage: "additional metadata for uploads",
EnvVars: []string{"PLUGIN_METADATA"}, EnvVars: []string{"PLUGIN_METADATA"},
Value: &DeepStringMapFlag{}, Value: &DeepStringMapFlag{},
Category: category,
}, },
&cli.GenericFlag{ &cli.GenericFlag{
Name: "redirects", Name: "redirects",
Usage: "redirects to create", Usage: "redirects to create",
EnvVars: []string{"PLUGIN_REDIRECTS"}, EnvVars: []string{"PLUGIN_REDIRECTS"},
Value: &MapFlag{}, Value: &MapFlag{},
Category: category,
}, },
&cli.StringFlag{ &cli.StringFlag{
Name: "cloudfront-distribution", Name: "cloudfront-distribution",
Usage: "id of cloudfront distribution to invalidate", Usage: "id of cloudfront distribution to invalidate",
EnvVars: []string{"PLUGIN_CLOUDFRONT_DISTRIBUTION"}, EnvVars: []string{"PLUGIN_CLOUDFRONT_DISTRIBUTION"},
Destination: &settings.CloudFrontDistribution, Destination: &settings.CloudFrontDistribution,
Category: category,
}, },
&cli.BoolFlag{ &cli.BoolFlag{
Name: "dry-run", Name: "dry-run",
Usage: "dry run disables api calls", Usage: "dry run disables api calls",
EnvVars: []string{"DRY_RUN", "PLUGIN_DRY_RUN"}, EnvVars: []string{"DRY_RUN", "PLUGIN_DRY_RUN"},
Destination: &settings.DryRun, Destination: &settings.DryRun,
Category: category,
}, },
&cli.IntFlag{ &cli.IntFlag{
Name: "max-concurrency", Name: "max-concurrency",
@ -119,6 +139,7 @@ func settingsFlags(settings *plugin.Settings) []cli.Flag {
Value: 100, Value: 100,
EnvVars: []string{"PLUGIN_MAX_CONCURRENCY"}, EnvVars: []string{"PLUGIN_MAX_CONCURRENCY"},
Destination: &settings.MaxConcurrency, Destination: &settings.MaxConcurrency,
Category: category,
}, },
} }
} }

View File

@ -5,7 +5,7 @@ import (
"os" "os"
"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/thegeeklab/drone-s3-sync/plugin" "github.com/thegeeklab/drone-s3-sync/plugin"
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
@ -31,12 +31,12 @@ func main() {
Name: "drone-s3-sync", Name: "drone-s3-sync",
Usage: "synchronize a directory with an S3 bucket", Usage: "synchronize a directory with an S3 bucket",
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)
} }
} }
@ -58,20 +58,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 {
return fmt.Errorf("execution failed: %w", err)
if e, ok := err.(errors.ExitCoder); ok {
return e
}
return errors.ExitMessagef("execution failed: %w", err)
} }
return nil return nil

View File

@ -48,10 +48,6 @@ var MissingAwsValuesMessage = "Must set 'bucket'"
// Validate handles the settings validation of the plugin. // Validate handles the settings validation of the plugin.
func (p *Plugin) Validate() error { func (p *Plugin) Validate() error {
if len(p.settings.Bucket) == 0 {
return fmt.Errorf("no bucket name provided")
}
wd, err := os.Getwd() wd, err := os.Getwd()
if err != nil { if err != nil {
return fmt.Errorf("error while retrieving working directory: %w", err) return fmt.Errorf("error while retrieving working directory: %w", err)