Switchboard

Do you ever get annoyed that your Downloads folder gets cluttered with all types of files and folders? Are your images scattered across various places on your machine and you just wish they could be organised into one place?

Switchboard is a tool here to help you organise files on your machine.

Motivation

As aptly described above, I built switchboard out of a frustration with the sporadic mess of files strewn throughout my machines and network servers.

In my particular case - I use Kubernetes running a couple of raspberry Pis which hosts deluge, a torrent download client. I have another Pi in turn which specifically runs Open Media Vault and Plex, for NFS and file sharing. Now, my problem, is that Deluge will explicitly download all file types to a downloads folder on my NFS - which means I have to ssh into my Pi NAS every time I (legally) download a video, and move it across to my videos folder so as Plex can pick it up and sync.

This can get tedious. So, to fix it, I built Switchboard.

How it works

Switchboard works by monitoring a directory you provide (or list of directories using a configuration file), and utilizes file system notifications to move a matched file to the appropriate destination directory of your choosing.

Under the hood, I use fsnotify which is a handy package to allow for cross-platform file system notifications. However, the cross-platform nature of the package has some limitations. For instance, for Windows and MacOS systems there is no concept of a IN_CLOSE_WRITE event as there is in Linux. Allow me to explain why this can be a problem with a simple example.

Imagine you are in the process of downloading a rather large file to your systems Downloads folder. We can use the Ubuntu 20.04 LTS ISO as our example. What will happen when you click that download button and save the file to your Downloads folder? A CREATE event will be sent, followed by susequent WRITE events to the file as it downloads. But, how do we know the file has finished being written to?

How do we know when a file has finished being writtten to?

Here in lies the crux of the problem outside of Linux. So, in order to mitigate this switchboard implements a polling solution. Events will be added to a queue, wherein a poller will check every x number of seconds when the file was last modified. If the file was modified within the last x number of seconds, we will continue polling, otherwise, switchboard will assume the operation has completed, and the file is valid.

This obviously does not cover all scenarios, for example, you might be downloading a file and halfway through you may loose internet connectivity. In this instance, the file will be moved, and may very well be invalid. That being said, I believe this change will greatly improve switchboard compared to how it currently stands.

Installation

You can install switchboard pre-compiled binary in a number of ways.

Homebrew
brew tap Cian911/switchboard
brew install switchboard

// Check everything is working as it should be
switchboard -h

You can also upgrade the version of switchboard you already have installed by doing the following.

brew upgrade switchboard
Docker
docker pull ghcr.io/cian911/switchboard:${VERSION}

docker run -d -v ${SRC} -v ${DEST} ghcr.io/cian911/switchboard:${VERSION} watch -h
Go Install
go install github.com/Cian911/switchboard@${VERSION}
Manually

You can download the pre-compiled binary for your specific OS type from the OSS releases page. You will need to copy these and extract the binary, then move it to you local bin directory. See the example below for extracting a zipped version.

curl https://github.com/Cian911/switchboard/releases/download/${VERSION}/${PACKAGE_NAME} -o ${PACKAGE_NAME}
sudo tar -xvf ${PACKAGE_NAME} -C /usr/local/bin/
sudo chmod +x /usr/local/bin/switchboard

Quick Start

Using switchboard is pretty easy. Below lists the set of commands and flags you can pass in.

Run the switchboard application passing in the path, destination, and file type you'd like to watch for.

Usage:
   watch [flags]

Flags:
      --config string        Pass an optional config file containing multiple paths to watch.
  -d, --destination string   Path you want files to be relocated.
  -e, --ext string           File type you want to watch for.
  -h, --help                 help for watch
  -p, --path string          Path you want to watch.
      --poll int             Specify a polling time in seconds. (default 60)

To get started quickly, you can run the following command, passing in the path, destination, and file extenstion you want to watch for. See the example below.

switchboard watch -p /home/user/Downloads -d /home/user/Movies -e .mp4

Future Plans

Linux

Some progress has already been made towards a new release which will negate the need for any polling on linux systems and incorporate the IN_CLOSE_WRITE event operation. Polling will remain as a feature however for Windows & MacOS architectures.

Pro

At some point in the near future, I hope to release a pro version of switchboard. This version will incorporate a whole host of features whcih I currently have plans for, and will be made available at a modest price. Best of all, you can join the wait-list now by sponsoring me on Github and receive a free copy of the pro version once it’s released!

In Conclusion

You can also visit https://goswitchboard.io for a full list of tips, tricks and examples to get you started using switchboard. Happy organising :)