1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

docs: show possible values in step documentation (#1453)

* add possibleValues to model

* include possible values into docs
This commit is contained in:
Christopher Fenner 2020-04-24 14:13:02 +02:00 committed by GitHub
parent 2100632b54
commit 96439972e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 1 deletions

View File

@ -52,6 +52,7 @@ type StepParameters struct {
Type string `json:"type"`
Mandatory bool `json:"mandatory,omitempty"`
Default interface{} `json:"default,omitempty"`
PossibleValues []interface{} `json:"possibleValues,omitempty"`
Aliases []Alias `json:"aliases,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
Secret bool `json:"secret,omitempty"`

View File

@ -147,7 +147,7 @@ func createParametersTable(parameters []config.StepParameters) string {
for _, param := range parameters {
if v, ok := m[param.Name]; ok {
table += fmt.Sprintf(" | %v | %v | %v | |\n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), ifThenElse(v == "<nil>", "", v))
table += fmt.Sprintf(" | %v | %v | %v | %v |\n ", param.Name, ifThenElse(param.Mandatory && param.Default == nil, "Yes", "No"), ifThenElse(v == "<nil>", "", v), possibleValuesToString(param.PossibleValues))
delete(m, param.Name)
}
}
@ -468,3 +468,17 @@ func sortStepParameters(stepData *config.StepData) {
})
}
}
func possibleValuesToString(in []interface{}) (out string) {
if len(in) == 0 {
return
}
out = fmt.Sprintf("`%v`", in[0])
if len(in) == 1 {
return
}
for _, value := range in[1:] {
out += fmt.Sprintf(", `%v`", value)
}
return
}

View File

@ -400,3 +400,41 @@ func TestAddExistingParameterWithCondition(t *testing.T) {
}
})
}
func TestRenderPossibleValues(t *testing.T) {
t.Run("none", func(t *testing.T) {
// init
var in []interface{}
// test
out := possibleValuesToString(in)
// assert
assert.Empty(t, out)
})
t.Run("one", func(t *testing.T) {
// init
var in []interface{}
in = append(in, "fu")
// test
out := possibleValuesToString(in)
// assert
assert.Equal(t, "`fu`", out)
})
t.Run("many", func(t *testing.T) {
// init
var in []interface{}
in = append(in, "fu", "fara")
// test
out := possibleValuesToString(in)
// assert
assert.Equal(t, "`fu`, `fara`", out)
})
t.Run("boolean", func(t *testing.T) {
// init
var in []interface{}
in = append(in, false, true)
// test
out := possibleValuesToString(in)
// assert
assert.Equal(t, "`false`, `true`", out)
})
}