diff --git a/cmd/config/root.go b/cmd/config/root.go new file mode 100644 index 000000000000..502fb9b701bc --- /dev/null +++ b/cmd/config/root.go @@ -0,0 +1,25 @@ +// Copyright © 2025 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + +package config + +import ( + "github.com/spf13/cobra" + + "github.com/ory/kratos/driver" + "github.com/ory/x/configx" +) + +func NewConfigCmd() *cobra.Command { + c := &cobra.Command{ + Use: "config", + } + configx.RegisterFlags(c.PersistentFlags()) + return c +} + +func RegisterCommandRecursive(parent *cobra.Command, dOpts []driver.RegistryOption) { + c := NewConfigCmd() + parent.AddCommand(c) + c.AddCommand(NewConfigViewCmd(dOpts)) +} diff --git a/cmd/config/view.go b/cmd/config/view.go new file mode 100644 index 000000000000..51c82367a14c --- /dev/null +++ b/cmd/config/view.go @@ -0,0 +1,48 @@ +// Copyright © 2025 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + +package config + +import ( + "fmt" + + "github.com/spf13/cobra" + + "github.com/ory/kratos/driver" + "github.com/ory/kratos/driver/config" + "github.com/ory/x/configx" + "github.com/ory/x/contextx" + "github.com/ory/x/logrusx" +) + +func NewConfigViewCmd(dOpts []driver.RegistryOption) *cobra.Command { + c := &cobra.Command{ + Use: "view", + RunE: func(cmd *cobra.Command, args []string) error { + l := logrusx.New("Ory Kratos", config.Version) + config, err := config.New( + cmd.Context(), + l, + cmd.ErrOrStderr(), + &contextx.Default{}, + configx.WithFlags(cmd.Flags()), + configx.SkipValidation(), + configx.WithContext(cmd.Context()), + ) + if err != nil { + return err + } + + out, err := config.View(cmd.Context()) + if err != nil { + return err + } + + fmt.Printf("%s\n", out) + + return nil + }, + } + + return c +} diff --git a/cmd/root.go b/cmd/root.go index e3e1127b8d3d..66b56cd293fa 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,6 +11,7 @@ import ( "github.com/spf13/cobra" "github.com/ory/kratos/cmd/cleanup" + configCmd "github.com/ory/kratos/cmd/config" "github.com/ory/kratos/cmd/courier" "github.com/ory/kratos/cmd/hashers" "github.com/ory/kratos/cmd/identities" @@ -31,6 +32,7 @@ func NewRootCmd(driverOpts ...driver.RegistryOption) (cmd *cobra.Command) { } cmdx.EnableUsageTemplating(cmd) + configCmd.RegisterCommandRecursive(cmd, driverOpts) courier.RegisterCommandRecursive(cmd, driverOpts) cmd.AddCommand(identities.NewGetCmd()) cmd.AddCommand(identities.NewDeleteCmd()) diff --git a/driver/config/config.go b/driver/config/config.go index 06fe6b4410e9..3595cea78749 100644 --- a/driver/config/config.go +++ b/driver/config/config.go @@ -492,6 +492,10 @@ func (p *Config) validateIdentitySchemas(ctx context.Context) error { return nil } +func (p *Config) View(ctx context.Context) ([]byte, error) { + return p.GetProvider(ctx).View() +} + func (p *Config) formatJsonErrors(schema []byte, err error) { _, _ = fmt.Fprintln(p.stdOutOrErr, "") jsonschemax.FormatValidationErrorForCLI(p.stdOutOrErr, schema, err) diff --git a/oryx/configx/provider.go b/oryx/configx/provider.go index 278ecd0fbc08..4f0aca01976d 100644 --- a/oryx/configx/provider.go +++ b/oryx/configx/provider.go @@ -16,6 +16,7 @@ import ( "github.com/inhies/go-bytesize" "github.com/knadh/koanf/parsers/json" + "github.com/knadh/koanf/parsers/yaml" "github.com/knadh/koanf/providers/posflag" "github.com/knadh/koanf/v2" "github.com/pkg/errors" @@ -534,6 +535,15 @@ func (p *Provider) PrintHumanReadableValidationErrors(w io.Writer, err error) { p.printHumanReadableValidationErrors(p.Koanf, w, err) } +func (p *Provider) View() ([]byte, error) { + out, err := p.Koanf.Marshal(yaml.Parser()) + if err != nil { + return nil, fmt.Errorf("Unable to marshal configuration: %w", err) + } + + return out, nil +} + func (p *Provider) printHumanReadableValidationErrors(k *koanf.Koanf, w io.Writer, err error) { if err == nil { return