Skip to content

Commit 93f743a

Browse files
committed
tcf/cookies: move command implementation to ui_cli_servers and new API framework
Cleans up a wee bit the output in the case of multiple servers so it is not confusing.
1 parent ef736a0 commit 93f743a

File tree

3 files changed

+61
-37
lines changed

3 files changed

+61
-37
lines changed

tcf

+1-37
Original file line numberDiff line numberDiff line change
@@ -243,31 +243,6 @@ def _cache_flush(_args):
243243

244244

245245

246-
def _cookies(args):
247-
if '/' in args.target:
248-
# cache is by full id
249-
rtb = ttb_client.rest_target_broker.rts_cache[args.target]['rtb']
250-
else:
251-
for rt_fullid, rt in ttb_client.rest_target_broker.rts_cache.items():
252-
if rt['id'] == args.target:
253-
rtb = rt['rtb']
254-
break
255-
else:
256-
raise ValueError("%s: unknown target" % args.target)
257-
258-
if args.json:
259-
json.dump(rtb.cookies, sys.stdout, indent = 4)
260-
print()
261-
elif args.cookiejar:
262-
# Follow https://curl.se/docs/http-cookies.html
263-
# Note we don't keep the TTL field, so we set it at zero
264-
for cookie, value in rtb.cookies.items():
265-
print(f"{rtb.parsed_url.hostname}\tFALSE\t/\tTRUE\t0"
266-
f"\t{cookie}\t{value}")
267-
else:
268-
commonl._dict_print_dotted(rtb.cookies, separator = "")
269-
270-
271246
if __name__ == "__main__":
272247
tcfl.tc.version = commonl.version_get(tcfl, "tcf")
273248

@@ -490,18 +465,7 @@ if __name__ == "__main__":
490465

491466

492467
# advanced commands
493-
494-
ap = arg_subparsers.add_parser("cookies",
495-
help = "Show logging cookies (to feed"
496-
" into curl, etc) maybe only for one server")
497-
ap.add_argument("target", metavar = "TARGET", action = "store",
498-
default = [], help = "Target name")
499-
ap.add_argument("-c","--cookiejar", action = "store_true", default = False,
500-
help = "Print in cookiejar format"
501-
" (https://curl.se/docs/http-cookies.html)")
502-
ap.add_argument("-j","--json", action = "store_true", default = False,
503-
help = "Print in JSON format")
504-
ap.set_defaults(func = _cookies)
468+
tcfl.ui_cli_servers.cmdline_setup_advanced(arg_subparsers)
505469

506470
import tcfl.ui_cli_users
507471
tcfl.ui_cli_users.cmdline_setup_advanced(arg_subparsers)

tcfl/servers.py

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def by_targetspec(targetspec: list = None, verbosity: int = 0):
6969
7070
"""
7171
if targetspec:
72+
import tcfl.targets # dependency loop otherwise
7273
# we are given a list of targets to look for their servers or
7374
# default to all, so pass it on to initialize the inventory
7475
# system so we can filter

tcfl/ui_cli_servers.py

+59
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ def _logged_in_username(_server_name, server):
4242
return server.logged_in_username()
4343

4444

45+
46+
def _cookies(_server_name, server,
47+
_cli_args: argparse.Namespace):
48+
return server.state_load()
49+
50+
def _cmdline_cookies(cli_args: argparse.Namespace):
51+
52+
tcfl.ui_cli.logger_verbosity_from_cli(logger, cli_args)
53+
verbosity = cli_args.verbosity - cli_args.quietosity
54+
servers = tcfl.servers.by_targetspec(
55+
cli_args.target, verbosity = verbosity)
56+
57+
r = tcfl.servers.run_fn_on_each_server(
58+
servers, _cookies, cli_args,
59+
parallelization_factor = cli_args.parallelization_factor,
60+
traces = cli_args.traces)
61+
# r now is a dict keyed by server_name of tuples cookies, exception
62+
if cli_args.json:
63+
d = {}
64+
for server_name, ( cookies, _e ) in r.items():
65+
d[server_name] = cookies
66+
json.dump(d, sys.stdout, indent = 4)
67+
print()
68+
elif cli_args.cookiejar:
69+
# Follow https://curl.se/docs/http-cookies.html
70+
# Note we don't keep the TTL field, so we set it at zero
71+
for server_name, ( cookies, _e ) in r.items():
72+
for cookie, value in cookies.items():
73+
print(f"{server_name}\tFALSE\t/\tTRUE\t0"
74+
f"\t{cookie}\t{value}")
75+
else:
76+
d = {}
77+
for server_name, ( cookies, _e ) in r.items():
78+
d[server_name] = cookies
79+
if len(d) == 1: # print less info if there is only one
80+
commonl._dict_print_dotted(d[server_name], separator = ".")
81+
else:
82+
commonl._dict_print_dotted(d, separator = ".")
83+
84+
85+
4586
def _cmdline_servers(cli_args: argparse.Namespace):
4687
import tcfl.servers
4788
import tcfl.targets
@@ -232,3 +273,21 @@ def cmdline_setup(arg_subparser):
232273
help = "Flush currently cached/known servers"
233274
" (might need to servers-discover after)")
234275
ap.set_defaults(func = _cmdline_servers_flush)
276+
277+
278+
279+
def cmdline_setup_advanced(arg_subparser):
280+
281+
ap = arg_subparser.add_parser(
282+
"cookies",
283+
help = "Show login cookies (to feed into curl, etc)")
284+
tcfl.ui_cli.args_verbosity_add(ap)
285+
tcfl.ui_cli.args_targetspec_add(ap)
286+
ap.add_argument(
287+
"-c","--cookiejar", action = "store_true", default = False,
288+
help = "Print in cookiejar format"
289+
" (https://curl.se/docs/http-cookies.html)")
290+
ap.add_argument(
291+
"-j","--json", action = "store_true", default = False,
292+
help = "Print in JSON format")
293+
ap.set_defaults(func = _cmdline_cookies)

0 commit comments

Comments
 (0)