Skip to content

Commit

Permalink
to-file and from-file sub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Clifford committed Sep 6, 2023
1 parent b93904b commit 6e5a94e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 7 deletions.
64 changes: 61 additions & 3 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var dryRun bool
var verboseSSH bool
var RsyncArguments string
var runSyncProcess synchers.RunSyncProcessFunctionType
var skipSourceRun bool
var skipSourceCleanup bool
var skipTargetCleanup bool
var skipTargetImport bool
Expand All @@ -46,8 +47,19 @@ var syncCmd = &cobra.Command{
}

func syncCommandRun(cmd *cobra.Command, args []string) {
SyncerType := args[0]
viper.Set("syncer-type", args[0])
SyncerType := ""
if len(args) > 0 {
SyncerType = args[0]
viper.Set("syncer-type", args[0])
} else {
if cmd.Name() == "to-file" || cmd.Name() == "from-file" {
// set default as mariadb for now if no arg is given
SyncerType = "mariadb"
} else {
fmt.Println("Error: No SyncerType provided.")
return
}
}

var configRoot synchers.SyncherConfigRoot

Expand Down Expand Up @@ -175,6 +187,7 @@ func syncCommandRun(cmd *cobra.Command, args []string) {
SyncerType: SyncerType,
DryRun: dryRun,
SshOptions: sshOptions,
SkipSourceRun: skipSourceRun,
SkipTargetCleanup: skipTargetCleanup,
SkipSourceCleanup: skipSourceCleanup,
SkipTargetImport: skipTargetImport,
Expand Down Expand Up @@ -209,6 +222,47 @@ 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)
}

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)
},
}

parentCmd.AddCommand(fromFileCmd)
}

func init() {
rootCmd.AddCommand(syncCmd)
syncCmd.PersistentFlags().StringVarP(&ProjectName, "project-name", "p", "", "The Lagoon project name of the remote system")
Expand All @@ -224,10 +278,14 @@ func init() {
syncCmd.PersistentFlags().BoolVar(&noCliInteraction, "no-interaction", false, "Disallow interaction")
syncCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Don't run the commands, just preview what will be run")
syncCmd.PersistentFlags().StringVarP(&RsyncArguments, "rsync-args", "r", "--omit-dir-times --no-perms --no-group --no-owner --chmod=ugo=rwX --recursive --compress", "Pass through arguments to change the behaviour of rsync")
syncCmd.PersistentFlags().BoolVar(&skipSourceRun, "skip-source-run", false, "Don't run any ops on the source")
syncCmd.PersistentFlags().BoolVar(&skipSourceCleanup, "skip-source-cleanup", false, "Don't clean up any of the files generated on the source")
syncCmd.PersistentFlags().BoolVar(&skipTargetCleanup, "skip-target-cleanup", false, "Don't clean up any of the files generated on the target")
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", "", "", "The name of the temporary file to be used to transfer generated resources (db dumps, etc) - random /tmp file otherwise")
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)

// 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: 7 additions & 4 deletions synchers/syncutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type RunSyncProcessFunctionTypeArguments struct {
SyncerType string
DryRun bool
SshOptions SSHOptions
SkipSourceRun bool
SkipSourceCleanup bool
SkipTargetCleanup bool
SkipTargetImport bool
Expand All @@ -57,10 +58,12 @@ func RunSyncProcess(args RunSyncProcessFunctionTypeArguments) error {
return err
}

err = SyncRunSourceCommand(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
if err != nil {
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
return err
if !args.SkipSourceRun {
err = SyncRunSourceCommand(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
if err != nil {
_ = SyncCleanUp(args.SourceEnvironment, args.LagoonSyncer, args.DryRun, args.SshOptions)
return err
}
}

args.TargetEnvironment, err = RunPrerequisiteCommand(args.TargetEnvironment, args.LagoonSyncer, args.SyncerType, args.DryRun, args.SshOptions)
Expand Down

0 comments on commit 6e5a94e

Please sign in to comment.