Skip to content

Commit b2ccfc0

Browse files
reckless: add listconfig command
Requested by @ShahanaFarooqui while accessing reckless via rpc in order to find out where the plugins are installed and enabled.
1 parent f58135f commit b2ccfc0

File tree

2 files changed

+58
-10
lines changed

2 files changed

+58
-10
lines changed

tests/test_reckless.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import PosixPath, Path
44
import socket
55
from pyln.testing.utils import VALGRIND, SLOW_MACHINE
6+
import json
67
import pytest
78
import os
89
import re
@@ -170,16 +171,30 @@ def test_basic_help():
170171
assert r.search_stdout("options:") or r.search_stdout("optional arguments:")
171172

172173

173-
def test_version():
174+
def test_reckless_version(node_factory):
174175
'''Version should be reported without loading config and should advance
175-
with lightningd'''
176-
r = reckless(["-V", "-v", "--json"])
176+
with lightningd.'''
177+
node = get_reckless_node(node_factory)
178+
r = reckless(["-V", "-v", "--json"], dir=node.lightning_dir)
177179
assert r.returncode == 0
178-
import json
179180
json_out = ''.join(r.stdout)
180181
with open('.version', 'r') as f:
181182
version = f.readlines()[0].strip()
182183
assert json.loads(json_out)['result'][0] == version
184+
assert not r.search_stdout('config file not found')
185+
186+
# reckless listconfig should report the reckless version as well.
187+
NETWORK = os.environ.get('TEST_NETWORK')
188+
if not NETWORK:
189+
NETWORK='regtest'
190+
r = reckless(['listconfig', f'--network={NETWORK}', '--json'],
191+
dir=node.lightning_dir)
192+
assert r.returncode == 0
193+
result = json.loads(''.join(r.stdout))['result']
194+
assert result['network'] == NETWORK
195+
assert result['reckless_dir'] == str(node.lightning_dir / 'reckless')
196+
assert result['lightning_conf'] == str(node.lightning_dir / NETWORK / 'config')
197+
assert result['version'] == version
183198

184199

185200
def test_contextual_help(node_factory):

tools/reckless

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,13 @@ class Logger:
9999

100100
def reply_json(self):
101101
"""json output to stdout with accumulated result."""
102-
if len(log.json_output["result"]) == 1 and \
103-
isinstance(log.json_output["result"][0], list):
104-
# unpack sources output
105-
log.json_output["result"] = log.json_output["result"][0]
102+
if len(log.json_output["result"]) == 1:
103+
if isinstance(log.json_output["result"][0], list):
104+
# unpack sources output
105+
log.json_output["result"] = log.json_output["result"][0]
106+
elif isinstance(log.json_output['result'][0], dict):
107+
# If result is only a single dict, unpack it from the result list
108+
log.json_output['result'] = log.json_output['result'][0]
106109
output = json.dumps(log.json_output, indent=3) + '\n'
107110
ratelimit_output(output)
108111

@@ -846,6 +849,8 @@ class RecklessConfig(Config):
846849
)
847850
Config.__init__(self, path=str(path), default_text=default_text)
848851
self.reckless_dir = Path(path).parent
852+
# Which lightning config needs to inherit the reckless config?
853+
self.lightning_conf = None
849854

850855

851856
class LightningBitcoinConfig(Config):
@@ -1817,6 +1822,7 @@ def load_config(reckless_dir: Union[str, None] = None,
18171822
reckless_abort('Error: could not load or create the network specific lightningd'
18181823
' config (default .lightning/bitcoin)')
18191824
net_conf.editConfigFile(f'include {reckless_conf.conf_fp}', None)
1825+
reckless_conf.lightning_conf = network_path
18201826
return reckless_conf
18211827

18221828

@@ -2118,6 +2124,31 @@ def available_plugins() -> list:
21182124
return candidates
21192125

21202126

2127+
def listconfig() -> dict:
2128+
"""Useful for checking options passed through the reckless-rpc."""
2129+
config = {}
2130+
2131+
log.info(f'requested lightning config: {LIGHTNING_CONFIG}')
2132+
config.update({'requested_lightning_conf': LIGHTNING_CONFIG})
2133+
2134+
log.info(f'lightning config in use: {RECKLESS_CONFIG.lightning_conf}')
2135+
config.update({'lightning_conf': str(RECKLESS_CONFIG.lightning_conf)})
2136+
2137+
log.info(f'lightning directory: {LIGHTNING_DIR}')
2138+
config.update({'lightning_dir': str(LIGHTNING_DIR)})
2139+
2140+
log.info(f'reckless directory: {RECKLESS_CONFIG.reckless_dir}')
2141+
config.update({'reckless_dir': str(RECKLESS_CONFIG.reckless_dir)})
2142+
2143+
log.info(f'network: {NETWORK}')
2144+
config.update({'network': NETWORK})
2145+
2146+
log.info(f'reckless version: {__VERSION__}')
2147+
config.update({'version': __VERSION__})
2148+
2149+
return config
2150+
2151+
21212152
def report_version() -> str:
21222153
"""return reckless version"""
21232154
log.info(__VERSION__)
@@ -2132,7 +2163,7 @@ def unpack_json_arg(json_target: str) -> list:
21322163
return None
21332164
if isinstance(targets, list):
21342165
return targets
2135-
log.warning(f'input {target_list} is not a json array')
2166+
log.warning(f'input {json_target} is not a json array')
21362167
return None
21372168

21382169

@@ -2231,10 +2262,12 @@ if __name__ == '__main__':
22312262
parser.add_argument('-V', '--version',
22322263
action=StoreTrueIdempotent, const=None,
22332264
help='print version and exit')
2265+
listconfig_cmd = cmd1.add_parser('listconfig', help='list options passed to reckless')
2266+
listconfig_cmd.set_defaults(func=listconfig)
22342267

22352268
all_parsers = [parser, install_cmd, uninstall_cmd, search_cmd, enable_cmd,
22362269
disable_cmd, list_parse, source_add, source_rem, help_cmd,
2237-
update, list_cmd, available_cmd]
2270+
update, list_cmd, available_cmd, listconfig_cmd]
22382271
for p in all_parsers:
22392272
# This default depends on the .lightning directory
22402273
p.add_argument('-d', '--reckless-dir', action=StoreIdempotent,

0 commit comments

Comments
 (0)