osbuild-pipeline: improve CLI interface

In the current state, osbuild-pipeline exits with random golang error,
such as goroutine failed, which is not at all helpful. This PR
introduces CLI arguments validation and helful error messages that use
the newly introduced types so that we don't waste time guessing what
was the right way to invoke this tool.
This commit is contained in:
Martin Sehnoutka 2020-01-30 10:24:28 +01:00 committed by Ondřej Budai
parent 8dac72f4fd
commit b0ddbc744a
2 changed files with 68 additions and 6 deletions

View file

@ -37,6 +37,23 @@ func marshalHelper(input int, mapping map[string]int, errorMessage string) ([]by
return nil, &CustomJsonConversionError{fmt.Sprintf("%d %s", input, errorMessage)}
}
func listHelper(mapping map[string]int) []string {
ret := make([]string, 0)
for k,_ := range mapping {
ret = append(ret, k)
}
return ret
}
func existsHelper(mapping map[string]int, testedValue string) bool {
for k, _ := range mapping {
if k == testedValue {
return true
}
}
return false
}
// Architecture represents one of the supported CPU architectures available for images
// produced by osbuild-composer. It is represented as an integer because if it
// was a string it would unmarshal from JSON just fine even in case that the architecture
@ -72,6 +89,14 @@ func getArchMapping() map[string]int {
return mapping
}
func ListArchitectures() []string {
return listHelper(getArchMapping())
}
func ArchitectureExists(testedArch string) bool {
return existsHelper(getArchMapping(), testedArch)
}
// UnmarshalJSON is a custom unmarshaling function to limit the set of allowed values
// in case the input is JSON.
func (arch Architecture) UnmarshalJSON(data []byte) error {
@ -153,6 +178,15 @@ func getDistributionMapping() map[string]int {
return mapping
}
func ListDistributions() []string {
return listHelper(getDistributionMapping())
}
func DistributionExists(testedDistro string) bool {
return existsHelper(getDistributionMapping(), testedDistro)
}
func (distro Distribution) UnmarshalJSON(data []byte) error {
value, err := unmarshalHelper(data, " is not a valid JSON value", " is not a valid distribution", getDistributionMapping())
if err != nil {