diff --git a/pkg/idp/provider/github/github.go b/pkg/idp/provider/github/github.go index 1f81bc8..e7b90dc 100644 --- a/pkg/idp/provider/github/github.go +++ b/pkg/idp/provider/github/github.go @@ -2,6 +2,7 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" "strconv" @@ -11,6 +12,7 @@ import ( "github.com/giantswarm/dex-operator/pkg/idp/provider" "github.com/giantswarm/dex-operator/pkg/idp/provider/github/manifest" "github.com/giantswarm/dex-operator/pkg/key" + "github.com/giantswarm/dex-operator/pkg/yaml" "github.com/bradleyfalzon/ghinstallation/v2" githubconnector "github.com/dexidp/dex/connector/github" @@ -18,7 +20,6 @@ import ( "github.com/go-logr/logr" githubclient "github.com/google/go-github/v50/github" "github.com/skratchdot/open-golang/open" - "gopkg.in/yaml.v2" ) const ( @@ -178,7 +179,7 @@ func (g *Github) CreateOrUpdateApp(config provider.AppConfig, ctx context.Contex RedirectURI: config.RedirectURI, TeamNameField: TeamNameFieldSlug, } - data, err := yaml.Marshal(connectorConfig) + data, err := yaml.MarshalWithJsonAnnotations(connectorConfig) if err != nil { return provider.ProviderApp{}, microerror.Mask(err) } diff --git a/pkg/yaml/yaml.go b/pkg/yaml/yaml.go new file mode 100644 index 0000000..ef03391 --- /dev/null +++ b/pkg/yaml/yaml.go @@ -0,0 +1,24 @@ +package yaml + +import ( + "encoding/json" + + "github.com/giantswarm/microerror" + yamllib "gopkg.in/yaml.v2" +) + +// MarshalWithJsonAnnotations marshals the given value to YAML, but uses the +// JSON annotations of the struct fields. +func MarshalWithJsonAnnotations(v any) ([]byte, error) { + jsonData, jsonErr := json.Marshal(v) + if jsonErr != nil { + return nil, microerror.Mask(jsonErr) + } + var mapData map[string]interface{} + jsonErr = json.Unmarshal(jsonData, &mapData) + if jsonErr != nil { + return nil, microerror.Mask(jsonErr) + } + data, yamlErr := yamllib.Marshal(mapData) + return data, yamlErr +}