Skip to content

Commit

Permalink
reckless: handle unresolvable situations in a --json friendly way
Browse files Browse the repository at this point in the history
  • Loading branch information
endothermicdev committed Aug 6, 2024
1 parent 07e88e9 commit 5f21acb
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions tools/reckless
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ log = Logger()
repos = ['https://github.com/lightningd/plugins']


def reckless_abort(err: str):
log.error(err)
log.add_result(None)
log.reply_json()
sys.exit(1)


def py_entry_guesses(name) -> list:
return [name, f'{name}.py', '__init__.py']

Expand Down Expand Up @@ -657,7 +664,7 @@ class Config():
confirm = True
sys.stdout = tmp
if not confirm:
sys.exit(1)
reckless_abort(f"config file required: {config_path}")
parent_path = Path(config_path).parent
# Create up to one parent in the directory tree.
if create_dir(parent_path):
Expand Down Expand Up @@ -912,11 +919,10 @@ def cargo_installation(cloned_plugin: InstInfo):
source = Path(cloned_plugin.source_loc)/'source'/cloned_plugin.name
log.debug(f'cargo installing from {source}')
if logging.root.level < logging.INFO and not log.capture:
cargo = Popen(call, cwd=str(source), text=True)
cargo = run(call, cwd=str(source), text=True)
else:
cargo = Popen(call, cwd=str(source), stdout=PIPE,
stderr=PIPE, text=True)
cargo.wait()
cargo = run(call, cwd=str(source), stdout=PIPE,
stderr=PIPE, text=True)

if cargo.returncode == 0:
log.debug('rust project compiled successfully')
Expand Down Expand Up @@ -1142,7 +1148,7 @@ def _install_plugin(src: InstInfo) -> Union[InstInfo, None]:
log.debug(f'Install requested from {src}.')
if RECKLESS_CONFIG is None:
log.error('reckless install directory unavailable')
sys.exit(2)
return None

# Use a unique directory for each cloned repo.
tmp_path = get_temp_reckless_dir()
Expand Down Expand Up @@ -1425,7 +1431,7 @@ def enable(plugin_name: str):
path = inst.entry
if not Path(path).exists():
log.error(f'cannot find installed plugin at expected path {path}')
sys.exit(1)
return None
log.debug(f'activating {plugin_name}')
try:
lightning_cli('plugin', 'start', path)
Expand Down Expand Up @@ -1495,24 +1501,21 @@ def load_config(reckless_dir: Union[str, None] = None,
reck_conf_path = Path(reckless_dir) / f'{network}-reckless.conf'
if net_conf:
if str(network_path) != net_conf.conf_fp:
log.error('reckless configuration does not match lightningd:\n'
f'reckless network config path: {network_path}\n'
f'lightningd active config: {net_conf.conf_fp}')
sys.exit(1)
reckless_abort('reckless configuration does not match lightningd:\n'
f'reckless network config path: {network_path}\n'
f'lightningd active config: {net_conf.conf_fp}')
else:
# The network-specific config file (bitcoin by default)
net_conf = LightningBitcoinConfig(path=network_path)
# Reckless manages plugins here.
try:
reckless_conf = RecklessConfig(path=reck_conf_path)
except FileNotFoundError:
log.error('reckless config file could not be written: '
+ str(reck_conf_path))
sys.exit(1)
reckless_abort('reckless config file could not be written: '
+ str(reck_conf_path))
if not net_conf:
print('Error: could not load or create the network specific lightningd'
' config (default .lightning/bitcoin)')
sys.exit(1)
reckless_abort('Error: could not load or create the network specific lightningd'
' config (default .lightning/bitcoin)')
net_conf.editConfigFile(f'include {reckless_conf.conf_fp}', None)
return reckless_conf

Expand Down

0 comments on commit 5f21acb

Please sign in to comment.