Files
archer/internal/cli/validate.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

120 lines
3.2 KiB
Go

package cli
import (
"strings"
"time"
"archer/internal/archererr"
)
// Validate checks required flags and the inter-parameter dependency rules.
// The shard column's existence and type are checked later, after metadata is
// loaded, so the error can list candidate columns.
func (c *Config) Validate() error {
if c.SourceDSN == "" {
return cfgErr("--source is required")
}
if c.Table == "" {
return cfgErr("--table is required")
}
if strings.TrimSpace(c.Where) == "" {
return cfgErr("--where is required")
}
if !c.HasFile() && c.DestDSN == "" && !c.Purge {
return cfgErr("at least one output target is required: --file, --dest or --purge")
}
if c.ChunkSize <= 0 {
return cfgErr("--chunk-size must be greater than 0")
}
if c.Retries < 0 {
return cfgErr("--retries must be 0 or greater")
}
if err := c.Progress.resolve(); err != nil {
return cfgErr("%v", err)
}
if c.ProgressInterval <= 0 {
return cfgErr("--progress-interval must be greater than 0")
}
switch c.ShardBy {
case "none", "count", "date":
default:
return cfgErr("--shard-by must be one of none, count, date")
}
switch c.ShardBy {
case "date":
if c.ShardRows < 0 {
return cfgErr("--shard-rows must be 0 or greater")
}
default:
if c.ShardColumn != "" {
return cfgErr("--shard-column is only valid with --shard-by date")
}
if c.ShardColumnType != "" {
return cfgErr("--shard-column-type is only valid with --shard-by date")
}
if c.ZeroDate != "" {
return cfgErr("--zero-date is only valid with --shard-by date")
}
}
if c.ShardBy == "none" && c.ShardRows != 0 {
return cfgErr("--shard-rows is only valid with --shard-by count or date")
}
if c.ShardBy == "count" && c.ShardRows == 0 {
return cfgErr("--shard-rows must be greater than 0 for --shard-by count")
}
if c.BulkDelete && !c.Purge {
return cfgErr("--bulk-delete requires --purge")
}
if c.BulkDelete && c.ShardBy == "date" {
return cfgErr("--bulk-delete cannot be used with --shard-by date; delete by IN list instead")
}
switch c.Format {
case "dump", "csv", "jsonl":
default:
return cfgErr("--format must be one of dump, csv, jsonl")
}
switch c.Compress {
case "none", "gzip", "zstd", "xz":
default:
return cfgErr("--compress must be one of none, gzip, zstd, xz")
}
if c.CompressLevel < 1 || c.CompressLevel > 9 {
return cfgErr("--compress-level must be between 1 and 9")
}
if c.Compress == "none" && c.CompressLevel != 3 {
return cfgErr("--compress-level is only valid when compression is enabled")
}
switch c.ZeroDate {
case "", "bucket":
default:
return cfgErr("--zero-date must be empty or bucket")
}
if _, err := time.LoadLocation(c.ShardTimeZone); err != nil {
return cfgErr("--shard-time-zone %q is not a valid location: %v", c.ShardTimeZone, err)
}
if _, err := c.SourceConfig(); err != nil {
return cfgErr("--source: %v", err)
}
if _, err := c.DestConfig(); err != nil {
return cfgErr("--dest: %v", err)
}
if c.CheckReplicaLag != "" {
if err := validateDSN(c.CheckReplicaLag); err != nil {
return cfgErr("--check-replica-lag: %v", err)
}
}
return nil
}
func validateDSN(dsn string) error {
_, err := normalize(dsn)
return err
}
func cfgErr(format string, args ...any) error {
return archererr.Newf(archererr.KindConfig, format, args...)
}