autosign, led: improve no sudo handling

This commit is contained in:
The MMGen Project 2024-09-20 09:36:03 +00:00
commit 48923c2219
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
2 changed files with 16 additions and 12 deletions

View file

@ -48,9 +48,9 @@ class SwapMgrBase:
return ret
def disable(self, quiet=False):
self.cfg._util.qmsg_r(f'Disabling {self.desc}...')
self.cfg._util.qmsg_r(f'Attempting to disable {self.desc}...')
ret = self.do_disable()
self.cfg._util.qmsg('done')
self.cfg._util.qmsg('success')
if not quiet:
self.cfg._util.qmsg(
f'{capfirst(self.desc)} successfully disabled ({fmt_list(ret, fmt="no_quotes")})'
@ -66,13 +66,14 @@ class SwapMgrBase:
for cmd in cmds:
run(cmd.split(), check=True)
else:
nl = '\n' if op == 'disable' else ''
fs = blue('{a} {b} by executing the following command{c}:\n{d}')
m = nl + fs.format(
a = 'Before continuing, please disable' if op == 'disable' else 'Enable',
pre = 'failure\n' if op == 'disable' else ''
fs = blue('{a} {b} manually by executing the following command{c}:\n{d}')
post = orange('[To prevent this message in the future, enable sudo without a password]')
m = pre + fs.format(
a = 'Please disable' if op == 'disable' else 'Enable',
b = self.desc,
c = suf(cmds),
d = fmt_list(cmds, indent=' ', fmt='col'))
d = fmt_list(cmds, indent=' ', fmt='col')) + '\n' + post
msg(m)
if not self.cfg.test_suite:
sys.exit(1)

View file

@ -20,12 +20,12 @@
led: Control the LED on a single-board computer
"""
import os,threading
import sys, os, threading
from collections import namedtuple
from subprocess import run
from .util import msg, msg_r, fmt, die, have_sudo
from .color import orange
from .color import blue, orange
class LEDControl:
@ -100,9 +100,12 @@ class LEDControl:
run(cmd.split(), check=True)
write_init_val(init_val)
else:
die(2,
f'\nYou do not have access to the {desc} file\n'
f'To allow access, run the following command:\n\n {cmd}')
msg('\n{}\n{}\n{}'.format(
blue(f'You do not have access to the {desc} file'),
blue(f'To allow access, run the following command:\n\n {cmd}'),
orange('[To prevent this message in the future, enable sudo without a password]')
))
sys.exit(1)
check_access(board.status,desc='status LED control')