Skip to content

Commit

Permalink
feat: handle legacy config with helpful advice
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Jul 15, 2024
1 parent cf39d4a commit 481b3ba
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/cloudx/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions cmd/cloudx/client/config_test.go
Original file line number Diff line number Diff line change
@@ -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!")
}

0 comments on commit 481b3ba

Please sign in to comment.