Skip to content

Commit d12fcd4

Browse files
authored
Use api.Client for http request in src version (#411)
This fixes us not using any authentication in the src version command, because the api.Client adds the Authorization header to the request automatically.
1 parent 7e6cf14 commit d12fcd4

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ All notable changes to `src-cli` are documented in this file.
1717

1818
### Fixed
1919

20+
- 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)
21+
2022
### Removed
2123

2224
## 3.23.0

cmd/src/version.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package main
22

33
import (
4+
"context"
45
"encoding/json"
56
"flag"
67
"fmt"
78
"io/ioutil"
89
"net/http"
9-
"net/url"
10+
11+
"github.com/sourcegraph/src-cli/internal/api"
1012
)
1113

1214
// buildTag is the git tag at the time of build and is used to
@@ -25,10 +27,13 @@ Examples:
2527

2628
flagSet := flag.NewFlagSet("version", flag.ExitOnError)
2729

30+
var apiFlags = api.NewFlags(flagSet)
31+
2832
handler := func(args []string) error {
2933
fmt.Printf("Current version: %s\n", buildTag)
3034

31-
recommendedVersion, err := getRecommendedVersion()
35+
client := cfg.apiClient(apiFlags, flagSet.Output())
36+
recommendedVersion, err := getRecommendedVersion(context.Background(), client)
3237
if err != nil {
3338
return err
3439
}
@@ -53,20 +58,12 @@ Examples:
5358
})
5459
}
5560

56-
func getRecommendedVersion() (string, error) {
57-
url, err := url.Parse(cfg.Endpoint + "/.api/src-cli/version")
61+
func getRecommendedVersion(ctx context.Context, client api.Client) (string, error) {
62+
req, err := client.NewHTTPRequest(ctx, "GET", ".api/src-cli/version", nil)
5863
if err != nil {
5964
return "", err
6065
}
6166

62-
req, err := http.NewRequest("GET", url.String(), nil)
63-
if err != nil {
64-
return "", err
65-
}
66-
for k, v := range cfg.AdditionalHeaders {
67-
req.Header.Set(k, v)
68-
}
69-
7067
resp, err := http.DefaultClient.Do(req)
7168
if err != nil {
7269
return "", err

0 commit comments

Comments
 (0)