2022-07-26 21:43:26 +02:00
|
|
|
package confirm
|
|
|
|
|
|
|
|
import (
|
2022-08-05 00:45:19 +02:00
|
|
|
"fmt"
|
2022-07-26 21:43:26 +02:00
|
|
|
"os"
|
|
|
|
|
2022-08-03 18:28:02 +02:00
|
|
|
"github.com/alecthomas/kong"
|
2022-07-26 21:43:26 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-08-05 00:45:19 +02:00
|
|
|
|
2022-08-03 18:28:02 +02:00
|
|
|
"github.com/charmbracelet/gum/style"
|
2022-07-26 21:43:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run provides a shell script interface for prompting a user to confirm an
|
|
|
|
// action with an affirmative or negative answer.
|
|
|
|
func (o Options) Run() error {
|
|
|
|
m, err := tea.NewProgram(model{
|
|
|
|
affirmative: o.Affirmative,
|
|
|
|
negative: o.Negative,
|
2022-07-29 17:48:37 +02:00
|
|
|
confirmation: o.Default,
|
2022-08-08 19:57:55 +02:00
|
|
|
timeout: o.Timeout,
|
|
|
|
hasTimeout: o.Timeout > 0,
|
2022-07-26 21:43:26 +02:00
|
|
|
prompt: o.Prompt,
|
|
|
|
selectedStyle: o.SelectedStyle.ToLipgloss(),
|
|
|
|
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
|
|
|
|
promptStyle: o.PromptStyle.ToLipgloss(),
|
|
|
|
}, tea.WithOutput(os.Stderr)).StartReturningModel()
|
|
|
|
|
|
|
|
if err != nil {
|
2022-08-05 00:45:19 +02:00
|
|
|
return fmt.Errorf("unable to run confirm: %w", err)
|
2022-07-26 21:43:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 17:48:37 +02:00
|
|
|
if m.(model).confirmation {
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-07-26 21:43:26 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-08-03 18:28:02 +02:00
|
|
|
|
|
|
|
// BeforeReset hook. Used to unclutter style flags.
|
|
|
|
func (o Options) BeforeReset(ctx *kong.Context) error {
|
2022-08-05 00:45:19 +02:00
|
|
|
style.HideFlags(ctx)
|
|
|
|
return nil
|
2022-08-03 18:28:02 +02:00
|
|
|
}
|