pydoit / doit

CLI task management & automation tool
http://pydoit.org
MIT License
1.87k stars 175 forks source link

Have doit read global vars from doit.cfg #343

Open arichiardi opened 4 years ago

arichiardi commented 4 years ago

Dear all,

I would like to propose the following feature in doit (I have to say that I am a doit newbie I might have misunderstood something here).

I basically would like to contribute to "global vars" from a doit.cfg file.

By that, I mean that the following should be equivalent:

doit broker_host=... broker_token=...
[GLOBAL]
broker_host = ...
broker_token = ...

A better proposal would be to use a different name for the init section, maybe the [CONFIG] tag could be a good fit.

Describe alternatives you've considered

I am currently working around this limition by using th [task:<task-name] feature of doit.cfg.

My code then includes the following function wrapper:

def get_task_option(task, key):
    """Get the key option from the input task object.

    Tries to task.cfg_values.get first, task.options.get second and
    doit.get_var third.

    Return the value if something is found or raises ValueError if not.
    """
    key_with_underscore = key.replace("-", "_")
    cfg_value = task.cfg_values.get(key_with_underscore) if task.cfg_values else None
    cli_value = task.options.get(key_with_underscore) if task.options else None
    global_value = doit.get_var(key)

    if cfg_value is not None:
        return cfg_value
    elif cli_value is not None:
        return cli_value
    elif global_value is not None:
        return global_value
    else:
        raise ValueError(f"Missing required parameter '{key}'")

This effectively makes the params global.

Fund with Polar

arichiardi commented 4 years ago

@schettino72 any thought on this?

I have capacity now for working on this but I would like to hear your opinion first.

schettino72 commented 4 years ago

Hi, thanks for this. Makes sense to me.

A better proposal would be to use a different name for the init section, maybe the [CONFIG] tag could be a good fit.

Well, the naming convention is to call it global vars, so lets define it in a section called VARS.

ebridges commented 4 years ago

How would variables declared in this way relate to values in the environment? Any reason to not use env vars for this purpose (e.g. loaded from .env or otherwise declared in the environment)?

Alternatively, provide for these values to be overridden via the environment?