Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/cmd/initcmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func run(command *cobra.Command, dependencies Dependencies, flags Setup, nonInte
if err != nil {
return exitcode.New(exitcode.Config, err)
}
configuredBackend := strings.TrimSpace(configValue.Keyring.Backend)
setup := Setup{
ClientID: configValue.ClientID, RedirectURI: configValue.RedirectURI,
CredentialRef: configValue.CredentialRef, Backend: configValue.Keyring.Backend,
Expand All @@ -89,11 +90,12 @@ func run(command *cobra.Command, dependencies Dependencies, flags Setup, nonInte
backendSet := backendFlag != nil && backendFlag.Changed
runtimeBackend := pointerValue(dependencies.Backend)
runtimeBackendSet := backendSet
interactive := dependencies.Interactive && !nonInteractive
if backendSet {
setup.Backend = runtimeBackend
}

if dependencies.Interactive && !nonInteractive {
if interactive {
prompt := dependencies.Prompt
if prompt == nil {
prompt = func(value *Setup) error { return RunPrompt(command.InOrStdin(), command.ErrOrStderr(), value) }
Expand Down Expand Up @@ -127,7 +129,7 @@ func run(command *cobra.Command, dependencies Dependencies, flags Setup, nonInte

result, err := dependencies.Initializer.Initialize(command.Context(), InitializeOptions{
Config: configValue, Profile: profile, Backend: runtimeBackend, BackendSet: runtimeBackendSet,
Overwrite: overwrite, Verify: !noVerify,
Overwrite: overwrite || (interactive && strings.TrimSpace(setup.Backend) != configuredBackend), Verify: !noVerify,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the documented overwrite contract: README currently directs users to use sptfy init --overwrite to replace an older credential, but interactive backend switches now replace an existing destination credential implicitly. Update that user-facing guidance to distinguish ordinary replacement from an intentional interactive backend change, so the durable behavior is discoverable.

Reply inline to this comment.

Authorization: auth.Request{
ClientID: configValue.ClientID, RedirectURI: configValue.RedirectURI,
NoBrowser: noBrowser || authCodeStdin, AuthCodeStdin: authCodeStdin,
Expand Down
37 changes: 37 additions & 0 deletions internal/cmd/initcmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,43 @@ func TestInteractiveInitUsesPromptedBackendForStoreAndConfig(t *testing.T) {
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File-level note: internal/cmd/initcmd/init_test.go

The new regression coverage does not prove the preserved interactive same-backend guard. TestInitRefusesExistingCredentialBeforeAuthorization exercises Interactive: false, while the changed overwrite condition branches on interactive; an accidental simplification to overwrite every interactive run would still pass both new tests. Add a case with Interactive: true, an existing credential, and a prompt that leaves the configured backend unchanged, asserting credstore.ErrExists (and ideally that authorization is not called).

Reply inline to this comment.

}

func TestInteractiveInitBackendChangeReplacesDestinationCredential(t *testing.T) {
harness := newInitHarness(t)
cfg := config.Default()
cfg.Keyring.Backend = "file"
if err := config.Save(harness.scope, cfg); err != nil {
t.Fatal(err)
}
key := "default/" + credentials.OAuthTokenKey
harness.store.values[key] = "stale-destination-credential"
harness.interactive = true
harness.prompt = func(setup *Setup) error {
setup.ClientID = "client-id"
setup.Backend = "keychain"
return nil
}
if err := harness.execute("--no-verify"); err != nil {
t.Fatal(err)
}
if harness.store.values[key] == "stale-destination-credential" || harness.store.setCalls != 1 {
t.Fatalf("credential = %q, set calls = %d", harness.store.values[key], harness.store.setCalls)
}
}

func TestNonInteractiveInitBackendChangeStillRequiresOverwrite(t *testing.T) {
harness := newInitHarness(t)
cfg := config.Default()
cfg.Keyring.Backend = "file"
if err := config.Save(harness.scope, cfg); err != nil {
t.Fatal(err)
}
harness.store.values["default/"+credentials.OAuthTokenKey] = "old-secret"
err := harness.execute("--backend", "keychain", "--non-interactive", "--client-id", "client-id", "--no-verify")
if !errors.Is(err, credstore.ErrExists) {
t.Fatalf("error = %v", err)
}
}

func TestNonInteractiveInitNamesMissingClientID(t *testing.T) {
harness := newInitHarness(t)
called := false
Expand Down
Loading