Skip to content

Commit f1ca82f

Browse files
committed
fix: Introduce NewListCommand, NewRSyncSimulateCommand
1 parent 86f72a8 commit f1ca82f

File tree

5 files changed

+40
-34
lines changed

5 files changed

+40
-34
lines changed

backup/cmd/list.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ func buildListCommand() *cobra.Command {
1414
configPath, _ := cmd.Flags().GetString("config")
1515
rsyncPath, _ := cmd.Flags().GetString("rsync-path")
1616
cfg := internal.LoadResolvedConfig(configPath)
17-
command := internal.NewRSyncCommand(rsyncPath)
18-
command.ListOnly = true
17+
command := internal.NewListCommand(rsyncPath)
1918

2019
cfg.Apply(command)
2120
},

backup/cmd/simulate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ func buildSimulateCommand() *cobra.Command {
1515
rsyncPath, _ := cmd.Flags().GetString("rsync-path")
1616

1717
cfg := internal.LoadResolvedConfig(configPath)
18-
command := internal.NewRSyncCommand(rsyncPath)
19-
command.Simulate = true
18+
command := internal.NewRSyncSimulateCommand(rsyncPath)
2019

2120
cfg.Apply(command)
2221
},

backup/internal/job.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
11
package internal
22

3-
import (
4-
"fmt"
5-
"strings"
6-
)
7-
83
func (job Job) Apply(rsync RSyncCommand, logPath string) string {
94
if !job.Enabled {
105
return "SKIPPED"
116
}
127

13-
args := rsync.ArgumentsForJob(job, logPath)
14-
fmt.Printf("Job: %s\n", job.Name)
15-
fmt.Printf("Command: rsync %s %s\n", rsync.BinPath, strings.Join(args, " "))
16-
17-
if rsync.ListOnly {
18-
return "SUCCESS"
19-
}
20-
21-
out, err := rsync.Run(args...)
22-
fmt.Printf("Output:\n%s\n", string(out))
23-
24-
if err != nil {
25-
return "FAILURE"
26-
}
27-
28-
return "SUCCESS"
8+
return rsync.Run(job, logPath)
299
}

backup/internal/rsync.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,30 @@ func NewRSyncCommand(binPath string) RSyncCommand {
2424
}
2525
}
2626

27+
func NewRSyncSimulateCommand(binPath string) RSyncCommand {
28+
return RSyncCommand{
29+
BinPath: binPath,
30+
Simulate: true,
31+
Executor: &RealSync{},
32+
}
33+
}
34+
35+
func NewListCommand(binPath string) RSyncCommand {
36+
return RSyncCommand{
37+
BinPath: binPath,
38+
ListOnly: true,
39+
Executor: &RealSync{},
40+
}
41+
}
42+
2743
func (command RSyncCommand) GetVersionInfo() (string, error) {
2844
rsyncPath := command.BinPath
2945

3046
if !filepath.IsAbs(rsyncPath) {
3147
return "", fmt.Errorf("%w: \"%s\"", ErrInvalidRsyncPath, rsyncPath)
3248
}
3349

34-
output, err := command.Run("--version")
50+
output, err := command.Executor.Execute("--version")
3551
if err != nil {
3652
return "", fmt.Errorf("error fetching rsync version: %w", err)
3753
}
@@ -44,7 +60,7 @@ func (command RSyncCommand) GetVersionInfo() (string, error) {
4460
return string(output), nil
4561
}
4662

47-
func (command RSyncCommand) ArgumentsForJob(job Job, logPath string) []string {
63+
func ArgumentsForJob(job Job, logPath string, simulate bool) []string {
4864
args := []string{"-aiv", "--stats"}
4965
if job.Delete {
5066
args = append(args, "--delete")
@@ -59,13 +75,28 @@ func (command RSyncCommand) ArgumentsForJob(job Job, logPath string) []string {
5975
}
6076

6177
args = append(args, job.Source, job.Target)
62-
if command.Simulate {
78+
if simulate {
6379
args = append([]string{"--dry-run"}, args...)
6480
}
6581

6682
return args
6783
}
6884

69-
func (command RSyncCommand) Run(args ...string) ([]byte, error) {
70-
return command.Executor.Execute(command.BinPath, args...)
85+
func (rsync RSyncCommand) Run(job Job, logPath string) string {
86+
args := ArgumentsForJob(job, logPath, rsync.Simulate)
87+
fmt.Printf("Job: %s\n", job.Name)
88+
fmt.Printf("Command: rsync %s %s\n", rsync.BinPath, strings.Join(args, " "))
89+
90+
if rsync.ListOnly {
91+
return "SUCCESS"
92+
}
93+
94+
out, err := rsync.Executor.Execute(rsync.BinPath, args...)
95+
fmt.Printf("Output:\n%s\n", string(out))
96+
97+
if err != nil {
98+
return "FAILURE"
99+
}
100+
101+
return "SUCCESS"
71102
}

backup/internal/test/rsync_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ func TestArgumentsForJob(t *testing.T) {
2020
WithTarget("/target/user/music/home"),
2121
WithExclusions([]string{"*.tmp", "node_modules/"}),
2222
)
23-
command := internal.RSyncCommand{
24-
Simulate: true,
25-
}
26-
args := command.ArgumentsForJob(job, "")
23+
args := internal.ArgumentsForJob(job, "", true)
2724

2825
expectedArgs := []string{
2926
"--dry-run", "-aiv", "--stats", "--delete",

0 commit comments

Comments
 (0)