Skip to content

Commit 4a90dbf

Browse files
committed
Merge branch 'master' of github.com:sourcegraph/src-cli into validate_instance_cmd
2 parents b05f417 + 3d0de6f commit 4a90dbf

File tree

8 files changed

+267
-43
lines changed

8 files changed

+267
-43
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ language: go
22
services:
33
- docker
44
go:
5-
- 1.13.x
5+
- 1.14.x
66
go_import_path: github.com/sourcegraph/src-cli
77
install:
88
- go get -d -t ./...

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ All notable changes to `src-cli` are documented in this file.
1414
### Added
1515

1616
- Pull missing docker images automatically. [#191](https://github.com/sourcegraph/src-cli/pull/191)
17-
1817
- Searches that result in errors will now display any alerts returned by Sourcegraph, including suggestions for how the search could be corrected. [#221](https://github.com/sourcegraph/src-cli/pull/221)
1918

2019
### Changed
2120

2221
- The terminal UI has been replaced by the logger-based UI that was previously only visible in verbose-mode (`-v`). [#228](https://github.com/sourcegraph/src-cli/pull/228)
22+
- Deprecated the `-endpoint` flag. Instead, use the `SRC_ENDPOINT` environment variable. [#235](https://github.com/sourcegraph/src-cli/pull/235)
2323

2424
### Fixed
2525

2626
### Removed
27+

DEVELOPMENT.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,11 @@ For example, suppose we have the the recommended versions.
3232
If a new feature is added to a new `3.91.6` release of src-cli and this change requires only features available in Sourcegraph `3.99`, then this feature should also be present in a new `3.85.8` release of src-cli. Because a Sourcegraph instance will automatically select the highest patch version, all non-breaking changes should increment only the patch version.
3333

3434
Note that if instead the recommended src-cli version for Sourcegraph `3.99` was `3.90.4` in the example above, there is no additional step required, and the new patch version of src-cli will be available to both Sourcegraph versions.
35+
36+
## AppVeyor builds
37+
38+
We use AppVeyor to test `src-cli` on Windows.
39+
40+
Configure the AppVeyor builds by editing the `appveyor.yml` file and logging in to AppVeyor and changing the settings there.
41+
42+
Login with your GitHub account, switch to the `sourcegraph` account and change the settings here: https://ci.appveyor.com/project/sourcegraph/src-cli/settings/environment

appveyor.yml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
version: "{build}"
22
clone_folder: c:\gopath\src\github.com\sourcegraph\src-cli
3+
34
environment:
5+
GOROOT: 'c:\go'
46
GOPATH: c:\gopath
7+
GOVERSION: 1.14
8+
PATH: '%GOPATH%\bin;%GOROOT%\bin;%PATH%'
9+
GO111MODULE: 'on'
10+
511
install:
6-
- set PATH=%GOPATH%\bin;c:\go\bin;%PATH%
7-
- echo %PATH%
8-
- echo %GOPATH%
12+
# Install the specific Go version.
13+
- rmdir c:\go /s /q
14+
- appveyor DownloadFile https://storage.googleapis.com/golang/go%GOVERSION%.windows-amd64.msi
15+
- msiexec /i go%GOVERSION%.windows-amd64.msi /q
916
- go version
1017
- go env
11-
- go get -t -v ./...
18+
1219
build_script:
13-
- go test -v ./...
20+
- go version
21+
- go get -v -t ./...
22+
23+
test_script:
24+
- go test ./...

cmd/src/actions_exec.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -476,28 +476,7 @@ fragment repositoryFields on Repository {
476476
for _, repo := range reposByID {
477477
repos = append(repos, repo)
478478
}
479-
for _, r := range skipped {
480-
logger.Infof("Skipping repository %s because we couldn't determine default branch.\n", r)
481-
}
482-
for _, r := range unsupported {
483-
logger.Infof("# Skipping repository %s because it's on a not supported code host.\n", r)
484-
}
485-
matchesStr := fmt.Sprintf("%d repositories match.", len(repos))
486-
unsupportedCount := len(unsupported)
487-
if includeUnsupported {
488-
if unsupportedCount > 0 {
489-
matchesStr += fmt.Sprintf(" (Including %d on unsupported code hosts.)", unsupportedCount)
490-
}
491-
} else {
492-
if unsupportedCount > 0 {
493-
matchesStr += " (Some repositories were filtered out because their code host is not supported by campaigns. Use -include-unsupported to generate patches for them anyways.)"
494-
}
495-
}
496-
logger.Infof("%s\n", matchesStr)
497-
498-
if len(repos) == 0 && !*verbose {
499-
yellow.Fprintf(os.Stderr, "WARNING: No repositories matched by scopeQuery\n")
500-
}
479+
logger.RepoMatches(len(repos), skipped, unsupported)
501480

502481
if content, err := result.Data.Search.Results.Alert.Render(); err != nil {
503482
yellow.Fprint(os.Stderr, err)

cmd/src/main.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"os/user"
1010
"path/filepath"
1111
"strings"
12+
13+
"github.com/pkg/errors"
1214
)
1315

1416
const usageText = `src is a tool that provides access to Sourcegraph instances.
@@ -24,7 +26,6 @@ Environment variables
2426
2527
The options are:
2628
27-
-endpoint= specifies the endpoint to use e.g. "https://sourcegraph.com" (overrides SRC_ENDPOINT if set)
2829
-v print verbose output
2930
3031
The commands are:
@@ -47,9 +48,11 @@ Use "src [command] -h" for more information about a command.
4748
`
4849

4950
var (
51+
verbose = flag.Bool("v", false, "print verbose output")
52+
53+
// The following arguments are deprecated which is why they are no longer documented
5054
configPath = flag.String("config", "", "")
5155
endpoint = flag.String("endpoint", "", "")
52-
verbose = flag.Bool("v", false, "print verbose output")
5356
)
5457

5558
// commands contains all registered subcommands.
@@ -76,14 +79,14 @@ func readConfig() (*config, error) {
7679
cfgPath := *configPath
7780
userSpecified := *configPath != ""
7881

79-
user, err := user.Current()
82+
u, err := user.Current()
8083
if err != nil {
8184
return nil, err
8285
}
8386
if !userSpecified {
84-
cfgPath = filepath.Join(user.HomeDir, "src-config.json")
87+
cfgPath = filepath.Join(u.HomeDir, "src-config.json")
8588
} else if strings.HasPrefix(cfgPath, "~/") {
86-
cfgPath = filepath.Join(user.HomeDir, cfgPath[2:])
89+
cfgPath = filepath.Join(u.HomeDir, cfgPath[2:])
8790
}
8891
data, err := ioutil.ReadFile(os.ExpandEnv(cfgPath))
8992
if err != nil && (!os.IsNotExist(err) || userSpecified) {
@@ -96,23 +99,39 @@ func readConfig() (*config, error) {
9699
}
97100
}
98101

102+
envToken := os.Getenv("SRC_ACCESS_TOKEN")
103+
envEndpoint := os.Getenv("SRC_ENDPOINT")
104+
105+
if userSpecified {
106+
// If a config file is present, either zero or both environment variables must be present.
107+
// We don't want to partially apply environment variables.
108+
if envToken == "" && envEndpoint != "" {
109+
return nil, errConfigMerge
110+
}
111+
if envToken != "" && envEndpoint == "" {
112+
return nil, errConfigMerge
113+
}
114+
}
115+
99116
// Apply config overrides.
100-
if envToken := os.Getenv("SRC_ACCESS_TOKEN"); envToken != "" {
117+
if envToken != "" {
101118
cfg.AccessToken = envToken
102119
}
103-
if *endpoint != "" {
104-
cfg.Endpoint = *endpoint
105-
}
106-
if cfg.Endpoint == "" {
107-
if endpoint := os.Getenv("SRC_ENDPOINT"); endpoint != "" {
108-
cfg.Endpoint = endpoint
109-
}
120+
if envEndpoint != "" {
121+
cfg.Endpoint = envEndpoint
110122
}
111123
if cfg.Endpoint == "" {
112124
cfg.Endpoint = "https://sourcegraph.com"
113125
}
114126

127+
// Lastly, apply endpoint flag if set
128+
if endpoint != nil && *endpoint != "" {
129+
cfg.Endpoint = *endpoint
130+
}
131+
115132
cfg.Endpoint = strings.TrimSuffix(cfg.Endpoint, "/")
116133

117134
return &cfg, nil
118135
}
136+
137+
var errConfigMerge = errors.New("when using a configuration file, zero or all environment variables must be set")

cmd/src/main_test.go

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
)
12+
13+
func TestReadConfig(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
fileContents *config
17+
envToken string
18+
envEndpoint string
19+
flagEndpoint string
20+
want *config
21+
wantErr string
22+
}{
23+
{
24+
name: "defaults",
25+
want: &config{
26+
Endpoint: "https://sourcegraph.com",
27+
},
28+
},
29+
{
30+
name: "config file, no overrides, trim slash",
31+
fileContents: &config{
32+
Endpoint: "https://example.com/",
33+
AccessToken: "deadbeef",
34+
},
35+
want: &config{
36+
Endpoint: "https://example.com",
37+
AccessToken: "deadbeef",
38+
},
39+
},
40+
{
41+
name: "config file, token override only",
42+
fileContents: &config{
43+
Endpoint: "https://example.com/",
44+
AccessToken: "deadbeef",
45+
},
46+
envToken: "abc",
47+
want: nil,
48+
wantErr: errConfigMerge.Error(),
49+
},
50+
{
51+
name: "config file, endpoint override only",
52+
fileContents: &config{
53+
Endpoint: "https://example.com/",
54+
AccessToken: "deadbeef",
55+
},
56+
envEndpoint: "https://exmaple2.com",
57+
want: nil,
58+
wantErr: errConfigMerge.Error(),
59+
},
60+
{
61+
name: "config file, both override",
62+
fileContents: &config{
63+
Endpoint: "https://example.com/",
64+
AccessToken: "deadbeef",
65+
},
66+
envToken: "abc",
67+
envEndpoint: "https://override.com",
68+
want: &config{
69+
Endpoint: "https://override.com",
70+
AccessToken: "abc",
71+
},
72+
},
73+
{
74+
name: "no config file, token from environment",
75+
envToken: "abc",
76+
want: &config{
77+
Endpoint: "https://sourcegraph.com",
78+
AccessToken: "abc",
79+
},
80+
},
81+
{
82+
name: "no config file, endpoint from environment",
83+
envEndpoint: "https://example.com",
84+
want: &config{
85+
Endpoint: "https://example.com",
86+
AccessToken: "",
87+
},
88+
},
89+
{
90+
name: "no config file, both variables",
91+
envEndpoint: "https://example.com",
92+
envToken: "abc",
93+
want: &config{
94+
Endpoint: "https://example.com",
95+
AccessToken: "abc",
96+
},
97+
},
98+
{
99+
name: "endpoint flag should override config",
100+
flagEndpoint: "https://override.com/",
101+
fileContents: &config{
102+
Endpoint: "https://example.com/",
103+
AccessToken: "deadbeef",
104+
},
105+
want: &config{
106+
Endpoint: "https://override.com",
107+
AccessToken: "deadbeef",
108+
},
109+
},
110+
{
111+
name: "endpoint flag should override environment",
112+
flagEndpoint: "https://override.com/",
113+
envEndpoint: "https://example.com",
114+
envToken: "abc",
115+
want: &config{
116+
Endpoint: "https://override.com",
117+
AccessToken: "abc",
118+
},
119+
},
120+
}
121+
122+
for _, test := range tests {
123+
t.Run(test.name, func(t *testing.T) {
124+
setEnv := func(name, val string) {
125+
old := os.Getenv(name)
126+
if err := os.Setenv(name, val); err != nil {
127+
t.Fatal(err)
128+
}
129+
t.Cleanup(func() { os.Setenv(name, old) })
130+
}
131+
setEnv("SRC_ACCESS_TOKEN", test.envToken)
132+
setEnv("SRC_ENDPOINT", test.envEndpoint)
133+
134+
if test.flagEndpoint != "" {
135+
val := test.flagEndpoint
136+
endpoint = &val
137+
t.Cleanup(func() { endpoint = nil })
138+
}
139+
140+
if test.fileContents != nil {
141+
oldConfigPath := *configPath
142+
t.Cleanup(func() { *configPath = oldConfigPath })
143+
144+
data, err := json.Marshal(*test.fileContents)
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
tmpDir, err := ioutil.TempDir("", "")
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
t.Cleanup(func() { os.RemoveAll(tmpDir) })
153+
filePath := filepath.Join(tmpDir, "config.json")
154+
err = ioutil.WriteFile(filePath, data, 0600)
155+
if err != nil {
156+
t.Fatal(err)
157+
}
158+
*configPath = filePath
159+
}
160+
161+
config, err := readConfig()
162+
if diff := cmp.Diff(test.want, config); diff != "" {
163+
t.Errorf("config: %v", diff)
164+
}
165+
var errMsg string
166+
if err != nil {
167+
errMsg = err.Error()
168+
}
169+
if diff := cmp.Diff(test.wantErr, errMsg); diff != "" {
170+
t.Errorf("err: %v", diff)
171+
}
172+
})
173+
}
174+
}

0 commit comments

Comments
 (0)