Skip to content

Commit

Permalink
feat: handle legacy config with helpful advice (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik authored Jul 16, 2024
1 parent 8c6b7f3 commit 8229289
Show file tree
Hide file tree
Showing 2 changed files with 59 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 @@ -88,6 +89,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
40 changes: 40 additions & 0 deletions cmd/cloudx/client/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package client_test

import (
"bytes"
"context"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/cli/cmd/cloudx/client"
"github.com/ory/cli/cmd/cloudx/testhelpers"
)

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 8229289

Please sign in to comment.