From 2a80ff0bc26524b0e7825ebd55e7057d16ff7c94 Mon Sep 17 00:00:00 2001 From: Michael Hohn Date: Thu, 8 Feb 2024 13:32:14 -0800 Subject: [PATCH] Added request/response logger to main for expansion --- README.org | 22 +++++++++++++++++++--- main.go | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.org b/README.org index 06bc878..b55721e 100644 --- a/README.org +++ b/README.org @@ -97,9 +97,10 @@ cd ~/local/gh-mrva # Build it go mod edit -replace="github.com/GitHubSecurityLab/gh-mrva=/Users/hohn/local/gh-mrva" - go build + go build . # Install + gh extension remove mrva gh extension install . # Sanity check @@ -128,7 +129,7 @@ #+BEGIN_SRC sh gh mrva submit --help - gh mrva submit --language cpp --session mirva-session-1 \ + gh mrva submit --language cpp --session mirva-session-4 \ --list mirva-list \ --query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql #+END_SRC @@ -181,4 +182,19 @@ The workflow producing the logs: https://github.com/github/codeql-variant-analysis-action/blob/main/variant-analysis-workflow.yml - +** Compacted Edit-Run-Debug Cycle + With a full [[*Using MRVA][Using MRVA]] cycle done, only these steps are needed in a + edit-run-debug cycle. + #+BEGIN_SRC sh + cd ~/local/gh-mrva + + # Build it + go build . + + ./gh-mrva -h + + # Submit the mrva job + ./gh-mrva submit --language cpp --session mirva-session-6 \ + --list mirva-list \ + --query /Users/hohn/local/gh-mrva/FlatBuffersFunc.ql + #+END_SRC diff --git a/main.go b/main.go index 7da83ab..2e08a2a 100644 --- a/main.go +++ b/main.go @@ -22,10 +22,46 @@ THE SOFTWARE. package main import ( + "log" + "net/http" + "time" + "github.com/GitHubSecurityLab/gh-mrva/cmd" - _ "github.com/motemen/go-loghttp/global" + + "github.com/motemen/go-loghttp" + "github.com/motemen/go-nuts/roundtime" ) func main() { + var transport = &loghttp.Transport{ + Transport: http.DefaultTransport, + LogRequest: LogRequestDump, + LogResponse: LogResponseDump, + } + + http.DefaultTransport = transport + cmd.Execute() } + +// Used if transport.LogRequest is not set. +func LogRequestDump(req *http.Request) { + log.Printf(">> %s %s", req.Method, req.URL) +} + +type contextKey struct { + name string +} + +var ContextKeyRequestStart = &contextKey{"RequestStart"} + +// Used if transport.LogResponse is not set. +func LogResponseDump(resp *http.Response) { + ctx := resp.Request.Context() + if start, ok := ctx.Value(ContextKeyRequestStart).(time.Time); ok { + log.Printf("<< %d %s (%s)", resp.StatusCode, resp.Request.URL, + roundtime.Duration(time.Since(start), 2)) + } else { + log.Printf("<< %d %s", resp.StatusCode, resp.Request.URL) + } +}