Create a “symlink-esque” file to “merge” two files together

filesystemssymbolic-linkunix

I'm dealing with an application that reads a config file, config.yml, from the current directory. This config file contains both secret information (API keys) that shouldn't be checked in to source control, and other configuration information that can be.

What I'd like to do is to split this config file in two, to have config.secret.yml and config.public.yml. config.secret.yml will be ignored by source control, while config.public.yml can be checked in.

Then, I want to somehow create a config.yml file-like object that effectively "symlinks" the concatenated contents of config.secret.yml and config.public.yml, so that this happens:

> cat config.secret.yml
---
:api_key: 2712ab206a8003e44b2a84c467fc1b58
>
> cat config.public.yml    
:site_id: 123
>
> cat config.yml
---
:api_key: 2712ab206a8003e44b2a84c467fc1b58
:site_id: 123

I'm aware that symlinks themselves don't support this sort of behaviour, and that one would usually have both "default" and "local" settings read from the application side, but let's work under the assumption that only a single configuration file can be read.

The file is read by Ruby's File.read() method and parsed by YAML.load() if that provides any opportunity for cunning hacks.

Best Answer

You should be able to use a named pipe for this.

As a sample:

echo foo > file1
echo bar > file2

then create a new script in the same directory as follows:

combine.sh:

#!/bin/bash

[ -p foobar ] && rm foobar

trap "[ -p foobar ] && rm foobar && exit" SIGINT

while [  true ]; do
  mkfifo foobar
  echo "Writing File"
  cat file1 file2 > foobar
  rm foobar
done

Then

chmod +x combine.sh
./combine.sh

This will create a pipe named foobar in the same directory. You can now read from this pipe from any process as if it were a real file.

For example, in a separate terminal:

$ cat foobar
foo
bar

Of course you could then run this in the background using ./combine.sh 2>&1 > /dev/null & or run it as a service if you need greater persistence.

Related Question