0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2024-12-16 21:56:37 -05:00

Raise error when adding a new zot config with an existed saved name

This commit is contained in:
Ionut Costin Craciun 2020-10-29 14:47:27 +02:00
parent 429a689ace
commit dad884ddeb
3 changed files with 30 additions and 0 deletions

View file

@ -36,4 +36,5 @@ var (
ErrIllegalConfigKey = errors.New("cli: given config key is not allowed")
ErrScanNotSupported = errors.New("search: scanning of image media type not supported")
ErrCLITimeout = errors.New("cli: Query timed out while waiting for results")
ErrDuplicateConfigName = errors.New("cli: cli config name already added")
)

View file

@ -204,6 +204,10 @@ func addConfig(configPath, configName, url string) error {
return zotErrors.ErrInvalidURL
}
if configNameExists(configs, configName) {
return zotErrors.ErrDuplicateConfigName
}
configMap := make(map[string]interface{})
configMap["url"] = url
configMap[nameKey] = configName
@ -361,6 +365,17 @@ func getAllConfig(configPath, configName string) (string, error) {
return "", zotErrors.ErrConfigNotFound
}
func configNameExists(configs []interface{}, configName string) bool {
for _, val := range configs {
configMap := val.(map[string]interface{})
if configMap[nameKey] == configName {
return true
}
}
return false
}
const (
examples = ` zot config add main https://zot-foo.com:8080
zot config main url

View file

@ -300,4 +300,18 @@ func TestConfigCmdMain(t *testing.T) {
So(err, ShouldNotBeNil)
So(buff.String(), ShouldContainSubstring, "cannot reset")
})
Convey("Test add a config with an existing saved name", t, func() {
args := []string{"add", "configtest", "https://test-url.com/new"}
configPath := makeConfigFile(`{"configs":[{"_name":"configtest","url":"https://test-url.com","showspinner":false}]}`)
defer os.Remove(configPath)
cmd := NewConfigCommand()
buff := bytes.NewBufferString("")
cmd.SetOut(buff)
cmd.SetErr(ioutil.Discard)
cmd.SetArgs(args)
err := cmd.Execute()
So(err, ShouldNotBeNil)
So(buff.String(), ShouldContainSubstring, "cli config name already added")
})
}