Skip to content

Commit 4f24894

Browse files
Merge pull request #94 from danielgtaylor/fix-20
fix: panic when multiple configured APIs have the same base URL
2 parents 725fd09 + 88ac792 commit 4f24894

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

cli/apiconfig.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"path"
@@ -112,16 +113,20 @@ func initAPIConfig() {
112113
panic(err)
113114
}
114115

116+
seen := map[string]bool{}
115117
for apiName, config := range configs {
116118
func(config *APIConfig) {
119+
if seen[config.Base] {
120+
panic(fmt.Errorf("Multiple APIs configured with the same base URL: %s", config.Base))
121+
}
122+
seen[config.Base] = true
117123
config.name = apiName
118124
configs[apiName] = config
119125

120126
n := apiName
121-
c := config
122127
cmd := &cobra.Command{
123128
Use: n,
124-
Short: c.Base,
129+
Short: config.Base,
125130
Run: func(cmd *cobra.Command, args []string) {
126131
cmd.Help()
127132
},

cli/cli_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"net/http"
55
"os"
6+
"path"
67
"strings"
78
"testing"
89
"time"
@@ -239,3 +240,33 @@ func TestAPISync(t *testing.T) {
239240

240241
runNoReset("api sync sync-test")
241242
}
243+
244+
func TestDuplicateAPIBase(t *testing.T) {
245+
defer func() {
246+
os.Remove(path.Join(userHomeDir(), ".test", "apis.json"))
247+
reset(false)
248+
}()
249+
reset(false)
250+
251+
configs["dupe1"] = &APIConfig{
252+
name: "dupe1",
253+
Base: "https://dupe.example.com",
254+
Profiles: map[string]*APIProfile{
255+
"default": {},
256+
},
257+
}
258+
configs["dupe2"] = &APIConfig{
259+
name: "dupe2",
260+
Base: "https://dupe.example.com",
261+
Profiles: map[string]*APIProfile{
262+
"default": {},
263+
},
264+
}
265+
266+
configs["dupe1"].Save()
267+
configs["dupe2"].Save()
268+
269+
assert.Panics(t, func() {
270+
run("--help")
271+
})
272+
}

0 commit comments

Comments
 (0)