Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the 'Partial Options Monoid' pattern for the configuration #130

Open
4 tasks
chshersh opened this issue Oct 7, 2022 · 2 comments
Open
4 tasks
Labels
CLI Command Line Interface config TOML configuration, config-related CLI options hacktoberfest https://hacktoberfest.com/ refactoring
Milestone

Comments

@chshersh
Copy link
Owner

chshersh commented Oct 7, 2022

This pattern is common in the FP world. The description (in Haskell) can be found here:

The idea is the following:

  • Create the Options type with all configuration fields optional

  • Create functions to create this Options type from different environments:

    • Hardcoded default
    • Environment variables
    • Files
    • CLI options

    It's absolutely okay for different environments to fill only some parts of the config.

  • Implement a function to combine two Options with the second argument overriding the previous values if they both are present (otherwise, take the one that is not None)

  • Read options on start from all four sources and combine them in the following order of increasing priority (e.g. CLI arguments are more important than everything else)

    1. Defaults.
    2. Environment variables
    3. Configuration file.
    4. CLI flags

That's all! With this approach, it should be possible to configure different options from different sources and have a uniform config.

@chshersh chshersh added config TOML configuration, config-related CLI options CLI Command Line Interface hacktoberfest https://hacktoberfest.com/ refactoring labels Oct 7, 2022
@chshersh chshersh added this to the v0.3.0: Auto milestone Oct 7, 2022
@crudiedo
Copy link
Contributor

Hi everyone,

I wanted to make sure we have the same variables name format in mind:

Currently, we have 2 different kinds of things we could configure - it's either store_directory\proxy (non tools related) or any tools field.

For non-tools related variables it's quite simple:

# ENV
TOOL_SYNC_STORE_DIRECTORY=~/.bin
# Cli
--store_directory=~/.bin

And for the tools it would be like:

# ENV
TOOL_SYNC_BOTTOM_OWNER=clementtsang
TOOL_SYNC_BOTTOM_REPO=bottom
TOOL_SYNC_BOTTOM_EXE_NAME=btm
TOOL_SYNC_ASSET_NAME_MACOS=darwin
# Cli
--bottom.owner=clementtsang
--bottom.repo=bottom
--bottom.exe_name=btm
--bottom.asset_name.macos=darwin
  1. Do we want to use TOOL_SYNC as the env prefix? I thought about just TOOL, but it's quite ambiguous and could be reused by any other tool (:

  2. Do we really want to let the ENV configure tools? It looks like parsing that would be a mess (still need to do it for the cli though), and even with some generic parsing function the code will become more complicated.

@chshersh
Copy link
Owner Author

@crudiedo Thanks for moving the conversation! Answering your question:

Do we want to use TOOL_SYNC as the env prefix? I thought about just TOOL, but it's quite ambiguous and could be reused by any other tool (:

I agree about the TOOL_SYNC_ prefix for environment variables 👍🏻

Do we really want to let the ENV configure tools? It looks like parsing that would be a mess (still need to do it for the cli though), and even with some generic parsing function the code will become more complicated.

No, we don't environment variables for individual tools 🙂 The usage for environment variables is to apply some configuration for every tool invocation and usually this is some global setting. Tool details are too granular and individual to be supported by env variables (and indeed will be a mess to parse as you mentioned).

My opinion:

Env:

  • Config path
  • Proxy
  • Store directory

CLI args:

File:

  • As current

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI Command Line Interface config TOML configuration, config-related CLI options hacktoberfest https://hacktoberfest.com/ refactoring
Projects
None yet
Development

No branches or pull requests

2 participants