Skip to content

Commit

Permalink
cli: improve Obtaining/Renewing wording (#8395)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
alexzorin authored Nov 13, 2020
1 parent 553d327 commit 78edb28
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
16 changes: 14 additions & 2 deletions certbot/certbot/_internal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
28 changes: 27 additions & 1 deletion certbot/certbot/display/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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
Expand Down Expand Up @@ -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
)


Expand Down Expand Up @@ -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)
23 changes: 22 additions & 1 deletion certbot/tests/display/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,27 @@ def test_multiple(self):
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 """

Expand All @@ -462,7 +483,7 @@ def test_notify(self, mock_util):
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
)


Expand Down

0 comments on commit 78edb28

Please sign in to comment.