-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmigrate.rs
More file actions
93 lines (77 loc) · 2.81 KB
/
migrate.rs
File metadata and controls
93 lines (77 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::path::Path;
use async_trait::async_trait;
use clap::{Arg, Command};
use liboxen::{error::OxenError, model::LocalRepository};
use crate::cmd::RunCmd;
use crate::helpers::migrations;
pub const NAME: &str = "migrate";
pub fn migrate_args(name: &'static str, desc: &'static str) -> Command {
Command::new(name)
.about(desc)
.arg(
Arg::new("PATH")
.help("Directory in which to apply the migration")
.required(true),
)
.arg(
Arg::new("all")
.long("all")
.short('a')
.help("Run the migration for all oxen repositories in this directory")
.action(clap::ArgAction::SetTrue),
)
}
pub fn subcommands(name: &'static str, desc: &'static str) -> Command {
let migrations = migrations();
let mut cmd = Command::new(name).about(desc).subcommand_required(true);
for (_, migration) in migrations {
cmd = cmd.subcommand(migrate_args(migration.name(), migration.description()))
}
cmd
}
pub struct MigrateCmd;
#[async_trait]
impl RunCmd for MigrateCmd {
fn name(&self) -> &str {
NAME
}
fn args(&self) -> Command {
// Setups the CLI args for the command
Command::new(NAME)
.about("Run a named migration on a server repository or set of repositories")
.subcommand_required(true)
.subcommand(subcommands("up", "Apply a named migration forward."))
.subcommand(subcommands("down", "Apply a named migration backward."))
}
async fn run(&self, args: &clap::ArgMatches) -> Result<(), OxenError> {
// Parse Args
let migrations = migrations();
if let Some((direction, sub_matches)) = args.subcommand()
&& let Some((migration, sub_matches)) = sub_matches.subcommand()
{
let migration = migrations
.get(migration)
.ok_or(OxenError::basic_str(&format!(
"Unknown migration: {migration}"
)))?;
let path_str = sub_matches.get_one::<String>("PATH").expect("required");
let path = Path::new(path_str);
let all = sub_matches.get_flag("all");
if direction == "up" {
let repo = LocalRepository::from_dir(path)?;
if migration.is_needed(&repo)? {
migration.up(path, all)?;
} else {
println!("Migration already applied: {}", migration.name());
}
} else if direction == "down" {
migration.down(path, all)?;
} else {
return Err(OxenError::basic_str(&format!(
"Unknown direction: {direction}"
)));
}
}
Ok(())
}
}