mirror of
https://gitea.com/gitea/tea.git
synced 2026-02-22 06:13:32 +01:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
52
modules/context/context_prompt_test.go
Normal file
52
modules/context/context_prompt_test.go
Normal file
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user