feat: can use .rougailcli.yml, env variable or command line argument from own script
This commit is contained in:
parent
97c87c24d3
commit
83b20dc0c1
5 changed files with 109 additions and 51 deletions
|
|
@ -17,7 +17,7 @@ along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
|||
"""
|
||||
|
||||
import os
|
||||
from warnings import warn, filterwarnings
|
||||
from warnings import filterwarnings
|
||||
from pathlib import Path
|
||||
from sys import exit
|
||||
|
||||
|
|
@ -28,10 +28,8 @@ from tiramisu import Config
|
|||
from rougail import Rougail
|
||||
from rougail.config import get_rougail_config
|
||||
from rougail.utils import load_modules
|
||||
from rougail.user_datas import UserDatas
|
||||
from rougail.error import RougailWarning
|
||||
|
||||
from rougail.user_data_commandline import RougailUserDataCommandline
|
||||
try:
|
||||
from rougail.user_data_yaml import RougailUserDataYaml
|
||||
except ImportError:
|
||||
|
|
@ -40,6 +38,7 @@ try:
|
|||
from rougail.user_data_environment import RougailUserDataEnvironment
|
||||
except ImportError:
|
||||
RougailUserDataEnvironment = None
|
||||
from .rougailconfig import load as rougailconfig_load
|
||||
|
||||
from .i18n import _
|
||||
|
||||
|
|
@ -52,8 +51,7 @@ def _main(arguments, do_not_print):
|
|||
rougailconfig = get_rougail_config(
|
||||
backward_compatibility=False, add_extra_options=False
|
||||
)
|
||||
rougailconfig.generate_config()
|
||||
cmd_config = load_cmd_user_datas(rougailconfig.config, arguments)
|
||||
cmd_config = load_cmd_user_datas(rougailconfig, arguments)
|
||||
print_traceback = rougailconfig["cli.debug"]
|
||||
if rougailconfig["cli.versions"]:
|
||||
versions = display_version(cmd_config)
|
||||
|
|
@ -72,59 +70,32 @@ def _main(arguments, do_not_print):
|
|||
exit(1)
|
||||
|
||||
|
||||
def load_cmd_user_datas(cmd_config, arguments):
|
||||
def load_cmd_user_datas(rougailconfig, arguments):
|
||||
rougailconfig.generate_config()
|
||||
cmd_config = rougailconfig.config
|
||||
origin_prop = cmd_config.property.default("read_write", "append")
|
||||
cmd_config.property.setdefault(
|
||||
frozenset(origin_prop | {"not_for_commandline"}), "read_write", "append"
|
||||
)
|
||||
cmd_config.property.read_write()
|
||||
cmd_user_datas = []
|
||||
config_file = None
|
||||
if RougailUserDataYaml:
|
||||
config_file = os.environ.pop(f"{ENV_PREFIX}_CLI.CONFIG_FILE", None)
|
||||
if not config_file:
|
||||
config_file = cmd_config.forcepermissive.option("cli.config_file").value.get()
|
||||
if Path(config_file).is_file():
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["yaml"],
|
||||
"yaml.filename": [config_file],
|
||||
"yaml.file_with_secrets": "all",
|
||||
}
|
||||
cmd_user_datas.extend(
|
||||
RougailUserDataYaml(cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
).run()
|
||||
)
|
||||
_config_file = os.environ.pop(f"{ENV_PREFIX}_CLI.CONFIG_FILE", None)
|
||||
if not _config_file:
|
||||
_config_file = cmd_config.forcepermissive.option("cli.config_file").value.get()
|
||||
if Path(_config_file).is_file():
|
||||
config_file = _config_file
|
||||
if RougailUserDataEnvironment:
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["environment"],
|
||||
"environment.default_environment_name": ENV_PREFIX,
|
||||
}
|
||||
cmd_user_datas.extend(
|
||||
RougailUserDataEnvironment(
|
||||
cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
).run()
|
||||
)
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["commandline"],
|
||||
}
|
||||
cmd_user_datas.extend(
|
||||
RougailUserDataCommandline(cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
short_name_max_len=2,
|
||||
arguments=arguments,
|
||||
).run()
|
||||
)
|
||||
user_data = UserDatas(cmd_config).user_datas(cmd_user_datas)
|
||||
if user_data["warnings"] and cmd_config.option("cli.warnings").value.get():
|
||||
for warning in user_data["warnings"]:
|
||||
warn(warning)
|
||||
env_prefix = ENV_PREFIX
|
||||
else:
|
||||
env_prefix = None
|
||||
user_data = rougailconfig_load(rougailconfig, config_file, env_prefix, True, display_warnings=cmd_config.option("cli.warnings").value.get(), raise_on_error=False, _arguments=arguments, _generate=False)
|
||||
# replays to display errors if needed
|
||||
parser = TiramisuCmdlineParser(
|
||||
cmd_config,
|
||||
short_name_max_len=2,
|
||||
)
|
||||
if not cmd_config.option("cli.versions").value.get():
|
||||
parser = TiramisuCmdlineParser(
|
||||
cmd_config,
|
||||
short_name_max_len=2,
|
||||
)
|
||||
parser.parse_args(arguments)
|
||||
if user_data["errors"]:
|
||||
raise Exception(user_data["errors"][0])
|
||||
|
|
|
|||
86
src/rougail/cli/rougailconfig.py
Normal file
86
src/rougail/cli/rougailconfig.py
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
"""
|
||||
Silique (https://www.silique.fr)
|
||||
Copyright (C) 2025
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
Mtools is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with Mtools. If not, see <http://www.gnu.org/licenses/>.
|
||||
"""
|
||||
from warnings import warn
|
||||
from rougail.user_datas import UserDatas
|
||||
|
||||
|
||||
def load(rougailconfig: "RougailConfig",
|
||||
yaml_file: str=None,
|
||||
env_prefix: str=None,
|
||||
commandline: bool=False,
|
||||
display_warnings: bool=True,
|
||||
raise_on_error: bool=True,
|
||||
_arguments=None,
|
||||
_generate=True
|
||||
):
|
||||
if _generate:
|
||||
rougailconfig.generate_config()
|
||||
cmd_config = rougailconfig.config
|
||||
origin_prop = cmd_config.property.exportation()
|
||||
cmd_config.property.read_write()
|
||||
user_datas = []
|
||||
if yaml_file:
|
||||
user_datas.extend(from_yaml(cmd_config, yaml_file))
|
||||
if env_prefix:
|
||||
user_datas.extend(from_env(cmd_config, env_prefix))
|
||||
if commandline:
|
||||
user_datas.extend(from_cmdline(cmd_config, _arguments))
|
||||
user_data = UserDatas(cmd_config).user_datas(user_datas)
|
||||
if display_warnings and user_data["warnings"]:
|
||||
for warning in user_data["warnings"]:
|
||||
warn(warning)
|
||||
if raise_on_error and user_data["errors"]:
|
||||
raise Exception(user_data["errors"][0])
|
||||
cmd_config.property.importation(origin_prop)
|
||||
return user_data
|
||||
|
||||
|
||||
def from_yaml(cmd_config, yaml_file):
|
||||
from rougail.user_data_yaml import RougailUserDataYaml
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["yaml"],
|
||||
"yaml.filename": [yaml_file],
|
||||
"yaml.file_with_secrets": "all",
|
||||
}
|
||||
return RougailUserDataYaml(cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
).run()
|
||||
|
||||
|
||||
def from_env(cmd_config, env_prefix):
|
||||
from rougail.user_data_environment import RougailUserDataEnvironment
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["environment"],
|
||||
"environment.default_environment_name": env_prefix,
|
||||
}
|
||||
return RougailUserDataEnvironment(
|
||||
cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
).run()
|
||||
|
||||
|
||||
def from_cmdline(cmd_config, arguments):
|
||||
from rougail.user_data_commandline import RougailUserDataCommandline
|
||||
fake_rougail_config = {
|
||||
"step.user_data": ["commandline"],
|
||||
}
|
||||
return RougailUserDataCommandline(cmd_config,
|
||||
rougailconfig=fake_rougail_config,
|
||||
short_name_max_len=2,
|
||||
arguments=arguments,
|
||||
).run()
|
||||
1
tests/cli/versions.txt
Normal file
1
tests/cli/versions.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
["tiramisu", "tiramisu-cmdline-parser", "rougail", "rougail-cli", "rougail-structural-bitwarden", "rougail-user-data-ansible", "rougail-user-data-bitwarden", "rougail-user-data-commandline", "rougail-user-data-environment", "rougail-user-data-yaml", "rougail-output-ansible", "rougail-output-console", "rougail-output-doc", "rougail-output-json"]
|
||||
|
|
@ -1 +1 @@
|
|||
"{\n \"my_variable\": {\n \"type\": \"variable\",\n \"default\": {\n \"name\": \"Default\",\n \"values\": \"my_value\"\n },\n \"properties\": [\n {\n \"type\": \"type\",\n \"name\": \"string\"\n },\n {\n \"type\": \"property\",\n \"name\": \"mandatory\"\n }\n ],\n \"paths\": [\n \"my_variable\"\n ],\n \"names\": [\n \"my_variable\"\n ],\n \"descriptions\": [\n \"A description.\"\n ]\n }\n}"
|
||||
"{\n \"my_variable\": {\n \"type\": \"variable\",\n \"default\": {\n \"name\": \"Default\",\n \"values\": \"my_value\"\n },\n \"properties\": [\n {\n \"type\": \"type\",\n \"name\": \"string\"\n },\n {\n \"type\": \"property\",\n \"name\": \"mandatory\"\n }\n ],\n \"path\": \"my_variable\",\n \"names\": [\n \"my_variable\"\n ],\n \"description\": \"A description.\"\n }\n}"
|
||||
|
|
@ -1 +1 @@
|
|||
"{\n \"my_variable\": {\n \"type\": \"variable\",\n \"default\": {\n \"name\": \"Default\",\n \"values\": \"my_value\"\n },\n \"properties\": [\n {\n \"type\": \"type\",\n \"name\": \"string\"\n },\n {\n \"type\": \"property\",\n \"name\": \"mandatory\"\n }\n ],\n \"paths\": [\n \"my_variable\"\n ],\n \"names\": [\n \"my_variable\"\n ],\n \"descriptions\": [\n \"A description.\"\n ]\n }\n}"
|
||||
"{\n \"my_variable\": {\n \"type\": \"variable\",\n \"default\": {\n \"name\": \"Default\",\n \"values\": \"my_value\"\n },\n \"properties\": [\n {\n \"type\": \"type\",\n \"name\": \"string\"\n },\n {\n \"type\": \"property\",\n \"name\": \"mandatory\"\n }\n ],\n \"path\": \"my_variable\",\n \"names\": [\n \"my_variable\"\n ],\n \"description\": \"A description.\"\n }\n}"
|
||||
Loading…
Reference in a new issue