|
| 1 | +(module salmonella-log-inquirer () |
| 2 | + |
| 3 | +(import chicken scheme) |
| 4 | +(use data-structures files extras) |
| 5 | +(use salmonella salmonella-log-parser) |
| 6 | +(include "salmonella-version.scm") |
| 7 | +(include "salmonella-common.scm") |
| 8 | + |
| 9 | +(define valid-actions '(fetch install check-version test meta-data)) |
| 10 | +(define valid-parts '(message status duration)) |
| 11 | + |
| 12 | +(define (query-action log-data egg action part) |
| 13 | + (log-get egg |
| 14 | + action |
| 15 | + (case part |
| 16 | + ((message) report-message) |
| 17 | + ((status) report-status) |
| 18 | + ((duration) report-duration) |
| 19 | + (else (error 'query-action "Invalid part" part))) |
| 20 | + log-data)) |
| 21 | + |
| 22 | +(define (log-statistics log-data) |
| 23 | + #<#EOF |
| 24 | +=== Summary |
| 25 | +Total eggs: #(count-total-eggs log-data) |
| 26 | + |
| 27 | +==== Installation |
| 28 | +Ok: #(count-install-ok log-data) |
| 29 | +Failed: #(count-install-fail log-data) |
| 30 | + |
| 31 | +==== Tests |
| 32 | +Ok: #(count-test-ok log-data) |
| 33 | +Failed: #(count-test-fail log-data) |
| 34 | +No tests: #(count-no-test log-data) |
| 35 | + |
| 36 | +==== Documentation |
| 37 | +Documented: #(count-documented log-data) |
| 38 | +Undocumented: #(count-undocumented log-data) |
| 39 | + |
| 40 | +==== Total run time |
| 41 | +#(prettify-time (inexact->exact (total-time log-data))) |
| 42 | +EOF |
| 43 | +) |
| 44 | + |
| 45 | +(define (read-log args) |
| 46 | + (let ((log-file (last args))) |
| 47 | + (read-log-file log-file))) |
| 48 | + |
| 49 | +(define (usage #!optional exit-code) |
| 50 | + (let ((this (pathname-strip-directory (program-name))) |
| 51 | + (port (if (and exit-code (not (zero? exit-code))) |
| 52 | + (current-error-port) |
| 53 | + (current-output-port)))) |
| 54 | + (display #<#EOF |
| 55 | +#this [ -h | --help ] |
| 56 | +#this --version |
| 57 | +#this --log-info <log-file> |
| 58 | +#this --statistics <log-file> |
| 59 | +#this --list-eggs <log file> |
| 60 | +#this --action=<action> --egg=<egg> [ --part=<part> ] <log file> |
| 61 | + |
| 62 | +<action>s: |
| 63 | +#(string-intersperse (map symbol->string valid-actions) "\n") |
| 64 | + |
| 65 | +<part>s (the default part is "message"): |
| 66 | +#(string-intersperse (map symbol->string valid-parts) "\n") |
| 67 | +EOF |
| 68 | + port) |
| 69 | + (newline) |
| 70 | + (when exit-code (exit exit-code)))) |
| 71 | + |
| 72 | + |
| 73 | +(let ((args (command-line-arguments))) |
| 74 | + (cond |
| 75 | + ((null? args) |
| 76 | + (usage 1)) |
| 77 | + ((or (member "-h" args) |
| 78 | + (member "-help" args) |
| 79 | + (member "--help" args)) |
| 80 | + (usage 0)) |
| 81 | + ((member "--version" args) |
| 82 | + (print salmonella-version)) |
| 83 | + ((null? (cdr args)) ;; from now on we require the log file |
| 84 | + (usage 1)) |
| 85 | + ((member "--list-eggs" args) |
| 86 | + (for-each print (log-eggs (read-log args)))) |
| 87 | + ((member "--log-info" args) |
| 88 | + (print (salmonella-info (read-log args)))) |
| 89 | + ((member "--statistics" args) |
| 90 | + (print (log-statistics (read-log args)))) |
| 91 | + (else |
| 92 | + (let ((action-str (cmd-line-arg '--action args)) |
| 93 | + (egg-str (cmd-line-arg '--egg args)) |
| 94 | + (part-str (cmd-line-arg '--part args))) |
| 95 | + (unless action-str |
| 96 | + (die "Missing --action=<action>")) |
| 97 | + (unless egg-str |
| 98 | + (die "Missing --egg=<egg>")) |
| 99 | + (let ((action (string->symbol action-str)) |
| 100 | + (egg (string->symbol egg-str)) |
| 101 | + (part (if part-str |
| 102 | + (string->symbol part-str) |
| 103 | + 'message))) |
| 104 | + (unless (memq action valid-actions) |
| 105 | + (die "Invalid action: " action)) |
| 106 | + (unless (memq part valid-parts) |
| 107 | + (die "Invalid part: " part)) |
| 108 | + (let ((printer (if (eq? action 'meta-data) |
| 109 | + pp |
| 110 | + print))) |
| 111 | + (printer (query-action (read-log args) egg action part))))))) |
| 112 | + ) |
| 113 | +) ;; end module |
0 commit comments