mirror of
https://github.com/project-zot/zot.git
synced 2025-02-17 23:45:36 -05:00
Merge pull request #148 from IonutCraciun/fixConfigNameCli
Raise error when adding a new zot config with an existed saved name
This commit is contained in:
commit
b80b7d2361
3 changed files with 30 additions and 0 deletions
|
@ -36,4 +36,5 @@ var (
|
||||||
ErrIllegalConfigKey = errors.New("cli: given config key is not allowed")
|
ErrIllegalConfigKey = errors.New("cli: given config key is not allowed")
|
||||||
ErrScanNotSupported = errors.New("search: scanning of image media type not supported")
|
ErrScanNotSupported = errors.New("search: scanning of image media type not supported")
|
||||||
ErrCLITimeout = errors.New("cli: Query timed out while waiting for results")
|
ErrCLITimeout = errors.New("cli: Query timed out while waiting for results")
|
||||||
|
ErrDuplicateConfigName = errors.New("cli: cli config name already added")
|
||||||
)
|
)
|
||||||
|
|
|
@ -204,6 +204,10 @@ func addConfig(configPath, configName, url string) error {
|
||||||
return zotErrors.ErrInvalidURL
|
return zotErrors.ErrInvalidURL
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if configNameExists(configs, configName) {
|
||||||
|
return zotErrors.ErrDuplicateConfigName
|
||||||
|
}
|
||||||
|
|
||||||
configMap := make(map[string]interface{})
|
configMap := make(map[string]interface{})
|
||||||
configMap["url"] = url
|
configMap["url"] = url
|
||||||
configMap[nameKey] = configName
|
configMap[nameKey] = configName
|
||||||
|
@ -361,6 +365,17 @@ func getAllConfig(configPath, configName string) (string, error) {
|
||||||
return "", zotErrors.ErrConfigNotFound
|
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 (
|
const (
|
||||||
examples = ` zot config add main https://zot-foo.com:8080
|
examples = ` zot config add main https://zot-foo.com:8080
|
||||||
zot config main url
|
zot config main url
|
||||||
|
|
|
@ -300,4 +300,18 @@ func TestConfigCmdMain(t *testing.T) {
|
||||||
So(err, ShouldNotBeNil)
|
So(err, ShouldNotBeNil)
|
||||||
So(buff.String(), ShouldContainSubstring, "cannot reset")
|
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")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue