2018-03-01 17:05:50 -05:00
|
|
|
"""Contains UI methods for Nginx operations."""
|
|
|
|
|
import logging
|
2022-01-12 19:36:51 -05:00
|
|
|
from typing import Iterable
|
|
|
|
|
from typing import List
|
|
|
|
|
from typing import Optional
|
|
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
from certbot.display import util as display_util
|
2023-02-10 13:51:20 -05:00
|
|
|
from certbot_nginx._internal.obj import VirtualHost
|
2018-03-01 17:05:50 -05:00
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2022-01-12 19:36:51 -05:00
|
|
|
def select_vhost_multiple(vhosts: Optional[Iterable[VirtualHost]]) -> List[VirtualHost]:
|
2018-03-01 17:05:50 -05:00
|
|
|
"""Select multiple Vhosts to install the certificate for
|
|
|
|
|
:param vhosts: Available Nginx VirtualHosts
|
|
|
|
|
:type vhosts: :class:`list` of type `~obj.Vhost`
|
|
|
|
|
:returns: List of VirtualHosts
|
|
|
|
|
:rtype: :class:`list`of type `~obj.Vhost`
|
|
|
|
|
"""
|
|
|
|
|
if not vhosts:
|
2020-04-13 13:41:39 -04:00
|
|
|
return []
|
2018-03-01 17:05:50 -05:00
|
|
|
tags_list = [vhost.display_repr()+"\n" for vhost in vhosts]
|
|
|
|
|
# Remove the extra newline from the last entry
|
2018-05-14 12:33:30 -04:00
|
|
|
if tags_list:
|
2018-03-01 17:05:50 -05:00
|
|
|
tags_list[-1] = tags_list[-1][:-1]
|
2021-07-19 20:09:06 -04:00
|
|
|
code, names = display_util.checklist(
|
2018-03-01 17:05:50 -05:00
|
|
|
"Which server blocks would you like to modify?",
|
|
|
|
|
tags=tags_list, force_interactive=True)
|
|
|
|
|
if code == display_util.OK:
|
|
|
|
|
return_vhosts = _reversemap_vhosts(names, vhosts)
|
|
|
|
|
return return_vhosts
|
|
|
|
|
return []
|
|
|
|
|
|
2021-07-19 20:09:06 -04:00
|
|
|
|
2022-01-12 19:36:51 -05:00
|
|
|
def _reversemap_vhosts(names: Iterable[str], vhosts: Iterable[VirtualHost]) -> List[VirtualHost]:
|
2018-03-01 17:05:50 -05:00
|
|
|
"""Helper function for select_vhost_multiple for mapping string
|
|
|
|
|
representations back to actual vhost objects"""
|
2020-04-13 13:41:39 -04:00
|
|
|
return_vhosts = []
|
2018-03-01 17:05:50 -05:00
|
|
|
|
|
|
|
|
for selection in names:
|
|
|
|
|
for vhost in vhosts:
|
|
|
|
|
if vhost.display_repr().strip() == selection.strip():
|
|
|
|
|
return_vhosts.append(vhost)
|
|
|
|
|
return return_vhosts
|