From 76d0345d0fe120fb1c6dd5278fc97a7b4c6f83a0 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Tue, 12 Jan 2021 16:27:04 -0600 Subject: [PATCH 1/7] Update link in error hint to non-broken docs. --- cmd/src/lsif_upload.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 7f3d785d64..3513c466b3 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -208,7 +208,7 @@ Examples: if isatty.IsTerminal(os.Stdout.Fd()) { fmt.Println("You may need to specify or update your GitHub access token to use this endpoint.") - fmt.Println("See https://github.com/sourcegraph/src-cli#authentication.") + fmt.Println("See https://docs.sourcegraph.com/cli/references/lsif/upload.") } } From 8a7898ecc73b7143c5d7b9f0c881aab7bed47882 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Tue, 12 Jan 2021 16:27:40 -0600 Subject: [PATCH 2/7] Do not return a canned 401 error that removes the response body text. --- cmd/src/lsif_upload.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 3513c466b3..f458ad8c09 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -202,10 +202,6 @@ Examples: wg.Wait() // Wait for progress bar goroutine to clear screen if err != nil { if err == codeintelutils.ErrUnauthorized { - if *flags.gitHubToken == "" { - return fmt.Errorf("you must provide -github-token=TOKEN, where TOKEN is a GitHub personal access token with 'repo' or 'public_repo' scope") - } - if isatty.IsTerminal(os.Stdout.Fd()) { fmt.Println("You may need to specify or update your GitHub access token to use this endpoint.") fmt.Println("See https://docs.sourcegraph.com/cli/references/lsif/upload.") From 0cbcbebef6364ffb8b7b660b0ce475616f301c0d Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Tue, 12 Jan 2021 18:20:39 -0600 Subject: [PATCH 3/7] Add trace logging to src lsif upload. --- cmd/src/lsif_upload.go | 101 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 7 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index f458ad8c09..bdb060fd6c 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -4,14 +4,15 @@ import ( "encoding/json" "flag" "fmt" + "net/http" "net/url" "os" + "sort" "strings" "sync" "time" "github.com/efritz/pentimento" - "github.com/mattn/go-isatty" "github.com/pkg/browser" "github.com/pkg/errors" "github.com/sourcegraph/codeintelutils" @@ -52,6 +53,8 @@ Examples: maxPayloadSizeMb *int ignoreUploadFailures *bool uploadRoute *string + rawVerbosity *int + verbosity lsifUploadVerbosity } flagSet := flag.NewFlagSet("upload", flag.ExitOnError) @@ -67,6 +70,7 @@ Examples: flags.maxPayloadSizeMb = flagSet.Int("max-payload-size", 100, `The maximum upload size (in megabytes). Indexes exceeding this limit will be uploaded over multiple HTTP requests.`) flags.ignoreUploadFailures = flagSet.Bool("ignore-upload-failure", false, `Exit with status code zero on upload failure.`) flags.uploadRoute = flagSet.String("upload-route", "/.api/lsif/upload", "The path of the upload route.") + flags.rawVerbosity = flagSet.Int("v", 0, "-v=0 shows no logs; -v=1 shows requests and response metadata; -v=2 shows headers, -v=3 shows response body") parseAndValidateFlags := func(args []string) error { flagSet.Parse(args) @@ -146,6 +150,11 @@ Examples: return errors.New("max-payload-size must be positive") } + // Don't need to check upper bounds as we only compare verbosity ranges + // It's fine if someone supplies -v=42, but it will just behave the same + // as if they supplied the highest verbosity level we define internally. + flags.verbosity = lsifUploadVerbosity(*flags.rawVerbosity) + if !*flags.json { fmt.Println(argsString) } @@ -173,6 +182,7 @@ Examples: MaxRetries: 10, RetryInterval: time.Millisecond * 250, UploadProgressEvents: make(chan codeintelutils.UploadProgressEvent), + Logger: &lsifUploadRequestLogger{verbosity: flags.verbosity}, } var wg sync.WaitGroup @@ -181,7 +191,7 @@ Examples: go func() { defer wg.Done() - if *flags.json || *flags.noProgress { + if *flags.json || *flags.noProgress || flags.verbosity > 0 { return } @@ -202,15 +212,19 @@ Examples: wg.Wait() // Wait for progress bar goroutine to clear screen if err != nil { if err == codeintelutils.ErrUnauthorized { - if isatty.IsTerminal(os.Stdout.Fd()) { - fmt.Println("You may need to specify or update your GitHub access token to use this endpoint.") - fmt.Println("See https://docs.sourcegraph.com/cli/references/lsif/upload.") + err = errorWithHint{ + err: err, hint: strings.Join([]string{ + "You may need to specify or update your GitHub access token to use this endpoint.", + "See https://docs.sourcegraph.com/cli/references/lsif/upload.", + }, "\n"), } + } if *flags.ignoreUploadFailures { - fmt.Printf("error: %s\n", err) - return nil + // Report but don't return + fmt.Println(err.Error()) + err = nil } return err @@ -308,3 +322,76 @@ func digits(n int) int { } return 1 } + +type errorWithHint struct { + err error + hint string +} + +func (e errorWithHint) Error() string { + return fmt.Sprintf("error: %s\n\n%s\n", e.err, e.hint) +} + +type lsifUploadVerbosity int + +const ( + lsifUploadVerbosityNone lsifUploadVerbosity = iota // -v=0 (default) + lsifUploadVerbosityTrace // -v=1 + lsifUploadVerbosityTraceShowHeaders // -v=2 + lsifUploadVerbosityTraceShowResponseBody // -v=3 +) + +type lsifUploadRequestLogger struct { + verbosity lsifUploadVerbosity +} + +func (l *lsifUploadRequestLogger) LogRequest(req *http.Request) { + if l.verbosity == lsifUploadVerbosityNone { + return + } + + if l.verbosity >= lsifUploadVerbosityTrace { + fmt.Printf("> %s %s\n", req.Method, req.URL) + } + + if l.verbosity >= lsifUploadVerbosityTraceShowHeaders { + fmt.Printf("> Request Headers:\n") + for _, k := range sortHeaders(req.Header) { + fmt.Printf("> %s: %s\n", k, req.Header[k]) + } + } + + fmt.Printf("\n") +} + +func (l *lsifUploadRequestLogger) LogResponse(req *http.Request, resp *http.Response, body []byte, elapsed time.Duration) { + if l.verbosity == lsifUploadVerbosityNone { + return + } + + if l.verbosity >= lsifUploadVerbosityTrace { + fmt.Printf("< %s %s %s in %s\n", req.Method, req.URL, resp.Status, elapsed) + } + + if l.verbosity >= lsifUploadVerbosityTraceShowHeaders { + fmt.Printf("< Response Headers:\n") + for _, k := range sortHeaders(resp.Header) { + fmt.Printf("< %s: %s\n", k, resp.Header[k]) + } + } + + if l.verbosity >= lsifUploadVerbosityTraceShowResponseBody { + fmt.Printf("< Response Body: %s\n", body) + } + + fmt.Printf("\n") +} + +func sortHeaders(header http.Header) []string { + var keys []string + for k := range header { + keys = append(keys, k) + } + sort.Strings(keys) + return keys +} From b51e386fa55226741c51aacd4c3921ba9bd1bd94 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Wed, 13 Jan 2021 11:16:15 -0600 Subject: [PATCH 4/7] Update codeintelutils. --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ebe9209de1..e12b4fb797 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 github.com/pkg/errors v0.9.1 github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2 - github.com/sourcegraph/codeintelutils v0.0.0-20201118031531-b82ba3167b30 + github.com/sourcegraph/codeintelutils v0.0.0-20210113171425-9ec641b48a8e github.com/sourcegraph/go-diff v0.6.1 github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect diff --git a/go.sum b/go.sum index d569c1db34..bcbbeb1b3a 100644 --- a/go.sum +++ b/go.sum @@ -52,8 +52,8 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxr github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ= github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2 h1:MJu/6WzWdPegzYnZLb04IS0u4VyUpPIAHQyWT5i2vR8= github.com/sourcegraph/campaignutils v0.0.0-20201124055807-2f9cfa9317e2/go.mod h1:xm6i78Mk2t4DBLQDqEFc/3x6IPf7yYZCgbNaTQGhJHA= -github.com/sourcegraph/codeintelutils v0.0.0-20201118031531-b82ba3167b30 h1:HrRrPyskdkHc6MqQS3ehH+DSraFnOhtvBWQ6AzEJC1o= -github.com/sourcegraph/codeintelutils v0.0.0-20201118031531-b82ba3167b30/go.mod h1:HplI8gRslTrTUUsSYwu28hSOderix7m5dHNca7xBzeo= +github.com/sourcegraph/codeintelutils v0.0.0-20210113171425-9ec641b48a8e h1:PdNc6fH0HHQ5xbnCwPkHuFdVCofQilFm9gG40fEQKms= +github.com/sourcegraph/codeintelutils v0.0.0-20210113171425-9ec641b48a8e/go.mod h1:HplI8gRslTrTUUsSYwu28hSOderix7m5dHNca7xBzeo= github.com/sourcegraph/go-diff v0.6.1 h1:hmA1LzxW0n1c3Q4YbrFgg4P99GSnebYa3x8gr0HZqLQ= github.com/sourcegraph/go-diff v0.6.1/go.mod h1:iBszgVvyxdc8SFZ7gm69go2KDdt3ag071iBaWPF6cjs= github.com/sourcegraph/jsonx v0.0.0-20200629203448-1a936bd500cf h1:oAdWFqhStsWiiMP/vkkHiMXqFXzl1XfUNOdxKJbd6bI= From dbbfccd9bf6f8244df3d823a9edf17b9901cff11 Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Wed, 13 Jan 2021 11:20:51 -0600 Subject: [PATCH 5/7] Update changelog. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83e0219bad..8c8ccceb83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ All notable changes to `src-cli` are documented in this file. ### Added +- Add verbosity flag to `lsif upload` action. Supply `-v=1`, `-v=2`, or `-v=3` to the action to specify verbosity. + ### Changed ### Fixed From d26d5e4e97b21f3aa0aa6563c0bebc677a362d9c Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Wed, 13 Jan 2021 11:36:05 -0600 Subject: [PATCH 6/7] Update var. --- CHANGELOG.md | 2 +- cmd/src/lsif_upload.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c8ccceb83..8955ce687e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ All notable changes to `src-cli` are documented in this file. ### Added -- Add verbosity flag to `lsif upload` action. Supply `-v=1`, `-v=2`, or `-v=3` to the action to specify verbosity. +- Add verbosity flag to `lsif upload` action. Supply `-trace=1`, `-trace=2`, or `-trace=3` to the action to specify verbosity. ### Changed diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index bdb060fd6c..93dfbfeba3 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -70,7 +70,7 @@ Examples: flags.maxPayloadSizeMb = flagSet.Int("max-payload-size", 100, `The maximum upload size (in megabytes). Indexes exceeding this limit will be uploaded over multiple HTTP requests.`) flags.ignoreUploadFailures = flagSet.Bool("ignore-upload-failure", false, `Exit with status code zero on upload failure.`) flags.uploadRoute = flagSet.String("upload-route", "/.api/lsif/upload", "The path of the upload route.") - flags.rawVerbosity = flagSet.Int("v", 0, "-v=0 shows no logs; -v=1 shows requests and response metadata; -v=2 shows headers, -v=3 shows response body") + flags.rawVerbosity = flagSet.Int("trace", 0, "-trace=0 shows no logs; -trace=1 shows requests and response metadata; -trace=2 shows headers, -trace=3 shows response body") parseAndValidateFlags := func(args []string) error { flagSet.Parse(args) From 035a8836ba1aa037602e1a39a30b66ffb4d4cd1c Mon Sep 17 00:00:00 2001 From: Eric Fritz Date: Wed, 13 Jan 2021 11:37:50 -0600 Subject: [PATCH 7/7] Fix other references. --- cmd/src/lsif_upload.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/src/lsif_upload.go b/cmd/src/lsif_upload.go index 93dfbfeba3..e653321af3 100644 --- a/cmd/src/lsif_upload.go +++ b/cmd/src/lsif_upload.go @@ -151,8 +151,9 @@ Examples: } // Don't need to check upper bounds as we only compare verbosity ranges - // It's fine if someone supplies -v=42, but it will just behave the same - // as if they supplied the highest verbosity level we define internally. + // It's fine if someone supplies -trace=42, but it will just behave the + // same as if they supplied the highest verbosity level we define + // internally. flags.verbosity = lsifUploadVerbosity(*flags.rawVerbosity) if !*flags.json { @@ -335,10 +336,10 @@ func (e errorWithHint) Error() string { type lsifUploadVerbosity int const ( - lsifUploadVerbosityNone lsifUploadVerbosity = iota // -v=0 (default) - lsifUploadVerbosityTrace // -v=1 - lsifUploadVerbosityTraceShowHeaders // -v=2 - lsifUploadVerbosityTraceShowResponseBody // -v=3 + lsifUploadVerbosityNone lsifUploadVerbosity = iota // -trace=0 (default) + lsifUploadVerbosityTrace // -trace=1 + lsifUploadVerbosityTraceShowHeaders // -trace=2 + lsifUploadVerbosityTraceShowResponseBody // -trace=3 ) type lsifUploadRequestLogger struct {