From 87c8c3d6e0d96b173b52fee1d8b4aad25bd2dc52 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 16 Feb 2026 03:37:44 +0000 Subject: [PATCH] Fix new tty prompt (#897) Fix #827 --------- Co-authored-by: silverwind Reviewed-on: https://gitea.com/gitea/tea/pulls/897 Reviewed-by: silverwind --- modules/context/context.go | 10 ++++- modules/context/context_prompt_test.go | 52 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 modules/context/context_prompt_test.go diff --git a/modules/context/context.go b/modules/context/context.go index 8fe5a76..4f6c9f4 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -18,6 +18,7 @@ import ( "github.com/charmbracelet/huh" gogit "github.com/go-git/go-git/v5" "github.com/urfave/cli/v3" + "golang.org/x/term" ) var errNotAGiteaRepo = errors.New("No Gitea login found. You might want to specify --repo (and --login) to work outside of a repository") @@ -48,6 +49,10 @@ func (ctx *TeaContext) IsInteractiveMode() bool { return ctx.Command.NumFlags() == 0 } +func shouldPromptFallbackLogin(login *config.Login, canPrompt bool) bool { + return login != nil && !login.Default && canPrompt +} + // InitCommand resolves the application context, and returns the active login, and if // available the repo slug. It does this by reading the config file for logins, parsing // the remotes of the .git repo specified in repoFlag or $PWD, and using overrides from @@ -131,7 +136,8 @@ and then run your command again.`) } // Only prompt for confirmation if the fallback login is not explicitly set as default - if !c.Login.Default { + canPrompt := term.IsTerminal(int(os.Stdin.Fd())) && term.IsTerminal(int(os.Stdout.Fd())) + if shouldPromptFallbackLogin(c.Login, canPrompt) { fallback := false if err := huh.NewConfirm(). Title(fmt.Sprintf("NOTE: no gitea login detected, whether falling back to login '%s'?", c.Login.Name)). @@ -143,6 +149,8 @@ and then run your command again.`) if !fallback { os.Exit(1) } + } else if !c.Login.Default { + fmt.Fprintf(os.Stderr, "NOTE: no gitea login detected, falling back to login '%s' in non-interactive mode.\n", c.Login.Name) } } diff --git a/modules/context/context_prompt_test.go b/modules/context/context_prompt_test.go new file mode 100644 index 0000000..ae0fded --- /dev/null +++ b/modules/context/context_prompt_test.go @@ -0,0 +1,52 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package context + +import ( + "testing" + + "code.gitea.io/tea/modules/config" +) + +func TestShouldPromptFallbackLogin(t *testing.T) { + tests := []struct { + name string + login *config.Login + canPrompt bool + expected bool + }{ + { + name: "no login", + login: nil, + canPrompt: true, + expected: false, + }, + { + name: "default login", + login: &config.Login{Default: true}, + canPrompt: true, + expected: false, + }, + { + name: "non-default no prompt", + login: &config.Login{Default: false}, + canPrompt: false, + expected: false, + }, + { + name: "non-default prompt", + login: &config.Login{Default: false}, + canPrompt: true, + expected: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := shouldPromptFallbackLogin(test.login, test.canPrompt); got != test.expected { + t.Fatalf("expected %v, got %v", test.expected, got) + } + }) + } +}