From 2055f604ba38e72f470f0429b1d43feacc0ffc3b Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Thu, 2 Aug 2018 00:12:52 -0700 Subject: [PATCH] relax the access token requirement (fixes #5) Not all Sourcegraph instances require authentication. --- README.md | 18 ++++++++++++++++++ cmd/src/api.go | 13 +++++++++++-- cmd/src/main.go | 15 --------------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 53c0316605..e97c67e521 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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 @@ -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`: diff --git a/cmd/src/api.go b/cmd/src/api.go index d501c58631..2ed2f50ed8 100644 --- a/cmd/src/api.go +++ b/cmd/src/api.go @@ -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 @@ -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. @@ -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 diff --git a/cmd/src/main.go b/cmd/src/main.go index 05b07f715c..e3deb65c6b 100644 --- a/cmd/src/main.go +++ b/cmd/src/main.go @@ -3,7 +3,6 @@ package main import ( "encoding/json" "flag" - "fmt" "io/ioutil" "log" "os" @@ -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 }