Skip to content

Commit

Permalink
Use api.Client for http request in src version (#411)
Browse files Browse the repository at this point in the history
This fixes us not using any authentication in the src version command, because the api.Client adds the Authorization header to the request automatically.
  • Loading branch information
eseliger authored Dec 16, 2020
1 parent 24485fc commit 70311d2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 9 additions & 12 deletions cmd/src/version.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 70311d2

Please sign in to comment.