2022-05-03 09:45:54 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2024-05-11 09:08:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-05-18 08:01:22 +00:00
|
|
|
plugin_base "github.com/thegeeklab/wp-plugin-go/v3/plugin"
|
|
|
|
plugin_types "github.com/thegeeklab/wp-plugin-go/v3/types"
|
2024-05-11 09:08:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2022-05-03 09:45:54 +00:00
|
|
|
)
|
|
|
|
|
2024-05-11 09:08:34 +00:00
|
|
|
//go:generate go run ../internal/doc/main.go -output=../docs/data/data-raw.yaml
|
|
|
|
|
2023-08-19 14:00:02 +00:00
|
|
|
// Plugin implements provide the plugin implementation.
|
2022-05-03 09:45:54 +00:00
|
|
|
type Plugin struct {
|
2024-05-18 08:01:22 +00:00
|
|
|
*plugin_base.Plugin
|
2023-08-19 14:00:02 +00:00
|
|
|
Settings *Settings
|
2022-05-03 09:45:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:00:02 +00:00
|
|
|
// Settings for the Plugin.
|
|
|
|
type Settings struct {
|
|
|
|
Endpoint string
|
|
|
|
AccessKey string
|
|
|
|
SecretKey string
|
|
|
|
Bucket string
|
|
|
|
Region string
|
|
|
|
Source string
|
|
|
|
Target string
|
|
|
|
Delete bool
|
2023-08-21 10:50:44 +00:00
|
|
|
ACL map[string]string
|
|
|
|
CacheControl map[string]string
|
|
|
|
ContentType map[string]string
|
|
|
|
ContentEncoding map[string]string
|
|
|
|
Metadata map[string]map[string]string
|
|
|
|
Redirects map[string]string
|
2023-08-19 14:00:02 +00:00
|
|
|
CloudFrontDistribution string
|
|
|
|
DryRun bool
|
|
|
|
PathStyle bool
|
|
|
|
Jobs []Job
|
|
|
|
MaxConcurrency int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Job struct {
|
|
|
|
local string
|
|
|
|
remote string
|
|
|
|
action string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
j Job
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2024-05-18 08:01:22 +00:00
|
|
|
func New(e plugin_base.ExecuteFunc, build ...string) *Plugin {
|
2024-05-11 09:08:34 +00:00
|
|
|
p := &Plugin{
|
|
|
|
Settings: &Settings{},
|
|
|
|
}
|
|
|
|
|
2024-05-18 08:01:22 +00:00
|
|
|
options := plugin_base.Options{
|
2024-05-11 09:08:34 +00:00
|
|
|
Name: "wp-s3-action",
|
|
|
|
Description: "Perform S3 actions",
|
2024-05-18 08:01:22 +00:00
|
|
|
Flags: Flags(p.Settings, plugin_base.FlagsPluginCategory),
|
2024-05-11 09:08:34 +00:00
|
|
|
Execute: p.run,
|
|
|
|
HideWoodpeckerFlags: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(build) > 0 {
|
|
|
|
options.Version = build[0]
|
|
|
|
}
|
2023-08-19 14:00:02 +00:00
|
|
|
|
2024-05-11 09:08:34 +00:00
|
|
|
if len(build) > 1 {
|
|
|
|
options.VersionMetadata = fmt.Sprintf("date=%s", build[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
options.Execute = e
|
|
|
|
}
|
2023-08-19 14:00:02 +00:00
|
|
|
|
2024-05-18 08:01:22 +00:00
|
|
|
p.Plugin = plugin_base.New(options)
|
2023-08-19 14:00:02 +00:00
|
|
|
|
|
|
|
return p
|
2022-05-03 09:45:54 +00:00
|
|
|
}
|
2024-05-11 09:08:34 +00:00
|
|
|
|
|
|
|
// Flags returns a slice of CLI flags for the plugin.
|
|
|
|
func Flags(settings *Settings, category string) []cli.Flag {
|
|
|
|
return []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "endpoint",
|
|
|
|
Usage: "endpoint for the s3 connection",
|
|
|
|
EnvVars: []string{"PLUGIN_ENDPOINT", "S3_ENDPOINT"},
|
|
|
|
Destination: &settings.Endpoint,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "access-key",
|
|
|
|
Usage: "s3 access key",
|
|
|
|
EnvVars: []string{"PLUGIN_ACCESS_KEY", "S3_ACCESS_KEY"},
|
|
|
|
Destination: &settings.AccessKey,
|
|
|
|
Required: true,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "secret-key",
|
|
|
|
Usage: "s3 secret key",
|
|
|
|
EnvVars: []string{"PLUGIN_SECRET_KEY", "S3_SECRET_KEY"},
|
|
|
|
Destination: &settings.SecretKey,
|
|
|
|
Required: true,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "path-style",
|
|
|
|
Usage: "enable path style for bucket paths",
|
|
|
|
EnvVars: []string{"PLUGIN_PATH_STYLE"},
|
|
|
|
Destination: &settings.PathStyle,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "bucket",
|
|
|
|
Usage: "name of the bucket",
|
|
|
|
EnvVars: []string{"PLUGIN_BUCKET"},
|
|
|
|
Destination: &settings.Bucket,
|
|
|
|
Required: true,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "region",
|
|
|
|
Usage: "s3 region",
|
|
|
|
Value: "us-east-1",
|
|
|
|
EnvVars: []string{"PLUGIN_REGION"},
|
|
|
|
Destination: &settings.Region,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "source",
|
|
|
|
Usage: "upload source path",
|
|
|
|
Value: ".",
|
|
|
|
EnvVars: []string{"PLUGIN_SOURCE"},
|
|
|
|
Destination: &settings.Source,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "target",
|
|
|
|
Usage: "upload target path",
|
|
|
|
Value: "/",
|
|
|
|
EnvVars: []string{"PLUGIN_TARGET"},
|
|
|
|
Destination: &settings.Target,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "delete",
|
|
|
|
Usage: "delete locally removed files from the target",
|
|
|
|
EnvVars: []string{"PLUGIN_DELETE"},
|
|
|
|
Destination: &settings.Delete,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "acl",
|
|
|
|
Usage: "access control list",
|
|
|
|
EnvVars: []string{"PLUGIN_ACL"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.StringMapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "content-type",
|
|
|
|
Usage: "content-type settings for uploads",
|
|
|
|
EnvVars: []string{"PLUGIN_CONTENT_TYPE"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.StringMapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "content-encoding",
|
|
|
|
Usage: "content-encoding settings for uploads",
|
|
|
|
EnvVars: []string{"PLUGIN_CONTENT_ENCODING"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.StringMapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "cache-control",
|
|
|
|
Usage: "cache-control settings for uploads",
|
|
|
|
EnvVars: []string{"PLUGIN_CACHE_CONTROL"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.StringMapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "metadata",
|
|
|
|
Usage: "additional metadata for uploads",
|
|
|
|
EnvVars: []string{"PLUGIN_METADATA"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.DeepStringMapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.GenericFlag{
|
|
|
|
Name: "redirects",
|
|
|
|
Usage: "redirects to create",
|
|
|
|
EnvVars: []string{"PLUGIN_REDIRECTS"},
|
2024-05-18 08:01:22 +00:00
|
|
|
Value: &plugin_types.MapFlag{},
|
2024-05-11 09:08:34 +00:00
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "cloudfront-distribution",
|
|
|
|
Usage: "ID of cloudfront distribution to invalidate",
|
|
|
|
EnvVars: []string{"PLUGIN_CLOUDFRONT_DISTRIBUTION"},
|
|
|
|
Destination: &settings.CloudFrontDistribution,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "dry-run",
|
|
|
|
Usage: "dry run disables api calls",
|
|
|
|
EnvVars: []string{"DRY_RUN", "PLUGIN_DRY_RUN"},
|
|
|
|
Destination: &settings.DryRun,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "max-concurrency",
|
|
|
|
Usage: "customize number concurrent files to process",
|
2024-05-12 09:08:28 +00:00
|
|
|
//nolint:mnd
|
2024-05-11 09:08:34 +00:00
|
|
|
Value: 100,
|
|
|
|
EnvVars: []string{"PLUGIN_MAX_CONCURRENCY"},
|
|
|
|
Destination: &settings.MaxConcurrency,
|
|
|
|
Category: category,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|