cli: improve Obtaining/Renewing wording (#8395)

* cli: improve Obtaining/Renewing wording

* dont use logger, and use new phrasing

* .display_util.notify: dont wrap

As this function is supposed to be an analogue for print, we do not want
it to wrap by default.
This commit is contained in:
alexzorin 2020-11-13 11:09:29 +11:00 committed by GitHub
parent 553d3279c6
commit 78edb2889e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 4 deletions

View file

@ -113,12 +113,24 @@ def _get_and_save_cert(le_client, config, domains=None, certname=None, lineage=N
if lineage is not None:
# Renewal, where we already know the specific lineage we're
# interested in
logger.info("Renewing an existing certificate")
display_util.notify(
"{action} for {domains}".format(
action="Simulating renewal of an existing certificate"
if config.dry_run else "Renewing an existing certificate",
domains=display_util.summarize_domain_list(domains or lineage.names())
)
)
renewal.renew_cert(config, domains, le_client, lineage)
else:
# TREAT AS NEW REQUEST
assert domains is not None
logger.info("Obtaining a new certificate")
display_util.notify(
"{action} for {domains}".format(
action="Simulating a certificate request" if config.dry_run else
"Requesting a certificate",
domains=display_util.summarize_domain_list(domains)
)
)
lineage = le_client.obtain_and_enroll_certificate(domains, certname)
if lineage is False:
raise errors.Error("Certificate could not be obtained")

View file

@ -16,6 +16,7 @@ import textwrap
import zope.interface
import zope.component
from acme.magic_typing import List
from certbot import errors
from certbot import interfaces
from certbot._internal import constants
@ -105,7 +106,7 @@ def notify(msg):
"""
zope.component.getUtility(interfaces.IDisplay).notification(
msg, pause=False, decorate=False
msg, pause=False, decorate=False, wrap=False
)
@ -633,3 +634,28 @@ def _parens_around_char(label):
"""
return "({first}){rest}".format(first=label[0], rest=label[1:])
def summarize_domain_list(domains):
# type: (List[str]) -> str
"""Summarizes a list of domains in the format of:
example.com.com and N more domains
or if there is are only two domains:
example.com and www.example.com
or if there is only one domain:
example.com
:param list domains: `str` list of domains
:returns: the domain list summary
:rtype: str
"""
if not domains:
return ""
l = len(domains)
if l == 1:
return domains[0]
elif l == 2:
return " and ".join(domains)
else:
return "{0} and {1} more domains".format(domains[0], l-1)

View file

@ -454,6 +454,27 @@ class PlaceParensTest(unittest.TestCase):
self.assertEqual("(y)es please", self._call("yes please"))
class SummarizeDomainListTest(unittest.TestCase):
@classmethod
def _call(cls, domains):
from certbot.display.util import summarize_domain_list
return summarize_domain_list(domains)
def test_single_domain(self):
self.assertEqual("example.com", self._call(["example.com"]))
def test_two_domains(self):
self.assertEqual("example.com and example.org",
self._call(["example.com", "example.org"]))
def test_many_domains(self):
self.assertEqual("example.com and 2 more domains",
self._call(["example.com", "example.org", "a.example.com"]))
def test_empty_domains(self):
self.assertEqual("", self._call([]))
class NotifyTest(unittest.TestCase):
"""Test the notify function """
@ -462,7 +483,7 @@ class NotifyTest(unittest.TestCase):
from certbot.display.util import notify
notify("Hello World")
mock_util().notification.assert_called_with(
"Hello World", pause=False, decorate=False
"Hello World", pause=False, decorate=False, wrap=False
)