@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"path/filepath"
24
24
"syscall"
25
+ "time"
25
26
26
27
"github.com/BurntSushi/toml"
27
28
"github.com/pingcap/errors"
@@ -179,9 +180,12 @@ func (t *TaskConfig) Init(
179
180
return errors .Trace (err )
180
181
}
181
182
182
- // Create output Dir if not exists
183
+ // Set default value when output is empty
183
184
if t .OutputDir == "" {
184
- return errors .New ("output-dir is missing from the task configuration" )
185
+ t .OutputDir = timestampOutputDir ()
186
+ if err := os .RemoveAll (t .OutputDir ); err != nil && ! os .IsNotExist (err ) {
187
+ log .Fatal ("fail to remove the temp directory" , zap .String ("path" , t .OutputDir ), zap .String ("error" , err .Error ()))
188
+ }
185
189
}
186
190
187
191
ok , err = pathExists (t .OutputDir )
@@ -292,6 +296,9 @@ type Config struct {
292
296
// config file
293
297
ConfigFile string
294
298
299
+ // export a template config file in the current directory
300
+ Template string `toml:"-" json:"-"`
301
+
295
302
// print version if set true
296
303
PrintVersion bool
297
304
}
@@ -305,6 +312,7 @@ func NewConfig() *Config {
305
312
fs .BoolVarP (& cfg .PrintVersion , "version" , "V" , false , "print version of sync_diff_inspector" )
306
313
fs .StringVarP (& cfg .LogLevel , "log-level" , "L" , "info" , "log level: debug, info, warn, error, fatal" )
307
314
fs .StringVarP (& cfg .ConfigFile , "config" , "C" , "" , "Config file" )
315
+ fs .StringVarP (& cfg .Template , "template" , "T" , "" , "<dm|norm> export a template config file in the current directory" )
308
316
fs .StringVar (& cfg .DMAddr , "dm-addr" , "" , "the address of DM" )
309
317
fs .StringVar (& cfg .DMTask , "dm-task" , "" , "identifier of dm task" )
310
318
fs .IntVar (& cfg .CheckThreadCount , "check-thread-count" , 1 , "how many goroutines are created to check data" )
@@ -327,6 +335,10 @@ func (c *Config) Parse(arguments []string) error {
327
335
return nil
328
336
}
329
337
338
+ if c .Template != "" {
339
+ return nil
340
+ }
341
+
330
342
// Load config file if specified.
331
343
if c .ConfigFile == "" {
332
344
return errors .Errorf ("argument --config is required" )
@@ -475,6 +487,10 @@ func (c *Config) CheckConfig() bool {
475
487
return true
476
488
}
477
489
490
+ func timestampOutputDir () string {
491
+ return filepath .Join (os .TempDir (), time .Now ().Format ("sync-diff.output.2006-01-02T15.04.05Z0700" ))
492
+ }
493
+
478
494
func pathExists (_path string ) (bool , error ) {
479
495
_ , err := os .Stat (_path )
480
496
if err != nil {
0 commit comments