Skip to content

Instantly share code, notes, and snippets.

@ZeroDragon
Created October 14, 2020 00:03
Show Gist options
  • Save ZeroDragon/b6aa2747f5a10055d6929653acf61e01 to your computer and use it in GitHub Desktop.
Save ZeroDragon/b6aa2747f5a10055d6929653acf61e01 to your computer and use it in GitHub Desktop.
Rclone Diff generator

Will create a display with all different files from one directory to another

< indicates that a file only exists in the first file
> indicates that a file only exists in the second file

Usage

node diff.js drive:sub/dir drive2:sub/dir

Will output dif between directories divided by subdirectory Will also create and return a tmp diff file that you can call directly using

node diff.js [tmpdifffile]

(no brackets)

Requeriments

You will need rclone setup and both your destinations configured

const fs = require('fs')
const { spawn } = require('child_process')
const [,,from, to] = process.argv
const UUID = () => {
var dt = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (dt + Math.random()*16)%16 | 0;
dt = Math.floor(dt/16);
return (c=='x' ? r :(r&0x3|0x8)).toString(16);
});
return uuid;
}
const [tmp1, tmp2, tmp3] = [UUID(), UUID(), UUID()]
let command = [
`rclone lsf -R "${from}" | sort > /tmp/${tmp1}`,
`rclone lsf -R "${to}" | sort > /tmp/${tmp2}`,
`diff /tmp/${tmp1} /tmp/${tmp2} > /tmp/${tmp3}`
].join('; ')
const looker = () => {
const exec = spawn('sh', ['-c', command])
exec.stderr.on('data', data => {
console.log(data.toString())
throw new Error('something went bad')
})
exec.stdout.on('close', _ => {
reporter()
})
}
const reporter = (dif = tmp3) => {
const file = fs.readFileSync(`/tmp/${dif}`, {encoding: 'utf8'})
const lines = file.split('
')
.map(line => line.split('/'))
.map(([head, tail]) => [...head.split(' '), tail])
const directories = {}
lines.forEach(([action, directory, game]) => {
if (!directory) return
if (!directories[directory]) directories[directory] = {}
const dir = directories[directory]
if (!dir[action]) dir[action] = 0
dir[action] += 1
})
console.table(directories)
console.log('temp diff saved to', `${tmp3}`)
}
if (!to) reporter(from)
else looker()
@ZeroDragon
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment