Files
archer/internal/cli/progress.go
T
voidandClaude Sonnet 4.6 bc4a6b504f Initial commit: archer, a Go-native MySQL/MariaDB table archiver
Renamed from arcdb: module path, cmd/archer, internal/archererr,
docs, build scripts and ARCHER_* environment variables.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-11-06 22:12:17 +08:00

85 lines
1.8 KiB
Go

package cli
import (
"fmt"
"strconv"
"strings"
)
// ProgressMode is the resolved meaning of --progress.
type ProgressMode int
const (
ProgressAuto ProgressMode = iota // live on a terminal, silent otherwise
ProgressOff
ProgressOn
ProgressRows // enabled, plus a row-count threshold
)
// ProgressValue is the flag.Value behind --progress. It keeps the raw
// argument so invalid values are reported as configuration errors by Validate
// rather than as bare flag parse errors.
type ProgressValue struct {
Mode ProgressMode
Rows int64
raw string
}
// Set records the raw argument; resolve happens in Validate.
func (p *ProgressValue) Set(s string) error {
p.raw = s
return nil
}
func (p *ProgressValue) String() string {
if p.raw != "" {
return p.raw
}
switch p.Mode {
case ProgressOn:
return "on"
case ProgressOff:
return "off"
case ProgressRows:
return strconv.FormatInt(p.Rows, 10)
default:
return "auto"
}
}
// resolve parses the recorded argument into Mode and Rows.
func (p *ProgressValue) resolve() error {
switch strings.ToLower(strings.TrimSpace(p.raw)) {
case "", "auto":
p.Mode, p.Rows = ProgressAuto, 0
case "on":
p.Mode, p.Rows = ProgressOn, 0
case "off":
p.Mode, p.Rows = ProgressOff, 0
default:
n, err := strconv.ParseInt(strings.TrimSpace(p.raw), 10, 64)
if err != nil || n < 0 {
return fmt.Errorf("--progress must be auto, on, off or a non-negative integer, got %q", p.raw)
}
if n == 0 {
p.Mode, p.Rows = ProgressOff, 0
} else {
p.Mode, p.Rows = ProgressRows, n
}
}
return nil
}
// Enabled reports whether progress output should be produced. tty describes
// whether stderr is a terminal.
func (p ProgressValue) Enabled(tty bool) bool {
switch p.Mode {
case ProgressOn, ProgressRows:
return true
case ProgressAuto:
return tty
default:
return false
}
}