Skip to content

Commit b2d3695

Browse files
committed
cmd: add formatting, color options to flux resource eventlog
Problem: `flux resource eventlog` doesn't have the same options and functionality as other eventlog commands. Make the options and functionality of `flux resource eventlog` match the standard Flux utility eventlog interface using the Python EventLogFormatter class.
1 parent f7a6251 commit b2d3695

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

src/cmd/flux-resource.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from itertools import combinations
1818

1919
import flux
20+
from flux.eventlog import EventLogFormatter
2021
from flux.hostlist import Hostlist
2122
from flux.idset import IDset
2223
from flux.resource import (
@@ -720,38 +721,47 @@ def emit_R(args):
720721
print(rset.encode())
721722

722723

723-
def print_events(events, follow, wait):
724+
def print_events(events, follow, wait, evf):
724725
if not events and not follow and not wait:
725726
return False
726727
for entry in events:
727-
print(json.dumps(entry))
728+
print(evf.format(entry))
728729
if wait and entry["name"] == wait:
729730
return False
730731
return True
731732

732733

733-
def eventlog_continuation(f, follow, wait):
734+
def eventlog_continuation(f, follow, wait, evf):
734735
try:
735736
payload = f.get()
736737
except OSError as exc:
737738
if exc.errno != errno.ENODATA:
738739
raise
739740
payload = None
740-
if not payload or not print_events(payload["events"], follow, wait):
741+
if not payload or not print_events(payload["events"], follow, wait, evf):
741742
f.flux_handle.reactor_stop()
742743
else:
743744
f.reset()
744745

745746

746747
def eventlog(args):
747748
"""Show the resource eventlog"""
749+
if args.human:
750+
args.format = "text"
751+
args.time_format = "human"
752+
if args.color is None:
753+
args.color = "auto"
754+
748755
h = flux.Flux()
756+
evf = EventLogFormatter(
757+
format=args.format, timestamp_format=args.time_format, color=args.color
758+
)
749759
f = h.rpc(
750760
"resource.journal",
751761
nodeid=0,
752762
flags=flux.constants.FLUX_RPC_STREAMING,
753763
)
754-
f.then(eventlog_continuation, args.follow, args.wait)
764+
f.then(eventlog_continuation, args.follow, args.wait, evf)
755765
h.reactor_run()
756766

757767

@@ -1047,6 +1057,36 @@ def main():
10471057
eventlog_parser = subparsers.add_parser(
10481058
"eventlog", formatter_class=flux.util.help_formatter()
10491059
)
1060+
eventlog_parser.add_argument(
1061+
"-f",
1062+
"--format",
1063+
default="text",
1064+
metavar="FORMAT",
1065+
choices=["text", "json"],
1066+
help="Specify output format: text, json",
1067+
)
1068+
eventlog_parser.add_argument(
1069+
"-T",
1070+
"--time-format",
1071+
default="raw",
1072+
metavar="FORMAT",
1073+
choices=["raw", "iso", "offset", "human", "reltime"],
1074+
help="Specify time format: raw, iso, offset, human",
1075+
)
1076+
eventlog_parser.add_argument(
1077+
"-H", "--human", action="store_true", help="Display human-readable output."
1078+
)
1079+
eventlog_parser.add_argument(
1080+
"-L",
1081+
"--color",
1082+
type=str,
1083+
metavar="WHEN",
1084+
choices=["never", "always", "auto"],
1085+
nargs="?",
1086+
const="always",
1087+
default="auto",
1088+
help="Use color; WHEN can be 'never', 'always', or 'auto' (default)",
1089+
)
10501090
eventlog_parser.add_argument(
10511091
"-F",
10521092
"--follow",

0 commit comments

Comments
 (0)