~egrajeda/arkiv

eeb3729b1d5ccad78116582c3c0d72247ca23244 — Eduardo Grajeda 5 months ago cc8d544
feat: support logging rclone output to file
M arkiv-cli/arkiv.go => arkiv-cli/arkiv.go +3 -0
@@ 39,6 39,7 @@ type configuration struct {
	delete      bool
	recover     bool
	dryRun      bool
	logFile     string
	defaultArgs []string
}



@@ 51,6 52,7 @@ func main() {
	delete := flag.Bool("delete", false, "When synchronizing, delete files and directories as needed")
	recover := flag.Bool("recover", false, "Run the failure recovery mechanism if needed")
	dryRun := flag.Bool("dry-run", false, "Do a trial run with no permanent changes")
	logFile := flag.String("log-file", "", "Log everything to this file")
	doResync := flag.Bool("resync", false, "Synchronizes the state between path1 and path2")
	flag.Parse()



@@ 61,6 63,7 @@ func main() {
		delete:      *delete,
		recover:     *recover,
		dryRun:      *dryRun,
		logFile:     *logFile,
		defaultArgs: []string{"--drive-skip-gdocs"},
	}


M arkiv-cli/arkiv_bisyncOrSyncBasedOnStatus_test.go => arkiv-cli/arkiv_bisyncOrSyncBasedOnStatus_test.go +39 -0
@@ 113,6 113,24 @@ func Test_shouldRunBisync_AndDeleteFiles_OnlyWhenRequested(t *testing.T) {
	assert.Equal(t, []string{"bisync", "/local", "remote:", "--drive-skip-gdocs", "-v"}, runner.bisyncArgs)
}

func Test_shouldRunBisync_AndLogToFile(t *testing.T) {
	// Given
	runner := newRcloneRunnerBuilder().Build()
	configuration := configuration{
		path1:       "/local",
		path2:       "remote:",
		logFile:     "/log-file",
		defaultArgs: []string{"--drive-skip-gdocs"},
	}

	// When
	nextLastRunStatus := bisyncOrSyncBasedOnStatus(&runner, configuration, LastRunBisyncSucceeded)

	// Then
	assert.Equal(t, LastRunBisyncSucceeded, nextLastRunStatus)
	assert.Equal(t, []string{"bisync", "/local", "remote:", "--drive-skip-gdocs", "--max-delete", "0", "--log-file", "/log-file", "-v"}, runner.bisyncArgs)
}

func Test_shouldRunSync_OnlyWhenRecoverIsEnabled(t *testing.T) {
	// Given
	runner := newRcloneRunnerBuilder().Build()


@@ 236,6 254,27 @@ func Test_shouldRunSync_AndDeleteFiles_OnlyWhenRequested(t *testing.T) {
	assert.Equal(t, []string{"bisync", "--resync", "/local", "remote:", "--drive-skip-gdocs", "-v"}, runner.bisyncResyncArgs)
}

func Test_shouldRunSync_AndLogToFile(t *testing.T) {
	// Given
	runner := newRcloneRunnerBuilder().Build()
	configuration := configuration{
		path1:       "/local",
		path2:       "remote:",
		recover:     true,
		logFile:     "/log-file",
		defaultArgs: []string{"--drive-skip-gdocs"},
	}
	lastRunStatus := LastRunBisyncFailedWithCorruptedOnTransfer

	// When
	nextLastRunStatus := bisyncOrSyncBasedOnStatus(&runner, configuration, lastRunStatus)

	// Then
	assert.Equal(t, LastRunBisyncSucceeded, nextLastRunStatus)
	assert.Equal(t, []string{"sync", "/local", "remote:", "--drive-skip-gdocs", "--max-delete", "0", "--log-file", "/log-file", "-v"}, runner.syncArgs)
	assert.Equal(t, []string{"bisync", "--resync", "/local", "remote:", "--drive-skip-gdocs", "--max-delete", "0", "--log-file", "/log-file", "-v"}, runner.bisyncResyncArgs)
}

func Test_shouldNotRunSync_WhenRecoverIsDisabled(t *testing.T) {
	// Given
	runner := newRcloneRunnerBuilder().Build()

M arkiv-cli/arkiv_resync_test.go => arkiv-cli/arkiv_resync_test.go +18 -0
@@ 58,3 58,21 @@ func Test_shouldRunBisyncResync_AndDeleteFiles_OnlyWhenRequested(t *testing.T) {
	assert.Nil(t, err)
	assert.Equal(t, []string{"bisync", "--resync", "/local", "remote:", "--drive-skip-gdocs", "-v"}, runner.bisyncResyncArgs)
}

func Test_shouldRunBisyncResync_AndLogToFile(t *testing.T) {
	// Given
	runner := newRcloneRunnerBuilder().Build()
	configuration := configuration{
		path1:       "/local",
		path2:       "remote:",
		logFile:     "/log-file",
		defaultArgs: []string{"--drive-skip-gdocs"},
	}

	// When
	err := resync(&runner, configuration)

	// Then
	assert.Nil(t, err)
	assert.Equal(t, []string{"bisync", "--resync", "/local", "remote:", "--drive-skip-gdocs", "--max-delete", "0", "--log-file", "/log-file", "-v"}, runner.bisyncResyncArgs)
}

M arkiv-cli/arkiv_test.go => arkiv-cli/arkiv_test.go +1 -1
@@ 42,7 42,7 @@ func Test_bisyncOrSyncBasedOnCacheFile_shouldNotWriteLastRunStatus_WhenDryRun(t 
		dryRun: true,
	}
	pathDirectory, _ := os.MkdirTemp(os.TempDir(), "arkiv-test")
	path := path.Join(pathDirectory, "last-run")
	path := path.Join(pathDirectory, "last-run.json")

	// When
	lastRunStatus := bisyncOrSyncBasedOnCacheFile(&runner, configuration, path)

M arkiv-cli/rclone.go => arkiv-cli/rclone.go +3 -0
@@ 49,6 49,9 @@ func appendArgsFromConfiguration(args []string, configuration configuration) []s
	if configuration.dryRun {
		args = append(args, "--dry-run")
	}
	if configuration.logFile != "" {
		args = append(args, "--log-file", configuration.logFile)
	}
	args = append(args, "-v")

	return args