Use shlex.join() when adding postfix alias

a bit convoluted perhaps, but still better...
This commit is contained in:
Lance Edgar 2022-11-01 18:41:27 -05:00
parent a2dca4ea65
commit 614fd92a20

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2019 Lance Edgar # Copyright © 2010-2022 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,6 +24,8 @@
Fabric library for Postfix Fabric library for Postfix
""" """
import shlex
from rattail_fabric2 import apt from rattail_fabric2 import apt
@ -42,10 +44,17 @@ def alias(c, name, alias_to, path='/etc/aliases'):
# does alias entry already exist? # does alias entry already exist?
if c.run("grep '^{}:' /etc/aliases".format(name), warn=True).failed: if c.run("grep '^{}:' /etc/aliases".format(name), warn=True).failed:
# append new entry # append new entry
c.sudo("""bash -c 'echo "{}: {}" >> /etc/aliases'""".format(name, alias_to)) entry = '{}: {}'.format(name, alias_to)
echo = shlex.join(['echo', entry])
cmd = '{} >> /etc/aliases'.format(echo)
cmd = shlex.join(['bash', '-c', cmd])
c.sudo(cmd)
else: else:
# update existing entry # update existing entry
c.sudo('sed -i.bak -e "s/^{}: .*/{}: {}/" /etc/aliases'.format(name, name, alias_to)) alias_to = alias_to.replace('|', '\\|')
sub = "s|^{}: .*|{}: {}|".format(name, name, alias_to)
cmd = shlex.join(['sed', '-i.bak', '-E', sub, '/etc/aliases'])
c.sudo(cmd)
c.sudo('newaliases') c.sudo('newaliases')