feat: use Calculation
This commit is contained in:
parent
a86a8bdc28
commit
89112ba2e1
1 changed files with 25 additions and 39 deletions
|
|
@ -25,7 +25,7 @@ from os import environ
|
||||||
from shutil import which
|
from shutil import which
|
||||||
|
|
||||||
|
|
||||||
from tiramisu.error import display_list
|
from tiramisu.error import ConfigError, display_list
|
||||||
from rougail.error import ExtensionError
|
from rougail.error import ExtensionError
|
||||||
from .i18n import _
|
from .i18n import _
|
||||||
|
|
||||||
|
|
@ -179,7 +179,7 @@ class RougailUserDataBitwarden:
|
||||||
values[option.path(uncalculated=True)] = (set_password, self.leader_informations, self.command)
|
values[option.path(uncalculated=True)] = (set_password, self.leader_informations, self.command)
|
||||||
|
|
||||||
|
|
||||||
def set_password(leader_informations, command, *, option, warnings, errors):
|
def set_password(leader_informations, command, *, option):
|
||||||
path = option.path()
|
path = option.path()
|
||||||
leader_key = None
|
leader_key = None
|
||||||
if option.isfollower():
|
if option.isfollower():
|
||||||
|
|
@ -187,92 +187,78 @@ def set_password(leader_informations, command, *, option, warnings, errors):
|
||||||
if leader_path in leader_informations:
|
if leader_path in leader_informations:
|
||||||
leader_key = leader_informations[leader_path][option.index()]
|
leader_key = leader_informations[leader_path][option.index()]
|
||||||
if not option.isleader():
|
if not option.isleader():
|
||||||
return get_values(command, option, warnings, errors, leader_key=leader_key)
|
return get_values(command, option, leader_key=leader_key)
|
||||||
leader_informations[path], option_values = get_values(
|
leader_informations[path], option_values = get_values(
|
||||||
command, option, warnings, errors, multiple=True, leader_key=option.value.default()[0]
|
command, option, multiple=True, leader_key=option.value.default()[0]
|
||||||
)
|
)
|
||||||
return option_values
|
return option_values
|
||||||
|
|
||||||
|
|
||||||
def get_values(command, option, warnings, errors, *, multiple=False, leader_key=None):
|
def get_values(command, option, *, multiple=False, leader_key=None):
|
||||||
if leader_key:
|
if leader_key:
|
||||||
key = leader_key
|
key = leader_key
|
||||||
else:
|
else:
|
||||||
key = option.value.default()
|
key = option.value.default()
|
||||||
path = option.path()
|
|
||||||
type_ = option.information.get("type")
|
type_ = option.information.get("type")
|
||||||
if "validator" not in option.property.get():
|
if "validator" not in option.property.get():
|
||||||
option.property.add("validator")
|
option.property.add("validator")
|
||||||
if not isinstance(key, str):
|
if not isinstance(key, str):
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_('the default value for "{0}" must be the Bitwarden item name').format(
|
_('the default value must be the Bitwarden item name')
|
||||||
path
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
if multiple:
|
|
||||||
return [], []
|
|
||||||
return None
|
|
||||||
try:
|
try:
|
||||||
data = command.get_key(key, multiple)
|
data = command.get_key(key, multiple)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_(
|
_(
|
||||||
'cannot execute the "{0}" commandline from Bitwarden for "{1}": {2}'
|
'cannot execute the "{0}" commandline from Bitwarden: {1}'
|
||||||
).format(command, path, exc)
|
).format(command, exc)
|
||||||
)
|
)
|
||||||
if multiple:
|
|
||||||
return [], []
|
|
||||||
return None
|
|
||||||
if not data:
|
if not data:
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_('item "{0}" in Bitwarden is not found for "{1}"').format(
|
_('item "{0}" in Bitwarden is not found"').format(
|
||||||
key, path
|
key
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if multiple:
|
|
||||||
return [], []
|
|
||||||
return None
|
|
||||||
if len(data) != 1:
|
if len(data) != 1:
|
||||||
names = [d["name"] for d in data]
|
names = [d["name"] for d in data]
|
||||||
if multiple:
|
if multiple:
|
||||||
ret = []
|
ret = []
|
||||||
return names, [
|
return names, [
|
||||||
get_value(key, path, type_, d, errors) for d in data
|
get_value(key, type_, d) for d in data
|
||||||
]
|
]
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_(
|
_(
|
||||||
'several items found with name "{0}" in Bitwarden for "{1}": "{2}"'
|
'several items found with name "{0}" in Bitwarden: "{1}"'
|
||||||
).format(key, path, '", "'.join(names))
|
).format(key, '", "'.join(names))
|
||||||
)
|
)
|
||||||
return None
|
value = get_value(key, type_, data[0])
|
||||||
value = get_value(key, path, type_, data[0], errors)
|
|
||||||
if multiple:
|
if multiple:
|
||||||
return [data[0]["name"]], [value]
|
return [data[0]["name"]], [value]
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
def get_value( key_bitwarden: str, path: str, type_: str, data: dict, errors: dict) -> str:
|
def get_value( key_bitwarden: str, type_: str, data: dict) -> str:
|
||||||
try:
|
try:
|
||||||
if type_ == "secret":
|
if type_ == "secret":
|
||||||
value = data["login"]["password"]
|
value = data["login"]["password"]
|
||||||
else:
|
else:
|
||||||
value = data["login"]["username"]
|
value = data["login"]["username"]
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(
|
_('unexpected datas "{0}" from Bitwarden: {2}').format(
|
||||||
key_bitwarden, path, exc
|
key_bitwarden, exc
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
value = None
|
|
||||||
else:
|
else:
|
||||||
if value is None:
|
if value is None:
|
||||||
if type_ == "secret":
|
if type_ == "secret":
|
||||||
bw_type = _("password")
|
bw_type = _("password")
|
||||||
else:
|
else:
|
||||||
bw_type = _("username")
|
bw_type = _("username")
|
||||||
errors.append(
|
raise ConfigError(
|
||||||
_('item "{0}" in Bitwarden has no {1} for "{2}"').format(
|
_('item "{0}" in Bitwarden has no {1}').format(
|
||||||
key_bitwarden, bw_type, path
|
key_bitwarden, bw_type
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return value
|
return value
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue