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>
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
// Package cli parses and validates archer command-line configuration.
|
|
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
// Config is the fully resolved runtime configuration.
|
|
type Config struct {
|
|
SourceDSN string
|
|
DestDSN string
|
|
Table string
|
|
DestTable string
|
|
Where string
|
|
Columns []string
|
|
FileDir string
|
|
Purge bool
|
|
BulkDelete bool
|
|
InsertMode string // "", "ignore", "replace"
|
|
|
|
ChunkSize int
|
|
|
|
ShardBy string // none|count|date
|
|
ShardRows int
|
|
ShardColumn string
|
|
ShardColumnType string
|
|
ShardTimeZone string
|
|
ZeroDate string
|
|
|
|
Format string
|
|
Compress string
|
|
CompressLevel int
|
|
CSVNull string
|
|
|
|
MaxLag time.Duration
|
|
CheckInterval time.Duration
|
|
CheckReplicaLag string
|
|
Retries int
|
|
|
|
SafeAutoIncrement bool
|
|
Progress ProgressValue
|
|
ProgressInterval time.Duration
|
|
Statistics bool
|
|
DryRun bool
|
|
AllowFullScan bool
|
|
ForceResume bool
|
|
Index string
|
|
Version bool
|
|
}
|
|
|
|
// Defaults returns a Config with documented default values applied.
|
|
func Defaults() *Config {
|
|
return &Config{
|
|
ChunkSize: 2000,
|
|
ShardBy: "none",
|
|
ShardRows: 0,
|
|
ShardTimeZone: "UTC",
|
|
Format: "dump",
|
|
Compress: "none",
|
|
CompressLevel: 3,
|
|
CSVNull: `\N`,
|
|
MaxLag: time.Second,
|
|
CheckInterval: time.Second,
|
|
Retries: 1,
|
|
SafeAutoIncrement: true,
|
|
Progress: ProgressValue{Mode: ProgressAuto},
|
|
ProgressInterval: time.Second,
|
|
}
|
|
}
|
|
|
|
// SourceConfig parses and normalizes the source DSN, forcing parseTime off so
|
|
// values arrive as raw bytes.
|
|
func (c *Config) SourceConfig() (*mysql.Config, error) {
|
|
return normalize(c.SourceDSN)
|
|
}
|
|
|
|
// DestConfig parses and normalizes the dest DSN.
|
|
func (c *Config) DestConfig() (*mysql.Config, error) {
|
|
if c.DestDSN == "" {
|
|
return nil, nil
|
|
}
|
|
return normalize(c.DestDSN)
|
|
}
|
|
|
|
func normalize(dsn string) (*mysql.Config, error) {
|
|
cfg, err := mysql.ParseDSN(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid DSN: %w", err)
|
|
}
|
|
cfg.ParseTime = false
|
|
if cfg.Loc == nil {
|
|
cfg.Loc = time.UTC
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
// SameInstance reports whether two normalized DSNs point at the same MySQL
|
|
// server, which determines whether dest writes and purges share a transaction.
|
|
func SameInstance(src, dst *mysql.Config) bool {
|
|
if src == nil || dst == nil {
|
|
return false
|
|
}
|
|
return src.Net == dst.Net && src.Addr == dst.Addr
|
|
}
|
|
|
|
// ShardMode helpers.
|
|
func (c *Config) IsDateShard() bool { return c.ShardBy == "date" }
|
|
func (c *Config) IsCountShard() bool { return c.ShardBy == "count" }
|
|
func (c *Config) HasFile() bool { return c.FileDir != "" }
|