Skip to content

Commit

Permalink
tcf/cookies: move command implementation to ui_cli_servers and new AP…
Browse files Browse the repository at this point in the history
…I framework

Cleans up a wee bit the output in the case of multiple servers so it
is not confusing.
  • Loading branch information
inaky-intc committed Jun 2, 2023
1 parent ef736a0 commit 93f743a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
38 changes: 1 addition & 37 deletions tcf
Original file line number Diff line number Diff line change
Expand Up @@ -243,31 +243,6 @@ def _cache_flush(_args):



def _cookies(args):
if '/' in args.target:
# cache is by full id
rtb = ttb_client.rest_target_broker.rts_cache[args.target]['rtb']
else:
for rt_fullid, rt in ttb_client.rest_target_broker.rts_cache.items():
if rt['id'] == args.target:
rtb = rt['rtb']
break
else:
raise ValueError("%s: unknown target" % args.target)

if args.json:
json.dump(rtb.cookies, sys.stdout, indent = 4)
print()
elif args.cookiejar:
# Follow https://curl.se/docs/http-cookies.html
# Note we don't keep the TTL field, so we set it at zero
for cookie, value in rtb.cookies.items():
print(f"{rtb.parsed_url.hostname}\tFALSE\t/\tTRUE\t0"
f"\t{cookie}\t{value}")
else:
commonl._dict_print_dotted(rtb.cookies, separator = "")


if __name__ == "__main__":
tcfl.tc.version = commonl.version_get(tcfl, "tcf")

Expand Down Expand Up @@ -490,18 +465,7 @@ if __name__ == "__main__":


# advanced commands

ap = arg_subparsers.add_parser("cookies",
help = "Show logging cookies (to feed"
" into curl, etc) maybe only for one server")
ap.add_argument("target", metavar = "TARGET", action = "store",
default = [], help = "Target name")
ap.add_argument("-c","--cookiejar", action = "store_true", default = False,
help = "Print in cookiejar format"
" (https://curl.se/docs/http-cookies.html)")
ap.add_argument("-j","--json", action = "store_true", default = False,
help = "Print in JSON format")
ap.set_defaults(func = _cookies)
tcfl.ui_cli_servers.cmdline_setup_advanced(arg_subparsers)

import tcfl.ui_cli_users
tcfl.ui_cli_users.cmdline_setup_advanced(arg_subparsers)
Expand Down
1 change: 1 addition & 0 deletions tcfl/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def by_targetspec(targetspec: list = None, verbosity: int = 0):
"""
if targetspec:
import tcfl.targets # dependency loop otherwise
# we are given a list of targets to look for their servers or
# default to all, so pass it on to initialize the inventory
# system so we can filter
Expand Down
59 changes: 59 additions & 0 deletions tcfl/ui_cli_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,47 @@ def _logged_in_username(_server_name, server):
return server.logged_in_username()



def _cookies(_server_name, server,
_cli_args: argparse.Namespace):
return server.state_load()

def _cmdline_cookies(cli_args: argparse.Namespace):

tcfl.ui_cli.logger_verbosity_from_cli(logger, cli_args)
verbosity = cli_args.verbosity - cli_args.quietosity
servers = tcfl.servers.by_targetspec(
cli_args.target, verbosity = verbosity)

r = tcfl.servers.run_fn_on_each_server(
servers, _cookies, cli_args,
parallelization_factor = cli_args.parallelization_factor,
traces = cli_args.traces)
# r now is a dict keyed by server_name of tuples cookies, exception
if cli_args.json:
d = {}
for server_name, ( cookies, _e ) in r.items():
d[server_name] = cookies
json.dump(d, sys.stdout, indent = 4)
print()
elif cli_args.cookiejar:
# Follow https://curl.se/docs/http-cookies.html
# Note we don't keep the TTL field, so we set it at zero
for server_name, ( cookies, _e ) in r.items():
for cookie, value in cookies.items():
print(f"{server_name}\tFALSE\t/\tTRUE\t0"
f"\t{cookie}\t{value}")
else:
d = {}
for server_name, ( cookies, _e ) in r.items():
d[server_name] = cookies
if len(d) == 1: # print less info if there is only one
commonl._dict_print_dotted(d[server_name], separator = ".")
else:
commonl._dict_print_dotted(d, separator = ".")



def _cmdline_servers(cli_args: argparse.Namespace):
import tcfl.servers
import tcfl.targets
Expand Down Expand Up @@ -232,3 +273,21 @@ def cmdline_setup(arg_subparser):
help = "Flush currently cached/known servers"
" (might need to servers-discover after)")
ap.set_defaults(func = _cmdline_servers_flush)



def cmdline_setup_advanced(arg_subparser):

ap = arg_subparser.add_parser(
"cookies",
help = "Show login cookies (to feed into curl, etc)")
tcfl.ui_cli.args_verbosity_add(ap)
tcfl.ui_cli.args_targetspec_add(ap)
ap.add_argument(
"-c","--cookiejar", action = "store_true", default = False,
help = "Print in cookiejar format"
" (https://curl.se/docs/http-cookies.html)")
ap.add_argument(
"-j","--json", action = "store_true", default = False,
help = "Print in JSON format")
ap.set_defaults(func = _cmdline_cookies)

0 comments on commit 93f743a

Please sign in to comment.