Skip to content

Commit 2055f60

Browse files
author
Stephen Gutekanst
committed
relax the access token requirement (fixes #5)
Not all Sourcegraph instances require authentication.
1 parent cd11984 commit 2055f60

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ chmod +x /usr/local/bin/src
3535
Note: Windows support is still rough around the edges, but is available. If you encounter issues, please let us know by filing an issue :)
3636

3737
Run in PowerShell as administrator:
38+
3839
```powershell
3940
New-Item -ItemType Directory 'C:\Program Files\Sourcegraph'
4041
Invoke-WebRequest https://github.com/sourcegraph/src-cli/releases/download/latest/src_windows_amd64.exe -OutFile 'C:\Program Files\Sourcegraph\src.exe'
@@ -43,6 +44,7 @@ $env:Path += ';C:\Program Files\Sourcegraph'
4344
```
4445

4546
Or manually:
47+
4648
- [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`.
4749
- Place the file under e.g. `C:\Program Files\Sourcegraph\src.exe`
4850
- Add that directory to your system path to access it from any command prompt
@@ -51,6 +53,22 @@ Or manually:
5153

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

56+
## Authentication
57+
58+
Some Sourcegraph instances will be configured to require authentication. You can do so via the environment:
59+
60+
```sh
61+
SRC_ACCESS_TOKEN="secret" src ...
62+
```
63+
64+
Or via the configuration file (`~/src-config.json`):
65+
66+
```sh
67+
{"accessToken": "secret"}
68+
```
69+
70+
See `src -h` for more information.
71+
5472
## Development
5573

5674
If you want to develop the CLI, you can install it with `go get`:

cmd/src/api.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ func curlCmd(endpoint, accessToken, query string, vars map[string]interface{}) (
134134
}
135135

136136
s := fmt.Sprintf("curl \\\n")
137-
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+accessToken))
137+
if accessToken != "" {
138+
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-H", "Authorization: token "+accessToken))
139+
}
138140
s += fmt.Sprintf(" %s \\\n", shellquote.Join("-d", string(data)))
139141
s += fmt.Sprintf(" %s", shellquote.Join(gqlURL(endpoint)))
140142
return s, nil
@@ -197,7 +199,9 @@ func (a *apiRequest) do() error {
197199
if err != nil {
198200
return err
199201
}
200-
req.Header.Set("Authorization", "token "+cfg.AccessToken)
202+
if cfg.AccessToken != "" {
203+
req.Header.Set("Authorization", "token "+cfg.AccessToken)
204+
}
201205
req.Body = ioutil.NopCloser(&buf)
202206

203207
// Perform the request.
@@ -211,6 +215,11 @@ func (a *apiRequest) do() error {
211215
// confirm the status code. You can test this easily with e.g. an invalid
212216
// endpoint like -endpoint=https://google.com
213217
if resp.StatusCode != http.StatusOK {
218+
if resp.StatusCode == http.StatusUnauthorized && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
219+
fmt.Println("You may need to specify or update your access token to use this endpoint.")
220+
fmt.Println("See https://github.com/sourcegraph/src-cli#authentication")
221+
fmt.Println("")
222+
}
214223
body, err := ioutil.ReadAll(resp.Body)
215224
if err != nil {
216225
return err

cmd/src/main.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"flag"
6-
"fmt"
76
"io/ioutil"
87
"log"
98
"os"
@@ -92,19 +91,5 @@ func readConfig() (*config, error) {
9291
if cfg.Endpoint == "" {
9392
cfg.Endpoint = "https://sourcegraph.com"
9493
}
95-
96-
if cfg.AccessToken == "" {
97-
return nil, fmt.Errorf(`error: you must specify an access token to use for %s
98-
99-
You can do so via the environment:
100-
101-
SRC_ACCESS_TOKEN="secret" src ...
102-
103-
or via the configuration file (%s):
104-
105-
{"accessToken": "secret"}
106-
107-
`, cfg.Endpoint, cfgPath)
108-
}
10994
return &cfg, nil
11095
}

0 commit comments

Comments
 (0)