diff --git a/main.go b/main.go index 7b123f5..1718564 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( + "fmt" "log/slog" "os" "os/exec" @@ -13,15 +14,26 @@ import ( var ( cliEndpoint = kingpin.Flag("endpoint", "Override command's default URL with the given URL").Envar("SKPR_S3_SYNC_ENDPOINT").String() cliExclude = kingpin.Flag("exclude", "Exclude paths from the list to be synced").Envar("SKPR_S3_SYNC_EXCLUDE").Default(".htaccess").String() - cliMode = kingpin.Flag("mode", "Mode which will used for syncing (sync or s3)").Envar("SKPR_S3_SYNC_MODE").Default("sync").String() + cliMode = kingpin.Flag("mode", "Mode which will used for syncing (sync or s3)").Envar("SKPR_S3_SYNC_MODE").Default(ModeSync).String() cliSource = kingpin.Arg("source", "Source files which are synced (local or S3 path)").Required().String() cliTarget = kingpin.Arg("target", "Target files which are synced (local or S3 path)").Required().String() ) +const ( + // ModeCP for copy only operations. + ModeCP = "cp" + // ModeSync for copy and syncing file operations. + ModeSync = "sync" +) + func main() { kingpin.Parse() - args := buildArgs(*cliEndpoint, *cliMode, *cliSource, *cliTarget, *cliExclude) + args, err := buildArgs(*cliEndpoint, *cliMode, *cliSource, *cliTarget, *cliExclude) + if err != nil { + slog.Error(err.Error()) + os.Exit(1) + } slog.Info("Starting sync", "args", strings.Join(args, " ")) @@ -39,15 +51,23 @@ func main() { } // Command which is compatible with the AWS S3 sync command line interface. -func buildArgs(endpoint, mode, source, target, exclude string) []string { +func buildArgs(endpoint, mode, source, target, exclude string) ([]string, error) { args := []string{"s3"} + if *cliMode != ModeCP && *cliMode != ModeSync { + return args, fmt.Errorf("mode not support: %s", mode) + } + if endpoint != "" { args = append(args, "--endpoint-url", endpoint) } args = append(args, mode) + if mode == ModeCP { + args = append(args, "--recursive") + } + if exclude != "" { for _, e := range strings.Split(exclude, ",") { args = append(args, "--exclude", e) @@ -56,5 +76,5 @@ func buildArgs(endpoint, mode, source, target, exclude string) []string { args = append(args, source, target) - return args + return args, nil }