Allow custom message for each deprecated parameter

This commit is contained in:
Darko Poljak 2019-06-19 18:19:32 +02:00
parent 55ba49efac
commit 3cb4e76175
8 changed files with 44 additions and 35 deletions
cdist
config.py
core
test/cdist_type
__init__.py
fixtures/__with_deprecated_parameters/parameter
docs/src

View file

@ -769,9 +769,14 @@ class Config(object):
self.log.warning("Type %s is deprecated.", cdist_type.name) self.log.warning("Type %s is deprecated.", cdist_type.name)
for param in cdist_object.parameters: for param in cdist_object.parameters:
if param in cdist_type.deprecated_parameters: if param in cdist_type.deprecated_parameters:
self.log.warning(("%s parameter of type %s is deprecated, " msg = cdist_type.deprecated_parameters[param]
"see man page for details"), if msg:
param, cdist_type.name) format = "%s parameter of type %s is deprecated: %s"
args = [param, cdist_type.name, msg]
else:
format = "%s parameter of type %s is deprecated."
args = [param, cdist_type.name]
self.log.warning(format, *args)
def object_prepare(self, cdist_object, transfer_type_explorers=True): def object_prepare(self, cdist_object, transfer_type_explorers=True):
"""Prepare object: Run type explorer + manifest""" """Prepare object: Run type explorer + manifest"""

View file

@ -279,20 +279,20 @@ class CdistType(object):
@property @property
def deprecated_parameters(self): def deprecated_parameters(self):
"""Return a list of deprecated parameters"""
if not self.__deprecated_parameters: if not self.__deprecated_parameters:
parameters = [] deprecated = {}
try: try:
with open(os.path.join(self.absolute_path, deprecated_dir = os.path.join(self.absolute_path,
"parameter", "parameter",
"deprecated")) as fd: "deprecated")
for line in fd: for name in cdist.core.listdir(deprecated_dir):
line = line.strip() try:
if line: with open(os.path.join(deprecated_dir, name)) as fd:
parameters.append(line) deprecated[name] = fd.read().strip()
except EnvironmentError:
pass # Swallow errors raised by open() or read()
except EnvironmentError: except EnvironmentError:
# error ignored pass # Swallow error raised by os.listdir()
pass
finally: finally:
self.__deprecated_parameters = parameters self.__deprecated_parameters = deprecated
return self.__deprecated_parameters return self.__deprecated_parameters

View file

@ -205,10 +205,13 @@ class TypeTestCase(test.CdistTestCase):
base_path = fixtures base_path = fixtures
cdist_type = core.CdistType(base_path, cdist_type = core.CdistType(base_path,
'__without_deprecated_parameters') '__without_deprecated_parameters')
self.assertEqual(cdist_type.deprecated_parameters, []) self.assertEqual(cdist_type.deprecated_parameters, {})
def test_with_deprecated_parameters(self): def test_with_deprecated_parameters(self):
base_path = fixtures base_path = fixtures
cdist_type = core.CdistType(base_path, '__with_deprecated_parameters') cdist_type = core.CdistType(base_path, '__with_deprecated_parameters')
self.assertEqual(cdist_type.deprecated_parameters, self.assertTrue('eggs' in cdist_type.deprecated_parameters)
['eggs', 'spam']) self.assertTrue('spam' in cdist_type.deprecated_parameters)
self.assertEqual(cdist_type.deprecated_parameters['eggs'],
'Deprecated')
self.assertEqual(cdist_type.deprecated_parameters['spam'], '')

View file

@ -1,4 +1,3 @@
spam spam
eggs eggs
sausage sausage
spam

View file

@ -188,24 +188,27 @@ Example: (e.g. in cdist/conf/type/__nginx_vhost/manifest)
Deprecated parameters Deprecated parameters
--------------------- ---------------------
To deprecate type parameters one can declare a newline separated file To deprecate type parameters one can declare a file for each deprecated
**parameter/deprecated** where each line contains deprecated parameter. parameter under **parameter/deprecated** directory.
When it is used cdist writes warning line, e.g.:
When such parameter is used cdist writes warning line with deprecation message.
If such file has content then this content is printed as deprecation message.
If there is no content then generic parameter deprecation message is printed.
Example:
.. code-block:: sh .. code-block:: sh
$ cat parameter/optional_multiple $ ls parameter/deprecated/
foo eggs spam
spam $ cat parameter/deprecated/eggs
eggs eggs parameter is deprecated, please use multiple egg parameter.
$ cat parameter/deprecated $ cat parameter/deprecated/spam
spam $ echo '__foo foo --foo foo --eggs eggs' | ./bin/cdist config -i - 185.203.112.26
eggs WARNING: 185.203.112.26: eggs parameter of type __foo is deprecated: eggs parameter is deprecated, please use multiple egg parameter.
$ echo '__foo foo --foo foo --spam spam' | ./bin/cdist config -i - 185.203.112.26
WARNING: 185.203.112.26: spam parameter of type __foo is deprecated, see man page for details
$ echo '__foo foo --foo foo --eggs eggs --spam spam' | ./bin/cdist config -i - 185.203.112.26 $ echo '__foo foo --foo foo --eggs eggs --spam spam' | ./bin/cdist config -i - 185.203.112.26
WARNING: 185.203.112.26: spam parameter of type __foo is deprecated, see man page for details WARNING: 185.203.112.26: spam parameter of type __foo is deprecated.
WARNING: 185.203.112.26: eggs parameter of type __foo is deprecated, see man page for details WARNING: 185.203.112.26: eggs parameter of type __foo is deprecated: eggs parameter is deprecated, please use multiple egg parameter.
Input from stdin Input from stdin