Skip to content

Commit

Permalink
add move CLI operation
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcmicu committed Oct 19, 2024
1 parent 7ad2a15 commit 6091766
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,25 @@ enum Commands {
delete_subcommand: DeleteSubcommands,
},

/// Undo the last row change
/// Reorder rows in database tables
Move {
#[arg(value_name = "TABLE", action = ArgAction::Set, help = TABLE_HELP)]
table: String,

#[arg(value_name = "ROW", action = ArgAction::Set,
help = "The number of the row from TABLE to be moved")]
row: u32,

#[arg(value_name = "AFTER", action = ArgAction::Set,
help = "The number of the row coming immediately before ROW in TABLE in the new row \
order. If this is 0, the row will be moved to the first position.")]
after: u32,
},

/// Undo changes to the database
Undo {},

/// Redo the last row change
/// Redo changes to the database that have been undone
Redo {},

/// Show recent changes to the database
Expand Down Expand Up @@ -623,22 +638,6 @@ async fn main() -> Result<()> {
}
};
}
Commands::Undo {} | Commands::Redo {} => {
let valve = build_valve(&cli.source, &cli.database).expect(BUILD_ERROR);
let updated_row = match &cli.command {
Commands::Undo {} => valve.undo().await?,
Commands::Redo {} => valve.redo().await?,
_ => unreachable!(),
};
if let Some(valve_row) = updated_row {
print!(
"{}",
json!(valve_row
.to_rich_json()
.expect("Error converting row to rich JSON"))
);
}
}
Commands::DropAll {} => {
let valve = build_valve(&cli.source, &cli.database).expect(BUILD_ERROR);
valve
Expand Down Expand Up @@ -882,6 +881,26 @@ async fn main() -> Result<()> {
.await
.expect("Error loading tables");
}
Commands::Move { table, row, after } => {
let valve = build_valve(&cli.source, &cli.database).expect(BUILD_ERROR);
valve.move_row(table, row, after).await?;
}
Commands::Redo {} | Commands::Undo {} => {
let valve = build_valve(&cli.source, &cli.database).expect(BUILD_ERROR);
let updated_row = match &cli.command {
Commands::Undo {} => valve.undo().await?,
Commands::Redo {} => valve.redo().await?,
_ => unreachable!(),
};
if let Some(valve_row) = updated_row {
print!(
"{}",
json!(valve_row
.to_rich_json()
.expect("Error converting row to rich JSON"))
);
}
}
Commands::Save { save_dir, tables } => {
let valve = build_valve(&cli.source, &cli.database).expect(BUILD_ERROR);
let tables = tables
Expand Down

0 comments on commit 6091766

Please sign in to comment.