Files
tasks/tools/tdbg/prompter.go
Michael SnowdenandGitHub df644cf934 Add tdbg dlq merge for v2 (#5025)
<!-- Describe what has changed in this PR -->
**What changed?**
This PR adds support for `--dlq-version v2` to the `tdbg dlq merge`
command.

<!-- Tell your future self why have you made these changes -->
**Why?**
Admins need this to be able to re-enqueue tasks.

<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
**How did you test it?**
I added an end-to-end test with a real workflow which does this:
1. Start a workflow whose WFT get DLQ'd
2. Merge that WFT back
3. Verify the workflow now succeeds.
4. Verify that the DLQ task is deleted.

I also added various unit tests for parameter validation and control
flow to get 100% test coverage.

Note that this won't work for cross-cluster tasks yet. I will add that
and update the history replication DLQ integration test as well in a
follow-up.

<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->
**Potential risks**


<!-- Is this PR a hotfix candidate or require that a notification be
sent to the broader community? (Yes/No) -->
**Is hotfix candidate?**
2023-10-26 05:45:39 +00:00

105 lines
3.2 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package tdbg
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
type (
// Prompter is a helper for prompting the user for confirmation.
Prompter struct {
writer io.Writer
reader io.Reader
exiter func(code int)
flagLookup BoolFlagLookup
}
// PrompterParams is used to configure a new Prompter.
PrompterParams struct {
// Reader defaults to os.Stdin.
Reader io.Reader
// Writer defaults to os.Stdout.
Writer io.Writer
// Exiter defaults to [os.Exit].
Exiter func(code int)
}
// PrompterOption is used to override default PrompterParams.
PrompterOption func(*PrompterParams)
// BoolFlagLookup can be satisfied by [github.com/urfave/cli/v2.Context].
BoolFlagLookup interface {
Bool(name string) bool
}
PrompterFactory func(c BoolFlagLookup) *Prompter
)
func NewPrompterFactory(opts ...PrompterOption) PrompterFactory {
return func(c BoolFlagLookup) *Prompter {
return NewPrompter(c, opts...)
}
}
// NewPrompter creates a new Prompter. In most cases, the first argument should be [github.com/urfave/cli/v2.Context].
func NewPrompter(c BoolFlagLookup, opts ...PrompterOption) *Prompter {
params := PrompterParams{
Writer: os.Stdout,
Reader: os.Stdin,
Exiter: os.Exit,
}
for _, opt := range opts {
opt(&params)
}
return &Prompter{
writer: params.Writer,
reader: params.Reader,
exiter: params.Exiter,
flagLookup: c,
}
}
// Prompt the user for confirmation. If the user does not respond with "y" or "yes" (case-insensitive and without
// leading or trailing space), the process will exit with code 1.
func (p *Prompter) Prompt(msg string) {
if p.flagLookup.Bool(FlagYes) {
return
}
_, err := p.writer.Write([]byte(msg + " [y/N]: "))
if err != nil {
panic(fmt.Errorf("failed to write prompt: %w", err))
}
reader := bufio.NewReader(p.reader)
text, err := reader.ReadString('\n')
if err != nil {
panic(fmt.Errorf("failed to read prompt: %w", err))
}
textLower := strings.ToLower(strings.TrimSpace(text))
if textLower != "y" && textLower != "yes" {
p.exiter(1)
}
}