From 481b3ba8c7b31942c4efa5d7e05d05a9a2e5cff3 Mon Sep 17 00:00:00 2001 From: zepatrik Date: Mon, 15 Jul 2024 19:25:25 +0200 Subject: [PATCH] feat: handle legacy config with helpful advice --- cmd/cloudx/client/config.go | 19 +++++++++++++++++ cmd/cloudx/client/config_test.go | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 cmd/cloudx/client/config_test.go diff --git a/cmd/cloudx/client/config.go b/cmd/cloudx/client/config.go index 7512b5fe..f5aeaa23 100644 --- a/cmd/cloudx/client/config.go +++ b/cmd/cloudx/client/config.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/fs" "os" "path/filepath" @@ -83,6 +84,24 @@ func (h *CommandHelper) getConfig() (*Config, error) { if err != nil { return nil, err } + switch c.Version { + case "v0alpha0": + if h.isQuiet { + return nil, fmt.Errorf("you have to authenticate the Ory CLI now differently, plese see ory auth for details") + } + + _, _ = fmt.Fprintln(h.VerboseErrWriter, "Thanks for upgrading! You will now be prompted to log in to the Ory CLI through the Ory Console.") + _, _ = fmt.Fprintln(h.VerboseErrWriter, "Press enter to continue...") + _, err := h.Stdin.ReadString('\n') + if err != nil && err != io.EOF { + return nil, fmt.Errorf("unable to read from stdin: %w", err) + } + fallthrough + default: + return nil, ErrNoConfig + case ConfigVersion: + // pass + } h.config = c } return h.config, nil diff --git a/cmd/cloudx/client/config_test.go b/cmd/cloudx/client/config_test.go new file mode 100644 index 00000000..babd5007 --- /dev/null +++ b/cmd/cloudx/client/config_test.go @@ -0,0 +1,35 @@ +package client_test + +import ( + "bytes" + "context" + "github.com/ory/cli/cmd/cloudx/client" + "github.com/ory/cli/cmd/cloudx/testhelpers" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "os" + "strings" + "testing" +) + +func TestLegacyConfigHandling(t *testing.T) { + ctx := context.Background() + legacyConfigFile := testhelpers.NewConfigFile(t) + require.NoError(t, os.WriteFile(legacyConfigFile, []byte(`{"version": "v0alpha0"}`), 0600)) + + out := bytes.Buffer{} + h, err := client.NewCommandHelper( + ctx, + client.WithConfigLocation(legacyConfigFile), + client.WithOpenBrowserHook(func(string) error { + return testhelpers.ErrAuthFlowTriggered + }), + client.WithVerboseErrWriter(&out), + client.WithStdin(strings.NewReader("\n")), + ) + require.NoError(t, err) + + _, err = h.GetAuthenticatedConfig(ctx) + assert.ErrorIs(t, err, testhelpers.ErrAuthFlowTriggered) + assert.Contains(t, out.String(), "Thanks for upgrading!") +}