|
4 | 4 | mod export;
|
5 | 5 | mod import;
|
6 | 6 |
|
7 |
| -use crate::export::ExporterOptions; |
8 |
| -use clap::Parser; |
| 7 | +use clap::{Parser, Subcommand}; |
| 8 | +use std::path::PathBuf; |
| 9 | +use starcoin_storage::{db_storage::DBStorage, storage::StorageInstance, Storage, StorageVersion}; |
| 10 | +use starcoin_statedb::ChainStateDB; |
| 11 | +use std::sync::Arc; |
| 12 | + |
| 13 | +#[derive(Parser)] |
| 14 | +#[clap(name = "resource-code-exporter", about = "Export and import state data")] |
| 15 | +struct Cli { |
| 16 | + #[clap(subcommand)] |
| 17 | + command: Commands, |
| 18 | +} |
| 19 | + |
| 20 | +#[derive(Subcommand)] |
| 21 | +enum Commands { |
| 22 | + /// Export state data to CSV file |
| 23 | + Export { |
| 24 | + #[clap(long, short = 'o', parse(from_os_str))] |
| 25 | + /// Output file path, e.g. accounts.csv |
| 26 | + output: PathBuf, |
| 27 | + |
| 28 | + #[clap(long, short = 'i', parse(from_os_str))] |
| 29 | + /// Starcoin node db path, e.g. ~/.starcoin/barnard/starcoindb/db/starcoindb |
| 30 | + db_path: PathBuf, |
| 31 | + |
| 32 | + #[clap(long)] |
| 33 | + /// Block id to export state at |
| 34 | + block_id: starcoin_crypto::HashValue, |
| 35 | + }, |
| 36 | + /// Import state data from CSV file |
| 37 | + Import { |
| 38 | + #[clap(long, short = 'i', parse(from_os_str))] |
| 39 | + /// Input CSV file path |
| 40 | + input: PathBuf, |
| 41 | + |
| 42 | + #[clap(long, short = 'o', parse(from_os_str))] |
| 43 | + /// Output database path |
| 44 | + db_path: PathBuf, |
| 45 | + }, |
| 46 | +} |
9 | 47 |
|
10 | 48 | fn main() -> anyhow::Result<()> {
|
11 |
| - let option: ExporterOptions = ExporterOptions::parse(); |
12 |
| - let output = option.output.as_path(); |
13 |
| - let block_id = option.block_id; |
14 |
| - export::export( |
15 |
| - option.db_path.display().to_string().as_str(), |
16 |
| - output, |
17 |
| - block_id, |
18 |
| - )?; |
| 49 | + let cli = Cli::parse(); |
| 50 | + |
| 51 | + match cli.command { |
| 52 | + Commands::Export { |
| 53 | + output, |
| 54 | + db_path, |
| 55 | + block_id, |
| 56 | + } => { |
| 57 | + export::export( |
| 58 | + db_path.display().to_string().as_str(), |
| 59 | + &output, |
| 60 | + block_id, |
| 61 | + )?; |
| 62 | + } |
| 63 | + Commands::Import { input, db_path } => { |
| 64 | + // Create new statedb for import |
| 65 | + let db_storage = DBStorage::open_with_cfs( |
| 66 | + &db_path, |
| 67 | + StorageVersion::current_version() |
| 68 | + .get_column_family_names() |
| 69 | + .to_vec(), |
| 70 | + false, |
| 71 | + Default::default(), |
| 72 | + None, |
| 73 | + )?; |
| 74 | + let storage = Storage::new(StorageInstance::new_db_instance(db_storage))?; |
| 75 | + let storage = Arc::new(storage); |
| 76 | + let statedb = ChainStateDB::new(storage.clone(), None); |
| 77 | + |
| 78 | + // Import data |
| 79 | + import::import(&statedb, &input)?; |
| 80 | + } |
| 81 | + } |
| 82 | + |
19 | 83 | Ok(())
|
20 | 84 | }
|
0 commit comments