From 16c2d7a57069bbe1c1099516e7a7d51e54ad41b5 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Thu, 19 Dec 2024 16:05:36 +1100 Subject: [PATCH] Updates to & from file commands --- cmd/sync.go | 72 +++++++++++++++++++------------------------ synchers/files.go | 11 ++++++- synchers/syncutils.go | 11 +++++-- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index 49b05c2..1fad6f6 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -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) @@ -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() { @@ -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 diff --git a/synchers/files.go b/synchers/files.go index f36aaac..f0068df 100644 --- a/synchers/files.go +++ b/synchers/files.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "reflect" + "regexp" "strconv" "time" @@ -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, } diff --git a/synchers/syncutils.go b/synchers/syncutils.go index f173686..dbc230c 100644 --- a/synchers/syncutils.go +++ b/synchers/syncutils.go @@ -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) @@ -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. @@ -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("%s@ssh.lagoon.amazeeio.cloud:%s", sourceEnvironment.GetOpenshiftProjectName(), sourceEnvironmentName) sourceEnvironmentName = fmt.Sprintf(":%s", sourceEnvironmentName)