mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-26 11:01:25 +01:00
fix: do not ask for confirmation right after prompting for input
secret set, variable set, team create, webhook create and admin user create asked a confirmation that defaults to No right after the user typed the missing values, so pressing Enter discarded the input. They now submit what the user entered; deletions still confirm. Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
6 files changed
+43
-29
No files matched your search
@@ -78,7 +78,6 @@ func (a *application) actionValueCommand(secret bool) *cobra.Command {
|
|||||||
if strings.EqualFold(name, "CI") || strings.HasPrefix(strings.ToUpper(name), "GITEA_") || strings.HasPrefix(strings.ToUpper(name), "GITHUB_") {
|
if strings.EqualFold(name, "CI") || strings.HasPrefix(strings.ToUpper(name), "GITEA_") || strings.HasPrefix(strings.ToUpper(name), "GITHUB_") {
|
||||||
return clierr.Wrap(clierr.Usage, errors.New("CI and names beginning GITEA_ or GITHUB_ are reserved"))
|
return clierr.Wrap(clierr.Usage, errors.New("CI and names beginning GITEA_ or GITHUB_ are reserved"))
|
||||||
}
|
}
|
||||||
prompted := false
|
|
||||||
if verb == "set" {
|
if verb == "set" {
|
||||||
var err error
|
var err error
|
||||||
if value, err = a.readSecret(cmd, value, file, "value", "value-file"); err != nil {
|
if value, err = a.readSecret(cmd, value, file, "value", "value-file"); err != nil {
|
||||||
@@ -100,7 +99,6 @@ func (a *application) actionValueCommand(secret bool) *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
prompted = true
|
|
||||||
}
|
}
|
||||||
if value == "" {
|
if value == "" {
|
||||||
return clierr.Wrap(clierr.Usage, fmt.Errorf("%s value cannot be empty", kind))
|
return clierr.Wrap(clierr.Usage, fmt.Errorf("%s value cannot be empty", kind))
|
||||||
@@ -113,7 +111,7 @@ func (a *application) actionValueCommand(secret bool) *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if verb == "delete" || prompted {
|
if verb == "delete" {
|
||||||
if err := a.confirm(cmd, verb+" Actions "+kind+" "+name); err != nil {
|
if err := a.confirm(cmd, verb+" Actions "+kind+" "+name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,6 @@ func (a *application) adminCommand() *cobra.Command {
|
|||||||
command.Flags().BoolVar(&mustChangePassword, "must-change-password", true, "Require password change on next sign-in (use =false to disable)")
|
command.Flags().BoolVar(&mustChangePassword, "must-change-password", true, "Require password change on next sign-in (use =false to disable)")
|
||||||
}
|
}
|
||||||
command.RunE = func(cmd *cobra.Command, args []string) error {
|
command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
prompted := false
|
|
||||||
if verb == "create" {
|
if verb == "create" {
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
if cmd.Flags().Changed("username") {
|
if cmd.Flags().Changed("username") {
|
||||||
@@ -88,11 +87,9 @@ func (a *application) adminCommand() *cobra.Command {
|
|||||||
value *string
|
value *string
|
||||||
flag string
|
flag string
|
||||||
}{{&username, "username"}, {&email, "email"}} {
|
}{{&username, "username"}, {&email, "email"}} {
|
||||||
completed, err := a.require(cmd, field.value, field.flag)
|
if _, err := a.require(cmd, field.value, field.flag); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
prompted = prompted || completed
|
|
||||||
}
|
}
|
||||||
opts.Email = &email
|
opts.Email = &email
|
||||||
opts.FullName = &fullName
|
opts.FullName = &fullName
|
||||||
@@ -159,7 +156,6 @@ func (a *application) adminCommand() *cobra.Command {
|
|||||||
if password != confirmation {
|
if password != confirmation {
|
||||||
return clierr.Wrap(clierr.Usage, errors.New("passwords do not match"))
|
return clierr.Wrap(clierr.Usage, errors.New("passwords do not match"))
|
||||||
}
|
}
|
||||||
prompted = true
|
|
||||||
}
|
}
|
||||||
if password == "" {
|
if password == "" {
|
||||||
return clierr.Wrap(clierr.Usage, errors.New("password cannot be empty"))
|
return clierr.Wrap(clierr.Usage, errors.New("password cannot be empty"))
|
||||||
@@ -178,7 +174,7 @@ func (a *application) adminCommand() *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if verb == "delete" || prompted {
|
if verb == "delete" {
|
||||||
if err := a.confirm(cmd, verb+" account "+username+" on "+target.Host); err != nil {
|
if err := a.confirm(cmd, verb+" account "+username+" on "+target.Host); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ func TestAdminPromptAndFlagsReachEquivalentPayloads(t *testing.T) {
|
|||||||
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account"}))
|
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account"}))
|
||||||
require.Equal(t, []string{"email"}, prompt.asked)
|
require.Equal(t, []string{"email"}, prompt.asked)
|
||||||
require.Len(t, prompt.secrets, 2)
|
require.Len(t, prompt.secrets, 2)
|
||||||
require.Len(t, prompt.confirmations, 1)
|
require.Empty(t, prompt.confirmations)
|
||||||
out.Reset()
|
out.Reset()
|
||||||
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account", "--email", "account@example.test", "--password", " preserved password ", "--no-input"}))
|
require.NoError(t, Execute(context.Background(), deps, []string{"admin", "user", "create", "account", "--full-name", "Test Account", "--email", "account@example.test", "--password", " preserved password ", "--no-input"}))
|
||||||
require.Equal(t, payloads[0], payloads[1])
|
require.Equal(t, payloads[0], payloads[1])
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ func (a *application) teamCommand() *cobra.Command {
|
|||||||
command.RunE = func(cmd *cobra.Command, args []string) error {
|
command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
var id int64
|
var id int64
|
||||||
var err error
|
var err error
|
||||||
prompted := false
|
|
||||||
opts := services.TeamOptions{}
|
opts := services.TeamOptions{}
|
||||||
if verb != "create" {
|
if verb != "create" {
|
||||||
id, err = number(args[0])
|
id, err = number(args[0])
|
||||||
@@ -85,8 +84,7 @@ func (a *application) teamCommand() *cobra.Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if verb == "create" {
|
if verb == "create" {
|
||||||
prompted, err = a.require(cmd, &name, "name")
|
if _, err := a.require(cmd, &name, "name"); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
opts.Name = &name
|
opts.Name = &name
|
||||||
@@ -131,11 +129,6 @@ func (a *application) teamCommand() *cobra.Command {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
service := services.Teams{Client: target.client}
|
service := services.Teams{Client: target.client}
|
||||||
if prompted {
|
|
||||||
if err := a.confirm(cmd, "create team "+name+" in "+args[0]+" on "+target.Host); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var item services.Team
|
var item services.Team
|
||||||
switch verb {
|
switch verb {
|
||||||
case "view":
|
case "view":
|
||||||
|
|||||||
@@ -257,8 +257,6 @@ func TestConfirmationsNameResolvedValues(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{[]string{"wiki", "create"}, map[string]string{"title": "Home", "body": "text"}, "create wiki page Home on alice/project"},
|
{[]string{"wiki", "create"}, map[string]string{"title": "Home", "body": "text"}, "create wiki page Home on alice/project"},
|
||||||
{[]string{"release", "create"}, map[string]string{"tag": "v1.0.0"}, "create release v1.0.0 on alice/project"},
|
{[]string{"release", "create"}, map[string]string{"tag": "v1.0.0"}, "create release v1.0.0 on alice/project"},
|
||||||
{[]string{"webhook", "create"}, map[string]string{"url": "https://hooks.test/path"}, "create webhook https://hooks.test/path"},
|
|
||||||
{[]string{"team", "create", "acme"}, map[string]string{"name": "Developers"}, "create team Developers in acme"},
|
|
||||||
{[]string{"time", "delete", "5", "7"}, nil, "delete tracked time entry 7 on alice/project #5"},
|
{[]string{"time", "delete", "5", "7"}, nil, "delete tracked time entry 7 on alice/project #5"},
|
||||||
} {
|
} {
|
||||||
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
|
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
|
||||||
@@ -280,6 +278,41 @@ func TestConfirmationsNameResolvedValues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPromptedCreateSubmitsWithoutConfirmation(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
args []string
|
||||||
|
inputs map[string]string
|
||||||
|
secret, write string
|
||||||
|
}{
|
||||||
|
{[]string{"secret", "set", "TOKEN"}, nil, "private", "PUT /api/v1/repos/alice/project/actions/secrets/TOKEN"},
|
||||||
|
{[]string{"variable", "set", "MODE"}, map[string]string{"Variable value": "fast"}, "", "POST /api/v1/repos/alice/project/actions/variables/MODE"},
|
||||||
|
{[]string{"webhook", "create"}, map[string]string{"url": "https://hooks.test/path"}, "", "POST /api/v1/repos/alice/project/hooks"},
|
||||||
|
{[]string{"team", "create", "acme"}, map[string]string{"name": "Developers"}, "", "POST /api/v1/orgs/acme/teams"},
|
||||||
|
} {
|
||||||
|
t.Run(strings.Join(tc.args, " "), func(t *testing.T) {
|
||||||
|
var writes []string
|
||||||
|
server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
writes = append(writes, r.Method+" "+r.URL.Path)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
_, _ = w.Write([]byte(`{"id":1}`))
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
deps, _, _ := testDeps(t)
|
||||||
|
deps.Getenv = serverEnv(server.URL, map[string]string{"TEA_REPO": "alice/project"})
|
||||||
|
prompt := &scriptedPrompter{inputs: tc.inputs, secret: tc.secret}
|
||||||
|
deps.Prompter = prompt
|
||||||
|
deps.Streams.InTTY, deps.Streams.ErrTTY = true, true
|
||||||
|
require.NoError(t, Execute(context.Background(), deps, tc.args))
|
||||||
|
require.Empty(t, prompt.confirmations)
|
||||||
|
require.Equal(t, []string{tc.write}, writes)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type unexpectedForms struct{ t *testing.T }
|
type unexpectedForms struct{ t *testing.T }
|
||||||
|
|
||||||
func (f unexpectedForms) CreateIssue(context.Context, terminal.IssueFormChoices) (terminal.IssueFormValues, error) {
|
func (f unexpectedForms) CreateIssue(context.Context, terminal.IssueFormChoices) (terminal.IssueFormValues, error) {
|
||||||
|
|||||||
@@ -88,7 +88,6 @@ func (a *application) webhookCommand() *cobra.Command {
|
|||||||
command.RunE = func(cmd *cobra.Command, args []string) error {
|
command.RunE = func(cmd *cobra.Command, args []string) error {
|
||||||
var id int64
|
var id int64
|
||||||
var err error
|
var err error
|
||||||
prompted := false
|
|
||||||
opts := services.WebhookOptions{}
|
opts := services.WebhookOptions{}
|
||||||
if verb != "create" {
|
if verb != "create" {
|
||||||
id, err = number(args[0])
|
id, err = number(args[0])
|
||||||
@@ -125,8 +124,7 @@ func (a *application) webhookCommand() *cobra.Command {
|
|||||||
endpoint = args[0]
|
endpoint = args[0]
|
||||||
}
|
}
|
||||||
if verb == "create" {
|
if verb == "create" {
|
||||||
prompted, err = a.require(cmd, &endpoint, "url")
|
if _, err := a.require(cmd, &endpoint, "url"); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,12 +198,8 @@ func (a *application) webhookCommand() *cobra.Command {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if verb == "delete" || prompted {
|
if verb == "delete" {
|
||||||
subject := endpoint
|
if err := a.confirm(cmd, "delete webhook "+args[0]); err != nil {
|
||||||
if verb == "delete" {
|
|
||||||
subject = args[0]
|
|
||||||
}
|
|
||||||
if err := a.confirm(cmd, verb+" webhook "+subject); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user