diff --git a/cmd/orb.go b/cmd/orb.go index 096829be3..bb7fc910a 100644 --- a/cmd/orb.go +++ b/cmd/orb.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "os" "path" @@ -390,6 +391,22 @@ func orbHelpLong(config *settings.Config) string { See a full explanation and documentation on orbs here: %s`, config.Data.Links.OrbDocs) } +// Transform a boolean parameter into a string. Because the value can be a boolean but can also be +// a string, we need to first parse it as a boolean and then if it is not a boolean, parse it as +// a string +// +// Documentation reference: https://circleci.com/docs/reusing-config/#boolean +func booleanParameterDefaultToString(parameter api.OrbElementParameter) string { + if v, ok := parameter.Default.(bool); ok { + return fmt.Sprintf("%t", v) + } + v, ok := parameter.Default.(string) + if !ok { + log.Panicf("Unable to parse boolean parameter with value %+v", v) + } + return v +} + func parameterDefaultToString(parameter api.OrbElementParameter) string { defaultValue := " (default: '" @@ -401,12 +418,18 @@ func parameterDefaultToString(parameter api.OrbElementParameter) string { } switch parameter.Type { - case "enum": - defaultValue += parameter.Default.(string) - case "string": - defaultValue += parameter.Default.(string) + case "enum", "string": + if v, ok := parameter.Default.(string); ok { + defaultValue += v + break + } + if v, ok := parameter.Default.(fmt.Stringer); ok { + defaultValue += v.String() + break + } + log.Panicf("Unable to parse parameter default with value %+v because it's neither a string nor a stringer", parameter.Default) case "boolean": - defaultValue += fmt.Sprintf("%t", parameter.Default.(bool)) + defaultValue += booleanParameterDefaultToString(parameter) default: defaultValue += "" } diff --git a/cmd/orb_unit_test.go b/cmd/orb_unit_test.go new file mode 100644 index 000000000..378d5650d --- /dev/null +++ b/cmd/orb_unit_test.go @@ -0,0 +1,66 @@ +package cmd + +import ( + "time" + + "github.com/CircleCI-Public/circleci-cli/api" + . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/extensions/table" + . "github.com/onsi/gomega" +) + +var _ = Describe("Orb unit tests", func() { + Describe("Orb formatters", func() { + DescribeTable( + "parameterDefaultToString", + func(input api.OrbElementParameter, expected string) { + Expect(parameterDefaultToString(input)).To(Equal(expected)) + }, + Entry( + "Normal behaviour for string", + api.OrbElementParameter{ + Type: "string", + Description: "", + Default: "Normal behavior", + }, + " (default: 'Normal behavior')", + ), + Entry( + "Normal behaviour for enum", + api.OrbElementParameter{ + Type: "enum", + Description: "", + Default: "Normal behavior", + }, + " (default: 'Normal behavior')", + ), + Entry( + "Normal behaviour for boolean", + api.OrbElementParameter{ + Type: "boolean", + Description: "", + Default: true, + }, + " (default: 'true')", + ), + Entry( + "String value for boolean", + api.OrbElementParameter{ + Type: "boolean", + Description: "", + Default: "yes", + }, + " (default: 'yes')", + ), + Entry( + "Time value for string", + api.OrbElementParameter{ + Type: "string", + Description: "", + Default: time.Date(2023, 02, 20, 11, 9, 0, 0, time.Now().UTC().Location()), + }, + " (default: '2023-02-20 11:09:00 +0000 UTC')", + ), + ) + }) +})