1
0
mirror of https://github.com/go-task/task.git synced 2025-03-19 21:17:46 +02:00
task/internal/logger/logger.go

151 lines
3.3 KiB
Go
Raw Normal View History

package logger
import (
"bufio"
"io"
"os"
"strconv"
"strings"
2019-05-26 18:36:39 -03:00
"github.com/fatih/color"
"golang.org/x/exp/slices"
2023-10-07 16:55:43 -05:00
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/term"
)
var (
ErrPromptCancelled = errors.New("prompt cancelled")
ErrNoTerminal = errors.New("no terminal")
2019-05-26 18:36:39 -03:00
)
2023-03-31 19:13:29 +00:00
type (
Color func() PrintFunc
PrintFunc func(io.Writer, string, ...any)
)
2019-05-26 18:36:39 -03:00
func Default() PrintFunc {
return color.New(envColor("TASK_COLOR_RESET", color.Reset)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Blue() PrintFunc {
return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Green() PrintFunc {
return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Cyan() PrintFunc {
return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Yellow() PrintFunc {
return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Magenta() PrintFunc {
return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)).FprintfFunc()
}
2023-03-31 19:13:29 +00:00
func Red() PrintFunc {
return color.New(envColor("TASK_COLOR_RED", color.FgRed)).FprintfFunc()
}
func envColor(env string, defaultColor color.Attribute) color.Attribute {
2023-02-17 03:12:44 +03:00
if os.Getenv("FORCE_COLOR") != "" {
color.NoColor = false
}
override, err := strconv.Atoi(os.Getenv(env))
if err == nil {
return color.Attribute(override)
}
return defaultColor
}
2019-05-26 18:36:39 -03:00
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
// with optional color.
type Logger struct {
2023-10-07 16:55:43 -05:00
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
Verbose bool
Color bool
AssumeYes bool
AssumeTerm bool // Used for testing
}
2019-05-26 18:36:39 -03:00
// Outf prints stuff to STDOUT.
func (l *Logger) Outf(color Color, s string, args ...any) {
l.FOutf(l.Stdout, color, s, args...)
}
// FOutf prints stuff to the given writer.
func (l *Logger) FOutf(w io.Writer, color Color, s string, args ...any) {
if len(args) == 0 {
s, args = "%s", []any{s}
}
if !l.Color {
color = Default
}
print := color()
print(w, s, args...)
}
2019-05-26 18:36:39 -03:00
// VerboseOutf prints stuff to STDOUT if verbose mode is enabled.
func (l *Logger) VerboseOutf(color Color, s string, args ...any) {
if l.Verbose {
l.Outf(color, s, args...)
}
}
2019-05-26 18:36:39 -03:00
// Errf prints stuff to STDERR.
func (l *Logger) Errf(color Color, s string, args ...any) {
if len(args) == 0 {
s, args = "%s", []any{s}
}
if !l.Color {
color = Default
}
print := color()
print(l.Stderr, s, args...)
}
2019-05-26 18:36:39 -03:00
// VerboseErrf prints stuff to STDERR if verbose mode is enabled.
func (l *Logger) VerboseErrf(color Color, s string, args ...any) {
if l.Verbose {
l.Errf(color, s, args...)
}
}
2023-10-07 16:55:43 -05:00
func (l *Logger) Prompt(color Color, prompt string, defaultValue string, continueValues ...string) error {
if l.AssumeYes {
l.Outf(color, "%s [assuming yes]\n", prompt)
return nil
}
if !l.AssumeTerm && !term.IsTerminal() {
return ErrNoTerminal
}
if len(continueValues) == 0 {
2023-10-07 16:55:43 -05:00
return errors.New("no continue values provided")
}
2023-10-07 16:55:43 -05:00
l.Outf(color, "%s [%s/%s]\n", prompt, strings.ToLower(continueValues[0]), strings.ToUpper(defaultValue))
reader := bufio.NewReader(l.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
2023-10-07 16:55:43 -05:00
return err
}
2023-10-07 16:55:43 -05:00
input = strings.TrimSpace(strings.ToLower(input))
2023-10-07 16:55:43 -05:00
if !slices.Contains(continueValues, input) {
return ErrPromptCancelled
}
return nil
}