Add migration subcommand

This commit is contained in:
Valentin Brandl 2019-07-07 14:52:42 +02:00
parent c5bbd14a05
commit c71925f61e
No known key found for this signature in database
GPG Key ID: 30D341DD34118D7D
2 changed files with 48 additions and 3 deletions

View File

@ -46,6 +46,14 @@ pub(crate) struct Opt {
)] )]
/// The logfile /// The logfile
pub(crate) logfile: PathBuf, pub(crate) logfile: PathBuf,
#[structopt(subcommand)]
pub(crate) migrate: Option<Migration>,
}
#[derive(StructOpt, Debug, Clone, Copy)]
pub(crate) enum Migration {
#[structopt(name = "migrate-commit-count")]
CacheCommitCount,
} }
pub(crate) fn init() -> Result<()> { pub(crate) fn init() -> Result<()> {

View File

@ -18,6 +18,7 @@ mod statics;
use crate::{ use crate::{
cache::CacheState, cache::CacheState,
config::Migration,
error::{Error, Result}, error::{Error, Result},
service::{Bitbucket, FormService, GitHub, Gitlab, Service}, service::{Bitbucket, FormService, GitHub, Gitlab, Service},
statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO}, statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO},
@ -34,7 +35,7 @@ use git2::Repository;
use number_prefix::{NumberPrefix, Prefixed, Standalone}; use number_prefix::{NumberPrefix, Prefixed, Standalone};
use std::{ use std::{
borrow::Cow, borrow::Cow,
fs::create_dir_all, fs::{create_dir_all, read_dir, rename},
path::Path, path::Path,
process::Command, process::Command,
sync::atomic::Ordering, sync::atomic::Ordering,
@ -341,8 +342,7 @@ fn favicon32() -> HttpResponse {
HttpResponse::Ok().content_type("image/png").body(FAVICON) HttpResponse::Ok().content_type("image/png").body(FAVICON)
} }
fn main() -> Result<()> { fn start_server() -> Result<()> {
config::init()?;
let interface = format!("{}:{}", OPT.host, OPT.port); let interface = format!("{}:{}", OPT.host, OPT.port);
let state = Arc::new(State { let state = Arc::new(State {
repos: OPT.outdir.display().to_string(), repos: OPT.outdir.display().to_string(),
@ -371,3 +371,40 @@ fn main() -> Result<()> {
.bind(interface)? .bind(interface)?
.run()?) .run()?)
} }
fn migrate_cache() -> Result<()> {
let mut backup_cache = OPT.cachedir.clone();
backup_cache.set_extension("bak");
rename(&OPT.cachedir, backup_cache)?;
let outdir = OPT.outdir.display().to_string();
let cachedir = OPT.cachedir.display().to_string();
for service in read_dir(&OPT.outdir)? {
let service = service?;
for namespace in read_dir(service.path())? {
let namespace = namespace?;
for repo in read_dir(namespace.path())? {
let repo_path = repo?.path().display().to_string();
let repo_path: String =
repo_path
.split(&outdir)
.fold(String::new(), |mut acc, next| {
acc.push_str(next);
acc
});
println!("{}", repo_path);
hoc(&repo_path, &outdir, &cachedir)?;
}
}
}
Ok(())
}
fn main() -> Result<()> {
config::init()?;
match &OPT.migrate {
None => start_server(),
Some(migration) => match migration {
Migration::CacheCommitCount => migrate_cache(),
},
}
}