Skip to content

Commit

Permalink
add s3_sync command
Browse files Browse the repository at this point in the history
  • Loading branch information
asteel-gsa committed May 14, 2024
1 parent fb66a0c commit 24facce
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
61 changes: 61 additions & 0 deletions cmd/s3_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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")
}
27 changes: 27 additions & 0 deletions internal/pipes/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pipes
import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/bitfield/script"
Expand Down Expand Up @@ -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)

}

0 comments on commit 24facce

Please sign in to comment.