Search code examples
command-linebase64gzip

What is a good way to pass complex configuration as a command-line (not a file)?


Suppose I want to be able to run a command line utility that takes a YAML file as a configuration, and I want to add another running method for a case that the utility doesn't have access to the file (e.g. it's running in a freshly created docker container).

I had the idea to gzip the contents of the YAML file, then encode the output as base64 and use the string as the command-line argument, the utility can then do the reverse to generate its configuration.

e.g.

$ echo "configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345" | gzip -fc | base64 -w0
H4sIAFrPSWUAA0vOz0vLTC8tSizJzM9TKEmtKFEwNDI2MeVKHloSAArBW9DIAAAA

and the reverse

$ echo "H4sIAFrPSWUAA0vOz0vLTC8tSizJzM9TKEmtKFEwNDI2MeVKHloSAArBW9DIAAAA" | base64 -d | gzip -dc
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345
configuration text 12345

And running the utility can be done by either ./myprogram -f config.yaml or by
./myprogram -e "H4sIAFrPSWUAA0vOz0vLTC8tSizJzM9TKEmtKFEwNDI2MeVKHloSAArBW9DIAAAA"

This sounds straightforward enough to be a commonplace practice but I haven't seen it done anywhere (which doesn't mean much).

Is this a bad idea for some reason?
Is there a better commonplace way?
Am I trying to reinvent the wheel here, i.e. is this a solved problem with a generic obvious solution that I'm not aware of?

Thank you in advance.


Solution

  • Your approach of encoding a configuration into a command-line argument as a base64 string after compressing it is a viable and functional solution for passing complex configurations as command-line arguments. It's commonly used in various contexts, including when running applications in containers, as you've described.

    However,

    While your approach is straightforward, it may become less convenient when configurations grow more complex. When dealing with a large or nested configuration, maintaining and decoding these base64-encoded strings could become challenging. The base64-encoded string is not human-readable, making it difficult for users to review or modify the configuration. When working with others or troubleshooting, readability can be an issue.

    Instead of encoding configurations directly in the command line, consider using environment variables, configuration files, or even Kubernetes ConfigMaps or Secrets for containerized applications. These are more traditional methods for managing configurations. For Docker containers, Docker Swarm and Kubernetes offer solutions for handling sensitive configuration data with secrets and ConfigMaps.

    Always, Choose the approach that best fits your specific requirements and use case.