Skip to content

Commit

Permalink
fix: circleci orb list --uncertified --details no longer crashes (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre authored Feb 20, 2023
1 parent 0a0bcbd commit c6dc6bf
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
33 changes: 28 additions & 5 deletions cmd/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -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: '"

Expand All @@ -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 += ""
}
Expand Down
66 changes: 66 additions & 0 deletions cmd/orb_unit_test.go
Original file line number Diff line number Diff line change
@@ -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')",
),
)
})
})

0 comments on commit c6dc6bf

Please sign in to comment.