File tree Expand file tree Collapse file tree 2 files changed +38
-2
lines changed
Expand file tree Collapse file tree 2 files changed +38
-2
lines changed Original file line number Diff line number Diff line change 11package cli
22
33import (
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 },
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package cli
33import (
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+ }
You can’t perform that action at this time.
0 commit comments