Fix new tty prompt (#897)

Fix #827

---------

Co-authored-by: silverwind <silverwind@noreply.gitea.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/897
Reviewed-by: silverwind <silverwind@noreply.gitea.com>
This commit is contained in:
Lunny Xiao
2026-02-16 03:37:44 +00:00
parent dfd400f15b
commit 87c8c3d6e0
2 changed files with 61 additions and 1 deletions

View File

@@ -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)
}
}