Allow export command to take first and last args
This commit is contained in:
		| @@ -26,6 +26,12 @@ var ( | |||||||
| 		Action: exportChain, | 		Action: exportChain, | ||||||
| 		Name:   "export", | 		Name:   "export", | ||||||
| 		Usage:  `export blockchain into file`, | 		Usage:  `export blockchain into file`, | ||||||
|  | 		Description: ` | ||||||
|  | Requires a first argument of the file to write to. | ||||||
|  | Optional second and third arguments control the first and | ||||||
|  | last block to write. In this mode, the file will be appended | ||||||
|  | if already existing. | ||||||
|  | 		`, | ||||||
| 	} | 	} | ||||||
| 	upgradedbCommand = cli.Command{ | 	upgradedbCommand = cli.Command{ | ||||||
| 		Action: upgradeDB, | 		Action: upgradeDB, | ||||||
| @@ -68,7 +74,21 @@ func exportChain(ctx *cli.Context) { | |||||||
| 	} | 	} | ||||||
| 	chain, _, _, _ := utils.MakeChain(ctx) | 	chain, _, _, _ := utils.MakeChain(ctx) | ||||||
| 	start := time.Now() | 	start := time.Now() | ||||||
| 	if err := utils.ExportChain(chain, ctx.Args().First()); err != nil { |  | ||||||
|  | 	var err error | ||||||
|  | 	if len(ctx.Args()) < 3 { | ||||||
|  | 		err = utils.ExportChain(chain, ctx.Args().First()) | ||||||
|  | 	} else { | ||||||
|  | 		// This can be improved to allow for numbers larger than 9223372036854775807 | ||||||
|  | 		first, ferr := strconv.ParseInt(ctx.Args().Get(1), 10, 64) | ||||||
|  | 		last, lerr := strconv.ParseInt(ctx.Args().Get(2), 10, 64) | ||||||
|  | 		if ferr != nil || lerr != nil { | ||||||
|  | 			utils.Fatalf("Export error in parsing parameters\n") | ||||||
|  | 		} | ||||||
|  | 		err = utils.ExportAppendChain(chain, ctx.Args().First(), uint64(first), uint64(last)) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if err != nil { | ||||||
| 		utils.Fatalf("Export error: %v\n", err) | 		utils.Fatalf("Export error: %v\n", err) | ||||||
| 	} | 	} | ||||||
| 	fmt.Printf("Export done in %v", time.Since(start)) | 	fmt.Printf("Export done in %v", time.Since(start)) | ||||||
|   | |||||||
| @@ -268,3 +268,18 @@ func ExportChain(chainmgr *core.ChainManager, fn string) error { | |||||||
| 	glog.Infoln("Exported blockchain to", fn) | 	glog.Infoln("Exported blockchain to", fn) | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func ExportAppendChain(chainmgr *core.ChainManager, fn string, first uint64, last uint64) error { | ||||||
|  | 	glog.Infoln("Exporting blockchain to", fn) | ||||||
|  | 	// TODO verify mode perms | ||||||
|  | 	fh, err := os.OpenFile(fn, os.O_CREATE|os.O_APPEND|os.O_WRONLY, os.ModePerm) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	defer fh.Close() | ||||||
|  | 	if err := chainmgr.ExportN(fh, first, last); err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	glog.Infoln("Exported blockchain to", fn) | ||||||
|  | 	return nil | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user