Update
This commit is contained in:
parent
584c07ff23
commit
53e144d9a7
24 changed files with 1236 additions and 254 deletions
|
@ -9,6 +9,7 @@ use anyhow::{bail, Result};
|
|||
use chrono::prelude::*;
|
||||
use clap::{Parser, Subcommand};
|
||||
use diesel::prelude::*;
|
||||
use r2d2_redis::redis;
|
||||
use scan_dir::ScanDir;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
|
@ -27,6 +28,9 @@ struct Cli {
|
|||
enum Commands {
|
||||
#[clap(about = "Imports new posts")]
|
||||
Import,
|
||||
|
||||
#[clap(about = "Clears redis cache")]
|
||||
Clear,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
|
@ -50,88 +54,137 @@ struct Post {
|
|||
fn main() -> Result<()> {
|
||||
match Cli::parse().commands {
|
||||
Commands::Import => {
|
||||
let conn = &mut db::establish_connection()?;
|
||||
let db_conn = &mut db::establish_connection()?;
|
||||
let redis_conn = &mut cache::establish_connection()?;
|
||||
|
||||
for post in ScanDir::dirs().read("/blog", |iter| {
|
||||
iter.map(|(entry, _)| {
|
||||
let path = entry.path().join("post.md");
|
||||
let src = fs::read_to_string(&path)?;
|
||||
let frontmatter = match fronma::parser::parse::<PostFrontmatter>(&src) {
|
||||
Ok(x) => x,
|
||||
Err(x) => bail!("Error parsing frontmatter: {:?}", x),
|
||||
};
|
||||
let posts = ScanDir::dirs()
|
||||
.read("/blog", |iter| {
|
||||
iter.map(|(entry, _)| {
|
||||
let path = entry.path().join("post.md");
|
||||
let src = fs::read_to_string(&path)?;
|
||||
let frontmatter = match fronma::parser::parse::<PostFrontmatter>(&src) {
|
||||
Ok(x) => x,
|
||||
Err(x) => bail!("Error parsing frontmatter: {:?}", x),
|
||||
};
|
||||
|
||||
Ok(Post {
|
||||
path,
|
||||
frontmatter: frontmatter.headers,
|
||||
content: frontmatter.body.to_string(),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})?? {
|
||||
let content = post.content.trim();
|
||||
|
||||
if let Some(id) = post.frontmatter.id {
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&post.frontmatter.name),
|
||||
slug: Some(&post.frontmatter.slug),
|
||||
description: Some(&post.frontmatter.description),
|
||||
content: Some(content),
|
||||
published_at: Some(post.frontmatter.published_at),
|
||||
edited_at: Some(post.frontmatter.edited_at),
|
||||
active: Some(post.frontmatter.active),
|
||||
Ok(Post {
|
||||
path,
|
||||
frontmatter: frontmatter.headers,
|
||||
content: frontmatter.body.to_string(),
|
||||
})
|
||||
.execute(conn)?;
|
||||
} else {
|
||||
let id = if let Some(id) = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&post.frontmatter.slug))
|
||||
.first::<i32>(conn)
|
||||
.optional()?
|
||||
{
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()
|
||||
})??
|
||||
.into_iter()
|
||||
.map(|post| -> Result<_> {
|
||||
let trimmed = PostFrontmatter {
|
||||
id: post.frontmatter.id,
|
||||
name: post.frontmatter.name.trim().to_string(),
|
||||
slug: post.frontmatter.slug.trim().to_string(),
|
||||
description: post.frontmatter.description.trim().to_string(),
|
||||
published_at: post.frontmatter.published_at,
|
||||
edited_at: post.frontmatter.edited_at,
|
||||
active: post.frontmatter.active,
|
||||
};
|
||||
let content = post.content.trim();
|
||||
|
||||
if let Some(id) = trimmed.id {
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&post.frontmatter.name),
|
||||
slug: None,
|
||||
description: Some(&post.frontmatter.description),
|
||||
name: Some(&trimmed.name),
|
||||
slug: Some(&trimmed.slug),
|
||||
description: Some(&trimmed.description),
|
||||
content: Some(content),
|
||||
published_at: Some(post.frontmatter.published_at),
|
||||
edited_at: Some(post.frontmatter.edited_at),
|
||||
active: Some(post.frontmatter.active),
|
||||
published_at: Some(trimmed.published_at),
|
||||
edited_at: Some(trimmed.edited_at),
|
||||
active: Some(trimmed.active),
|
||||
})
|
||||
.execute(conn)?;
|
||||
.execute(db_conn)?;
|
||||
|
||||
id
|
||||
Ok(id)
|
||||
} else {
|
||||
diesel::insert_into(db::schema::posts::table)
|
||||
.values(db::models::NewPost {
|
||||
name: &post.frontmatter.name,
|
||||
slug: &post.frontmatter.slug,
|
||||
description: &post.frontmatter.description,
|
||||
content: content,
|
||||
published_at: post.frontmatter.published_at,
|
||||
edited_at: post.frontmatter.edited_at,
|
||||
active: post.frontmatter.active,
|
||||
})
|
||||
.returning(db::schema::posts::id)
|
||||
.get_result::<i32>(conn)?
|
||||
};
|
||||
let id = if let Some(id) = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.filter(db::schema::posts::slug.eq(&trimmed.slug))
|
||||
.first::<i32>(db_conn)
|
||||
.optional()?
|
||||
{
|
||||
diesel::update(db::schema::posts::table)
|
||||
.filter(db::schema::posts::id.eq(id))
|
||||
.set(db::models::UpdatePost {
|
||||
name: Some(&trimmed.name),
|
||||
slug: None,
|
||||
description: Some(&trimmed.description),
|
||||
content: Some(content),
|
||||
published_at: Some(trimmed.published_at),
|
||||
edited_at: Some(trimmed.edited_at),
|
||||
active: Some(trimmed.active),
|
||||
})
|
||||
.execute(db_conn)?;
|
||||
|
||||
fs::write(
|
||||
post.path,
|
||||
format!(
|
||||
"---\n{}---\n{}",
|
||||
serde_yaml::to_string(&PostFrontmatter {
|
||||
id: Some(id),
|
||||
..post.frontmatter
|
||||
})?,
|
||||
post.content
|
||||
),
|
||||
)?;
|
||||
}
|
||||
id
|
||||
} else {
|
||||
diesel::insert_into(db::schema::posts::table)
|
||||
.values(db::models::NewPost {
|
||||
name: &trimmed.name,
|
||||
slug: &trimmed.slug,
|
||||
description: &trimmed.description,
|
||||
content: content,
|
||||
published_at: trimmed.published_at,
|
||||
edited_at: trimmed.edited_at,
|
||||
active: trimmed.active,
|
||||
})
|
||||
.returning(db::schema::posts::id)
|
||||
.get_result::<i32>(db_conn)?
|
||||
};
|
||||
|
||||
fs::write(
|
||||
post.path,
|
||||
format!(
|
||||
"---\n{}---\n\n{}\n",
|
||||
serde_yaml::to_string(&PostFrontmatter {
|
||||
id: Some(id),
|
||||
..trimmed
|
||||
})?,
|
||||
content
|
||||
),
|
||||
)?;
|
||||
|
||||
Ok(id)
|
||||
}
|
||||
})
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let ids = db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.load::<i32>(db_conn)?;
|
||||
|
||||
diesel::delete(
|
||||
db::schema::posts::table
|
||||
.filter(diesel::dsl::not(db::schema::posts::id.eq_any(posts))),
|
||||
)
|
||||
.execute(db_conn)?;
|
||||
|
||||
for id in ids {
|
||||
redis::cmd("DEL")
|
||||
.arg(cache::keys::post_content(id))
|
||||
.query::<()>(redis_conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Commands::Clear => {
|
||||
let db_conn = &mut db::establish_connection()?;
|
||||
let redis_conn = &mut cache::establish_connection()?;
|
||||
|
||||
for id in db::schema::posts::table
|
||||
.select(db::schema::posts::id)
|
||||
.load::<i32>(db_conn)?
|
||||
{
|
||||
redis::cmd("DEL")
|
||||
.arg(cache::keys::post_content(id))
|
||||
.query::<()>(redis_conn)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue