diff --git a/cmd/s3_sync.go b/cmd/s3_sync.go new file mode 100644 index 0000000..54c42dc --- /dev/null +++ b/cmd/s3_sync.go @@ -0,0 +1,61 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" + "gov.gsa.fac.cgov-util/internal/pipes" + "gov.gsa.fac.cgov-util/internal/structs" + "gov.gsa.fac.cgov-util/internal/util" +) + +var ( + source_s3 string + dest_s3 string +) + +// s3SyncCmd represents the s3Sync command +var s3SyncCmd = &cobra.Command{ + Use: "s3_sync", + Short: "Syncs two buckets together", + Long: `Uses aws s3 sync to sync two buckets contents.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("s3_sync called") + util.Unset_Proxy() + source_s3 := parseS3Path(source_s3) + dest_s3 := parseS3Path(dest_s3) + source_creds := getBucketCredentials(source_s3) + dest_creds := getBucketCredentials(dest_s3) + + ch := structs.Choice{ + Local: func() { + pipes.S3Sync(source_creds, dest_creds) + }, + Remote: func() { + pipes.S3Sync(source_creds, dest_creds) + }} + runLocalOrRemote(ch) + }, +} + +func init() { + rootCmd.AddCommand(s3SyncCmd) + s3SyncCmd.Flags().StringVarP(&source_s3, "source_s3", "", "", "Source Bucket") + s3SyncCmd.Flags().StringVarP(&dest_s3, "dest_s3", "", "", "Destination Bucket") + + //s3SyncCmd.PersistentFlags().String("source_s3", "", "Source Bucket. (s3://fac-private-s3)") + //s3SyncCmd.PersistentFlags().String("dest_s3", "", "Destination Bucket. (s3://backups)") + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // s3SyncCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // s3SyncCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/internal/pipes/s3.go b/internal/pipes/s3.go index 382a741..73d8eac 100644 --- a/internal/pipes/s3.go +++ b/internal/pipes/s3.go @@ -3,6 +3,7 @@ package pipes import ( "fmt" "os" + "os/exec" "strings" "github.com/bitfield/script" @@ -68,3 +69,29 @@ func S3Read(s3_creds vcap.Credentials, } return script.Exec(combined) } + +func S3Sync(source_creds vcap.Credentials, + dest_creds vcap.Credentials) { + + os.Setenv("AWS_SECRET_ACCESS_KEY", source_creds.Get("secret_access_key").String()) + os.Setenv("AWS_ACCESS_KEY_ID", source_creds.Get("access_key_id").String()) + os.Setenv("AWS_DEFAULT_REGION", source_creds.Get("region").String()) + cmd := []string{ + util.AWS_path, + "s3", + "sync", + fmt.Sprintf("s3://%s/", + source_creds.Get("bucket").String(), + ), + fmt.Sprintf("s3://%s/", + dest_creds.Get("bucket").String(), + ), + } + combined := strings.Join(cmd[:], " ") + logging.Logger.Printf("S3 Syncing " + source_creds.Get("bucket").String() + " to " + dest_creds.Get("bucket").String()) + logging.Logger.Printf("Running command: " + combined) + sync := exec.Command("bash", "-c", combined) + syncOutput, syncError := sync.Output() + util.ErrorCheck(string(syncOutput), syncError) + +}