Skip to content

Commit 8229289

Browse files
authored
feat: handle legacy config with helpful advice (#365)
1 parent 8c6b7f3 commit 8229289

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

cmd/cloudx/client/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"errors"
1010
"fmt"
11+
"io"
1112
"io/fs"
1213
"os"
1314
"path/filepath"
@@ -88,6 +89,24 @@ func (h *CommandHelper) getConfig() (*Config, error) {
8889
if err != nil {
8990
return nil, err
9091
}
92+
switch c.Version {
93+
case "v0alpha0":
94+
if h.isQuiet {
95+
return nil, fmt.Errorf("you have to authenticate the Ory CLI now differently, plese see ory auth for details")
96+
}
97+
98+
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Thanks for upgrading! You will now be prompted to log in to the Ory CLI through the Ory Console.")
99+
_, _ = fmt.Fprintln(h.VerboseErrWriter, "Press enter to continue...")
100+
_, err := h.Stdin.ReadString('\n')
101+
if err != nil && err != io.EOF {
102+
return nil, fmt.Errorf("unable to read from stdin: %w", err)
103+
}
104+
fallthrough
105+
default:
106+
return nil, ErrNoConfig
107+
case ConfigVersion:
108+
// pass
109+
}
91110
h.config = c
92111
}
93112
return h.config, nil

cmd/cloudx/client/config_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright © 2024 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package client_test
5+
6+
import (
7+
"bytes"
8+
"context"
9+
"os"
10+
"strings"
11+
"testing"
12+
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
16+
"github.com/ory/cli/cmd/cloudx/client"
17+
"github.com/ory/cli/cmd/cloudx/testhelpers"
18+
)
19+
20+
func TestLegacyConfigHandling(t *testing.T) {
21+
ctx := context.Background()
22+
legacyConfigFile := testhelpers.NewConfigFile(t)
23+
require.NoError(t, os.WriteFile(legacyConfigFile, []byte(`{"version": "v0alpha0"}`), 0600))
24+
25+
out := bytes.Buffer{}
26+
h, err := client.NewCommandHelper(
27+
ctx,
28+
client.WithConfigLocation(legacyConfigFile),
29+
client.WithOpenBrowserHook(func(string) error {
30+
return testhelpers.ErrAuthFlowTriggered
31+
}),
32+
client.WithVerboseErrWriter(&out),
33+
client.WithStdin(strings.NewReader("\n")),
34+
)
35+
require.NoError(t, err)
36+
37+
_, err = h.GetAuthenticatedConfig(ctx)
38+
assert.ErrorIs(t, err, testhelpers.ErrAuthFlowTriggered)
39+
assert.Contains(t, out.String(), "Thanks for upgrading!")
40+
}

0 commit comments

Comments
 (0)