Skip to content

Commit

Permalink
relax the access token requirement (fixes #5)
Browse files Browse the repository at this point in the history
Not all Sourcegraph instances require authentication.
  • Loading branch information
emidoots committed Aug 2, 2018
1 parent cd11984 commit 2055f60
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ chmod +x /usr/local/bin/src
Note: Windows support is still rough around the edges, but is available. If you encounter issues, please let us know by filing an issue :)

Run in PowerShell as administrator:

```powershell
New-Item -ItemType Directory 'C:\Program Files\Sourcegraph'
Invoke-WebRequest https://github.com/sourcegraph/src-cli/releases/download/latest/src_windows_amd64.exe -OutFile 'C:\Program Files\Sourcegraph\src.exe'
Expand All @@ -43,6 +44,7 @@ $env:Path += ';C:\Program Files\Sourcegraph'
```

Or manually:

- [Download the latest src_windows_amd64.exe](https://github.com/sourcegraph/src-cli/releases/download/latest/src_windows_amd64.exe) and rename to `src.exe`.
- Place the file under e.g. `C:\Program Files\Sourcegraph\src.exe`
- Add that directory to your system path to access it from any command prompt
Expand All @@ -51,6 +53,22 @@ Or manually:

Consult `src -h` and `src api -h` for usage information.

## Authentication

Some Sourcegraph instances will be configured to require authentication. You can do so via the environment:

```sh
SRC_ACCESS_TOKEN="secret" src ...
```

Or via the configuration file (`~/src-config.json`):

```sh
{"accessToken": "secret"}
```

See `src -h` for more information.

## Development

If you want to develop the CLI, you can install it with `go get`:
Expand Down
13 changes: 11 additions & 2 deletions cmd/src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func curlCmd(endpoint, accessToken, query string, vars map[string]interface{}) (
}

s := fmt.Sprintf("curl \\\n")
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+accessToken))
if accessToken != "" {
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+accessToken))
}
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-d", string(data)))
s += fmt.Sprintf(" %s", shellquote.Join(gqlURL(endpoint)))
return s, nil
Expand Down Expand Up @@ -197,7 +199,9 @@ func (a *apiRequest) do() error {
if err != nil {
return err
}
req.Header.Set("Authorization", "token "+cfg.AccessToken)
if cfg.AccessToken != "" {
req.Header.Set("Authorization", "token "+cfg.AccessToken)
}
req.Body = ioutil.NopCloser(&buf)

// Perform the request.
Expand All @@ -211,6 +215,11 @@ func (a *apiRequest) do() error {
// confirm the status code. You can test this easily with e.g. an invalid
// endpoint like -endpoint=https://google.com
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusUnauthorized && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
fmt.Println("You may need to specify or update your access token to use this endpoint.")
fmt.Println("See https://github.com/sourcegraph/src-cli#authentication")
fmt.Println("")
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
Expand Down
15 changes: 0 additions & 15 deletions cmd/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -92,19 +91,5 @@ func readConfig() (*config, error) {
if cfg.Endpoint == "" {
cfg.Endpoint = "https://sourcegraph.com"
}

if cfg.AccessToken == "" {
return nil, fmt.Errorf(`error: you must specify an access token to use for %s
You can do so via the environment:
SRC_ACCESS_TOKEN="secret" src ...
or via the configuration file (%s):
{"accessToken": "secret"}
`, cfg.Endpoint, cfgPath)
}
return &cfg, nil
}

0 comments on commit 2055f60

Please sign in to comment.