1

I am an amateur developper and would like to share some of my code via GitHub or GitLabs.

The problem I am trying to resolve is that I have a configuration file with my real credentials (used during development) and would like to provide a generic one (with placehholders for the credentials) -- all this without changing the code.

In practical terms I have today (in pseudo-code)

arguments = yaml.read("configuration.yaml") 

and would like to avoid dragging a

# do not forget to switch the config files before pushing to github arguments = yaml.read("configuration-dev.yaml") #arguments = yaml.read("configuration.yaml") 

Is there an established method for such issues? (generally speaking - elements in code which are specific to an installation, but with the need to send generic/placeholder entries)

Note: I am mostly developping in Python if it makes an approach easier

1 Answer 1

3

Im not sure these are the established methods, nonetheless the often seen approach is to use either environment variables or config merging.

Using Env variables

This allows you to specify a default config or, when a certain ENV_VAR is defined, to use that environment var.

The example code would be (using Python's ConfigParser INI files):

config_path = os.environ.get('ENV_VAR', 'default_config.ini') config = ConfigParser() config.read(config_path) 

The advantage is that you could use the same method to deploy with a production_config.ini. Just call your code as:

$ ENV_VAR='prod_config.ini' my_python_script.py 

Using config merging

Again, Python's ConfigParser allows you to read a list of config files, where options defined in successive files overwrite previous ones. If specified files do not exist, they are ignored. This is effectively config merging.

(...) config.read(['generic_config.ini', 'local_config.ini']) 

If you're using YAML you might need to provide custom logic for config merging.

In both cases, the local_config.ini should be ignored and not committed to the repository. This allows every team member/contributor to have his/her own local_config.ini automatically used.

Make sure never to commit credentials to the code repository!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.