package cli import ( "flag" "fmt" "io" "strings" ) // Parse parses args (without the program name) into a Config. It does not // validate combinations; call Validate for that. func Parse(args []string, errOut io.Writer) (*Config, error) { c := Defaults() var ( columnsArg string limitArg = -1 insertIgnoreArg bool replaceArg bool ) fs := flag.NewFlagSet("archer", flag.ContinueOnError) fs.SetOutput(errOut) fs.StringVar(&c.SourceDSN, "source", "", "source MySQL DSN (required)") fs.StringVar(&c.DestDSN, "dest", "", "destination MySQL DSN") fs.StringVar(&c.Table, "table", "", "source table (required)") fs.StringVar(&c.DestTable, "dest-table", "", "destination table (defaults to --table)") fs.StringVar(&c.Where, "where", "", "row filter for the source table (required)") fs.StringVar(&columnsArg, "columns", "", "comma separated columns to export (default: all)") fs.StringVar(&c.FileDir, "file", "", "output directory for shard files") fs.BoolVar(&c.Purge, "purge", false, "delete archived rows from the source") fs.BoolVar(&c.BulkDelete, "bulk-delete", false, "delete by key range instead of IN list (requires --purge)") fs.BoolVar(&insertIgnoreArg, "insert-ignore", false, "use INSERT IGNORE for --dest") fs.BoolVar(&replaceArg, "replace", false, "use REPLACE for --dest") fs.IntVar(&c.ChunkSize, "chunk-size", c.ChunkSize, "rows per SELECT / transaction / checkpoint") fs.IntVar(&limitArg, "limit", -1, "deprecated alias for --chunk-size") fs.StringVar(&c.ShardBy, "shard-by", c.ShardBy, "shard mode: none|count|date") fs.IntVar(&c.ShardRows, "shard-rows", c.ShardRows, "rows per shard file (count/date modes)") fs.StringVar(&c.ShardColumn, "shard-column", "", "date or epoch column used for date sharding") fs.StringVar(&c.ShardColumnType, "shard-column-type", "", "epoch unit: epoch-seconds|epoch-milliseconds|epoch-microseconds") fs.StringVar(&c.ShardTimeZone, "shard-time-zone", c.ShardTimeZone, "time zone for epoch shard values") fs.StringVar(&c.ZeroDate, "zero-date", "", "zero date handling: bucket (default: error)") fs.StringVar(&c.Format, "format", c.Format, "output format: dump|csv|jsonl") fs.StringVar(&c.Compress, "compress", c.Compress, "compression: none|gzip|zstd|xz") fs.IntVar(&c.CompressLevel, "compress-level", c.CompressLevel, "compression effort 1-9 (higher is smaller and slower)") fs.StringVar(&c.CSVNull, "csv-null", c.CSVNull, "string used for NULL in csv output") fs.DurationVar(&c.MaxLag, "max-lag", c.MaxLag, "maximum tolerated replica lag") fs.DurationVar(&c.CheckInterval, "check-interval", c.CheckInterval, "wait between replica lag checks") fs.StringVar(&c.CheckReplicaLag, "check-replica-lag", "", "DSN of a replica whose lag is checked before each fetch") fs.IntVar(&c.Retries, "retries", c.Retries, "retries for cross-instance commits") fs.BoolVar(&c.SafeAutoIncrement, "safe-auto-increment", c.SafeAutoIncrement, "never purge the maximum auto-increment row") fs.Var(&c.Progress, "progress", "progress output: auto|on|off|N (default auto; N: also every N rows)") fs.DurationVar(&c.ProgressInterval, "progress-interval", c.ProgressInterval, "minimum interval between progress updates") fs.BoolVar(&c.Statistics, "statistics", false, "print a summary when finished") fs.BoolVar(&c.DryRun, "dry-run", false, "print resolved config and SQL without writing") fs.BoolVar(&c.AllowFullScan, "allow-full-scan", false, "allow date sharding without a usable index") fs.BoolVar(&c.ForceResume, "force-resume", false, "resume even when the config fingerprint changed") fs.StringVar(&c.Index, "index", "", "force a unique index for the scan key") fs.BoolVar(&c.Version, "version", false, "print version and exit") if err := fs.Parse(args); err != nil { return nil, err } if c.Version { return c, nil } set := map[string]bool{} fs.Visit(func(f *flag.Flag) { set[f.Name] = true }) if set["limit"] { if set["chunk-size"] { return nil, fmt.Errorf("--limit is a deprecated alias of --chunk-size; pass only one") } c.ChunkSize = limitArg } if columnsArg != "" { for _, part := range strings.Split(columnsArg, ",") { part = strings.TrimSpace(part) if part != "" { c.Columns = append(c.Columns, part) } } } if insertIgnoreArg && replaceArg { return nil, fmt.Errorf("--insert-ignore and --replace are mutually exclusive") } switch { case insertIgnoreArg: c.InsertMode = "ignore" case replaceArg: c.InsertMode = "replace" } if c.DestTable == "" { c.DestTable = c.Table } return c, nil }