skip to Main Content

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.

2

Answers


  1. Is this a bad idea for some reason?

    Yes, it is completely unreadable. You have to have base64 and gzip, they are not always available. It is not straightforward – you have to base64 and gzip. It will look incredibly bad in git version control systems and be unmaintainable and unreadable to track changes.

    Is there a better commonplace way?

    Typically, tools read from stdin.

    $ ./myprogram -f - <<EOF
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    EOF
    

    Or just pass the string:

    $ ./myprogram -e 'configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345
    configuration text 12345'
    

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

    With standard input or as a literal string or with a separate file descriptor. Users can invoke Bash if they want to redirect strings to a file descriptor.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search