Log In  

Cart #util_merge-0 | 2024-03-17 | Embed ▽ | License: CC4-BY-NC-SA

This cartridge is an installer and uninstaller for a new globally-available command: 'merge'. You can install this by running this cartridge and pressing X as prompted, manually by copying /ram/cart/exports/appdata/system/util/new.lua to /appdata/system/util/new.lua, or by using my dependency and package manager 'yotta' to install this BBS cartridge ID as a util (yotta util install #util_merge-0).

The merge command will take a number of source directories and a single destination directory, and will recursively merge the source directories atop the destination directory. Normally, the system-provided cp command is not kind to attempts to do this - a cp src dst will result in dst being replaced by a copy of src. This, however, will attempt to intelligently place new source files into the destination directory tree without interfering with any of the remaining destination files.

It also has the ability to attempt an un-merge which will attempt to clean-up after itself if there are lingering empty directories after an unmerge attempt.

usage: merge [options] [src...] [dst]
 Recursively merges source directories into a destination
Options:
  -u|--unmerge - Will attempt to undo a merge, removing src files from dst
  -g|--gentle - Will make an in-place backup (file.ext.bak) before overwriting files
  -p|--paranoid - Will simply not overwrite files and will continue merging

Example: merge src_dir dst_dir - simple merge.
Example: merge -g src_dir dst_dir - simple merge but will rename, eg, /dst/file to /dst/file.bak if there is already a /dst/file but /src/file also exists.
Example: merge -p src_dir dst_dir - same as above, but instead of creating a backup it will simply NOT copy the /dst/file to /src/file and will move on.
Example: merge -u src_dir dst_dir will attempt to remove all the files that appear to be merged from src_dir from dst_dir. I say attempt because if you, say, ran a paranoid merge above and then ran this, it would remove the originals from dst_dir!! So, be careful with this, but it's still quite useful in my opinion.

Use as a library

This utility script can also be included by other lua files without executing immediately in order to provide its merge and unmerge functions. Simply include the file in your code (include "/appdata/system/util/merge.lua") and two functions will become available for your use. Just... don't name your own lua file merge.lua. It's kind of dumb, but my merge.lua is looking at the currently running script's basename to determine if it should be a command or a library. Sorry.

merge(string src_path, string dst_path, int gentleness = 0)

Merges source path over the destination path. Gentleness = 0 is equivalent to running the command without -p or -g. Gentleness = 1 is equivalent to running the command with -g. Gentleness = 2 is equivalent to running the command with -p.

The merge function will return a table in the following format that outlines the new files and folders within the destination directory that were created:

{
  files = {"/created/a/file.txt", "/another/created/file.pod"},
  dirs = {"/created/a"}
}

unmerge(string src_path, string dst_path)

Attempts to unmerge a source path from a destination path. If it leaves empty directories that it had to create during the merge process, it will attempt to remove these directories on its way out, to hopefully leave your destination just as it was before.

The unmerge function will, similarly to the merge function, return a table in the following format that outlines the destination directory files removed and folders that were pruned due to being empty after file removal:

{
  files = {"/baleeted/a/file.txt", "/another/removed/file.pod"},
  dirs = {"/baleeted/a"}
}
P#143595 2024-03-17 23:46


[Please log in to post a comment]