Skip to content

Commit

Permalink
Updates to & from file commands
Browse files Browse the repository at this point in the history
  • Loading branch information
CGoodwin90 committed Dec 19, 2024
1 parent 6e5a94e commit 16c2d7a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
72 changes: 32 additions & 40 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
RsyncArgs: RsyncArguments,
}

// let's update the named transfer resource if it is set
if namedTransferResource != "" {
// let's update the named transfer resource if it is set & not syncing to/from a file
if namedTransferResource != "" && !strings.Contains(cmd.Name(), "-file") {
err = lagoonSyncer.SetTransferResource(namedTransferResource)
if err != nil {
utils.LogFatalError(err.Error(), nil)
Expand Down Expand Up @@ -222,45 +222,37 @@ func confirmPrompt(message string) (bool, error) {
return false, err
}

func addToFileSubCommand(parentCmd *cobra.Command) {
var toFileCmd = &cobra.Command{
Use: "to-file",
Short: "Sync to a file",
Long: "Sync/Dump to a file to produce a resource dump",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Parent().PersistentFlags().GetString("transfer-resource-name")
fmt.Println("You are about to dump to:", fileFlag)

// set the skipTargetImport and skipTargetCleanup flags to true
cmd.Parent().PersistentFlags().Set("skip-target-import", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

syncCommandRun(cmd, args)
},
}

parentCmd.AddCommand(toFileCmd)
var toFileCmd = &cobra.Command{
Use: "to-file",
Short: "Sync to a file",
Long: "Sync/Dump to a file to produce a resource dump",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Flags().GetString("transfer-resource-name")
fmt.Println("You are about to dump to:", fileFlag)

// set the skipTargetImport and skipTargetCleanup flags to true
cmd.Parent().PersistentFlags().Set("skip-target-import", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

syncCommandRun(cmd, args)
},
}

func addFromFileSubCommand(parentCmd *cobra.Command) {
var fromFileCmd = &cobra.Command{
Use: "from-file",
Short: "Sync from a file",
Long: "Sync from a file and perform related actions",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Parent().PersistentFlags().GetString("transfer-resource-name")
fmt.Println("You are about to import from:", fileFlag)

cmd.Parent().PersistentFlags().Set("skip-source-run", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

syncCommandRun(cmd, args)
},
}
var fromFileCmd = &cobra.Command{
Use: "from-file",
Short: "Sync from a file",
Long: "Sync from a file and perform related actions",
Run: func(cmd *cobra.Command, args []string) {
fileFlag, _ := cmd.Flags().GetString("transfer-resource-name")
fmt.Println("You are about to import from:", fileFlag)

cmd.Parent().PersistentFlags().Set("skip-source-run", "true")
cmd.Parent().PersistentFlags().Set("skip-source-cleanup", "true")
cmd.Parent().PersistentFlags().Set("skip-target-cleanup", "true")

parentCmd.AddCommand(fromFileCmd)
syncCommandRun(cmd, args)
},
}

func init() {
Expand All @@ -284,8 +276,8 @@ func init() {
syncCmd.PersistentFlags().BoolVar(&skipTargetImport, "skip-target-import", false, "This will skip the import step on the target, in combination with 'no-target-cleanup' this essentially produces a resource dump")
syncCmd.PersistentFlags().StringVarP(&namedTransferResource, "transfer-resource-name", "f", "", "The name of the temporary file to be used to transfer generated resources (db dumps, etc) - random /tmp file otherwise")

addToFileSubCommand(syncCmd)
addFromFileSubCommand(syncCmd)
syncCmd.AddCommand(toFileCmd)
syncCmd.AddCommand(fromFileCmd)

// By default, we hook up the syncers.RunSyncProcess function to the runSyncProcess variable
// by doing this, it lets us easily override it for testing the command - but for most of the time
Expand Down
11 changes: 10 additions & 1 deletion synchers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"time"

Expand Down Expand Up @@ -111,12 +112,20 @@ func (m *FilesSyncRoot) GetFilesToCleanup(environment Environment) []string {

func (m *FilesSyncRoot) GetTransferResource(environment Environment) SyncerTransferResource {
config := m.Config
isDirectory := true
if environment.EnvironmentName == LOCAL_ENVIRONMENT_NAME {
config = m.getEffectiveLocalDetails()
}
isFile, err := regexp.MatchString(`^(?:\.|~)?\/?[\w\/\-\.]*\.\w+$`, config.SyncPath)
if err != nil {
log.Fatalf("Error while matching file path: %v", err)
}
if isFile {
isDirectory = false
}
return SyncerTransferResource{
Name: fmt.Sprintf(config.SyncPath),
IsDirectory: true,
IsDirectory: isDirectory,
SkipCleanup: true,
ExcludeResources: m.Config.Exclude,
}
Expand Down
11 changes: 9 additions & 2 deletions synchers/syncutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RunSyncProcess(args RunSyncProcessFunctionTypeArguments) error {
return err
}

err = SyncRunTransfer(args.SourceEnvironment, args.TargetEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
err = SyncRunTransfer(args.SourceEnvironment, args.TargetEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions, args.TransferResourceName)
if err != nil {
_ = PrerequisiteCleanUp(args.SourceEnvironment, sourceRsyncPath, args.DryRun, args.SshOptions)
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
Expand Down Expand Up @@ -148,7 +148,7 @@ func SyncRunSourceCommand(remoteEnvironment Environment, syncer Syncer, dryRun b
return nil
}

func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environment, syncer Syncer, dryRun bool, sshOptions SSHOptions) error {
func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environment, syncer Syncer, dryRun bool, sshOptions SSHOptions, transferResourceName string) error {
utils.LogProcessStep("Beginning file transfer logic", nil)

// If we're transferring to the same resource, we can skip this whole process.
Expand Down Expand Up @@ -180,6 +180,13 @@ func SyncRunTransfer(sourceEnvironment Environment, targetEnvironment Environmen
// rsyncRemoteSystemUsername is used by the rsync command to set up the ssh tunnel
rsyncRemoteSystemUsername := ""

// checks transferResourceName is provided for a file sync
if transferResourceName != "" {
if _, ok := syncer.(*FilesSyncRoot); ok {
sourceEnvironmentName = transferResourceName
}
}

if sourceEnvironment.EnvironmentName != LOCAL_ENVIRONMENT_NAME {
//sourceEnvironmentName = fmt.Sprintf("%[email protected]:%s", sourceEnvironment.GetOpenshiftProjectName(), sourceEnvironmentName)
sourceEnvironmentName = fmt.Sprintf(":%s", sourceEnvironmentName)
Expand Down

0 comments on commit 16c2d7a

Please sign in to comment.