From 9a38699e551fb0f0b0422178a7505d26e0014bf6 Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Wed, 16 Dec 2020 22:20:59 +0100 Subject: [PATCH 1/2] Use api.Client for http request in src version This fixes us not using any authentication in the src version command, because the api.Client adds the Authorization header to the request automatically --- cmd/src/version.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/src/version.go b/cmd/src/version.go index a6e8f5f1db..a80d7269f3 100644 --- a/cmd/src/version.go +++ b/cmd/src/version.go @@ -1,12 +1,14 @@ package main import ( + "context" "encoding/json" "flag" "fmt" "io/ioutil" "net/http" - "net/url" + + "github.com/sourcegraph/src-cli/internal/api" ) // buildTag is the git tag at the time of build and is used to @@ -25,10 +27,13 @@ Examples: flagSet := flag.NewFlagSet("version", flag.ExitOnError) + var apiFlags = api.NewFlags(flagSet) + handler := func(args []string) error { fmt.Printf("Current version: %s\n", buildTag) - recommendedVersion, err := getRecommendedVersion() + client := cfg.apiClient(apiFlags, flagSet.Output()) + recommendedVersion, err := getRecommendedVersion(context.Background(), client) if err != nil { return err } @@ -53,20 +58,12 @@ Examples: }) } -func getRecommendedVersion() (string, error) { - url, err := url.Parse(cfg.Endpoint + "/.api/src-cli/version") +func getRecommendedVersion(ctx context.Context, client api.Client) (string, error) { + req, err := client.NewHTTPRequest(ctx, "GET", ".api/src-cli/version", nil) if err != nil { return "", err } - req, err := http.NewRequest("GET", url.String(), nil) - if err != nil { - return "", err - } - for k, v := range cfg.AdditionalHeaders { - req.Header.Set(k, v) - } - resp, err := http.DefaultClient.Do(req) if err != nil { return "", err From bf2f43117ae3ed2c8b459a63d91e75a1955ffb10 Mon Sep 17 00:00:00 2001 From: Erik Seliger Date: Wed, 16 Dec 2020 22:26:59 +0100 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e237c1035f..0698f8d5b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ All notable changes to `src-cli` are documented in this file. ### Fixed +- The src version command didn't send any authentication headers before, which could have failed for some instance configurations. The authentication header is now properly set for the request done in this command. [#411](https://github.com/sourcegraph/src-cli/pull/411) + ### Removed ## 3.23.0