Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use api.Client for http request in src version #411

Merged
merged 2 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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