A Garbage Collector in 2 Shell Scripts

My previous post about a creating lightweight garbage collector for settings was a little obtuse.  This post is an example implementation for companies using LaunchDarkly and Gitlab.

Context, You’ve Got a Mess of Distributed Settings

Remember, this is a technique for getting a handle on your settings AFTER you’ve created or inherited a distributed mess.  Not making a mess in the first place is always a better idea.

This technique is to help you get started cleaning up a mess.  A mess is an ambiguous problem, it doesn’t have a central mass.  There’s no “worst” and no particularly good or bad place to start.

A Garbage Collector will help you get started by showing you the easiest things to clean up!

Step 1 – Extract Your Settings From LaunchDarkly

Create a shell script, extractSettings.sh

Calling ./extractSettings.sh <your api key> will give you a list of setting names, one per line.

These are the unique strings that you need to search for in gitlab.

Step 2 – Search Gitlab for Setting Strings

Create the second shell script, settingsToProjects.sh

Calling “./settingsToProjects.sh <your api key>” will give you a directory full of files.
Note: You need an admin level API key so that the script can see all projects.  Otherwise you might miss some references in projects you can’t see.

Each file will be “settingName.txt” and contain results like this:

“project_id”: 1
“project_id”: 15
“project_id”: 243

Find the Unused Settings

This doesn’t even need a script, run:

This will give you a list of every setting and the number of projects it is used in, sorted highest to lowest.  Anything with 0 lines is used in your codebase, 1 line means that it is referenced by one project, etc.

Take Out The Garbage

Start with the zeros.  Any file with 0 lines is either brand new, or no longer used.  That’s an easy check in LaunchDarkly.

Next, check for the single project use cases.  Some of those will be unused strings in constants files.  Clean up the constants files, and clean up the settings that fell to zero.

Because your garbage collector is tracking references, you can stop at any time.  You’ve automated the massive data gathering element, which makes cleanup cheap and manageable.

Leave a Reply

Site Footer

Discover more from Sherman On Software

Subscribe now to keep reading and get access to the full archive.

Continue reading