Skip to content

Commit

Permalink
reckless: all commands return objects to enable json out
Browse files Browse the repository at this point in the history
  • Loading branch information
endothermicdev committed Jul 24, 2024
1 parent 4971dd6 commit 7c8f041
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions tools/reckless
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ class Logger:
def add_result(self, result: str):
self.json_output["result"].append(result)

def reply_json(self):
"""json output to stdout with accumulated result."""
if len(log.json_output["result"]) == 1 and \
isinstance(log.json_output["result"], list):
# unpack sources output
log.json_output["result"] = log.json_output["result"][0]
print(json.dumps(log.json_output, indent=4))


log = Logger()

Expand Down Expand Up @@ -1265,8 +1273,10 @@ def _install_plugin(src: InstInfo) -> Union[InstInfo, None]:
return staged_src


def install(plugin_name: str):
"""downloads plugin from source repos, installs and activates plugin"""
def install(plugin_name: str) -> Union[str, None]:
"""Downloads plugin from source repos, installs and activates plugin.
Returns the location of the installed plugin or "None" in the case of
failure."""
assert isinstance(plugin_name, str)
# Specify a tag or commit to checkout by adding @<tag> to plugin name
if '@' in plugin_name:
Expand Down Expand Up @@ -1295,22 +1305,26 @@ def install(plugin_name: str):
return f"{installed.source_loc}"
log.error(('dynamic activation failed: '
f'{installed.name} not found in reckless directory'))
sys.exit(1)
return None


def uninstall(plugin_name: str):
"""disables plugin and deletes the plugin's reckless dir"""
def uninstall(plugin_name: str) -> str:
"""dDisables plugin and deletes the plugin's reckless dir. Returns the
status of the uninstall attempt."""
assert isinstance(plugin_name, str)
log.debug(f'Uninstalling plugin {plugin_name}')
disable(plugin_name)
inst = InferInstall(plugin_name)
if not Path(inst.entry).exists():
log.error("cannot find installed plugin at expected path"
f"{inst.entry}")
sys.exit(1)
return "uninstall failed"
log.debug(f'looking for {str(Path(inst.entry).parent)}')
if remove_dir(str(Path(inst.entry).parent)):
log.info(f"{inst.name} uninstalled successfully.")
else:
return "uninstall failed"
return "uninstalled"


def search(plugin_name: str) -> Union[InstInfo, None]:
Expand Down Expand Up @@ -1342,7 +1356,7 @@ def search(plugin_name: str) -> Union[InstInfo, None]:
log.debug(f"entry: {found.entry}")
if found.subdir:
log.debug(f'sub-directory: {found.subdir}')
return found
return found.source_loc
log.debug("Search exhausted all sources")
return None

Expand Down Expand Up @@ -1406,12 +1420,14 @@ def enable(plugin_name: str):
log.debug(f'{inst.name} is already running')
else:
log.error(f'reckless: {inst.name} failed to start!')
raise err
log.error(err)
return None
except RPCError:
log.debug(('lightningd rpc unavailable. '
'Skipping dynamic activation.'))
RECKLESS_CONFIG.enable_plugin(path)
log.info(f'{inst.name} enabled')
return 'enabled'


def disable(plugin_name: str):
Expand All @@ -1422,7 +1438,7 @@ def disable(plugin_name: str):
path = inst.entry
if not Path(path).exists():
sys.stderr.write(f'Could not find plugin at {path}\n')
sys.exit(1)
return None
log.debug(f'deactivating {plugin_name}')
try:
lightning_cli('plugin', 'stop', path)
Expand All @@ -1431,12 +1447,14 @@ def disable(plugin_name: str):
log.debug('plugin not currently running')
else:
log.error('lightning-cli plugin stop failed')
raise err
logging.error(err)
return None
except RPCError:
log.debug(('lightningd rpc unavailable. '
'Skipping dynamic deactivation.'))
RECKLESS_CONFIG.disable_plugin(path)
log.info(f'{inst.name} disabled')
return 'disabled'


def load_config(reckless_dir: Union[str, None] = None,
Expand Down Expand Up @@ -1516,18 +1534,17 @@ def add_source(src: str):
assert isinstance(src, str)
# Is it a file?
maybe_path = os.path.realpath(src)
sources = Config(path=str(get_sources_file()),
default_text='https://github.com/lightningd/plugins')
if Path(maybe_path).exists():
if os.path.isdir(maybe_path):
default_repo = 'https://github.com/lightningd/plugins'
my_file = Config(path=str(get_sources_file()),
default_text=default_repo)
my_file.editConfigFile(src, None)
sources.editConfigFile(src, None)
elif 'github.com' in src or 'http://' in src or 'https://' in src:
my_file = Config(path=str(get_sources_file()),
default_text='https://github.com/lightningd/plugins')
my_file.editConfigFile(src, None)
sources.editConfigFile(src, None)
else:
log.warning(f'failed to add source {src}')
return None
return sources_from_file()


def remove_source(src: str):
Expand All @@ -1540,12 +1557,14 @@ def remove_source(src: str):
log.info('plugin source removed')
else:
log.warning(f'source not found: {src}')
return sources_from_file()


def list_source():
"""Provide the user with all stored source repositories."""
for src in sources_from_file():
log.info(src)
return sources_from_file()


class StoreIdempotent(argparse.Action):
Expand Down Expand Up @@ -1718,8 +1737,7 @@ if __name__ == '__main__':
for target in args.targets:
log.add_result(args.func(target))
elif 'func' in args:
args.func()
log.add_result(args.func())

# reply with json if requested
if log.capture:
print(json.dumps(log.json_output, indent=4))
log.reply_json()

0 comments on commit 7c8f041

Please sign in to comment.