-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate publish and publish-flow, add reslug, split in files
- Loading branch information
Geobert Quach
committed
Nov 3, 2019
1 parent
dba2a30
commit c7fe7f2
Showing
7 changed files
with
201 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use std::io; | ||
use std::process::Command; | ||
|
||
use crate::config::Config; | ||
use anyhow::{bail, Result}; | ||
|
||
pub fn update_repo() -> Result<()> { | ||
match Command::new("git").arg("pull").output() { | ||
Ok(output) => { | ||
if !output.status.success() { | ||
bail!( | ||
"issue updating repo: {}\nerr: {}", | ||
String::from_utf8_lossy(&output.stdout), | ||
String::from_utf8_lossy(&output.stderr) | ||
); | ||
} | ||
} | ||
Err(e) => match e.kind() { | ||
io::ErrorKind::NotFound => { | ||
bail!("`git` was not found, please verify the PATH env."); | ||
} | ||
_ => { | ||
bail!("{}", e); | ||
} | ||
}, | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn update_remote(slug: &str, cfg: &Config) -> Result<()> { | ||
let dest_dir = cfg | ||
.publish_dest | ||
.as_ref() | ||
.expect("Should have a value by now") | ||
.to_string_lossy(); | ||
Command::new("git") | ||
.arg("add") | ||
.arg(format!("{}*.md", dest_dir)) | ||
.output()?; | ||
Command::new("git") | ||
.arg("commit") | ||
.arg("-a") | ||
.arg("-m") | ||
.arg(format!("\"published {}.md\"", slug)) | ||
.output()?; | ||
Command::new("git").arg("push").output()?; | ||
Ok(()) | ||
} | ||
|
||
pub fn get_last_log() -> Result<String> { | ||
match Command::new("git") | ||
.arg("log") | ||
.arg("-n") | ||
.arg("1") | ||
.arg("--format=%B") | ||
.output() | ||
{ | ||
Ok(output) => { | ||
if output.status.success() { | ||
Ok(String::from_utf8_lossy(&output.stdout).to_string()) | ||
} else { | ||
bail!("{}", String::from_utf8_lossy(&output.stdout)); | ||
} | ||
} | ||
Err(e) => match e.kind() { | ||
io::ErrorKind::NotFound => { | ||
bail!("`git` was not found, please verify the PATH env."); | ||
} | ||
_ => { | ||
bail!("{}", e); | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::fs; | ||
use std::io::{BufRead, BufReader}; | ||
|
||
use anyhow::{bail, Result}; | ||
use slug::slugify; | ||
|
||
fn _reslug(file_path: &std::path::Path) -> Result<()> { | ||
let file = std::fs::File::open(&file_path)?; | ||
let reader = BufReader::new(&file); | ||
let mut title = String::new(); | ||
for line in reader.lines() { | ||
let line = line.expect("Should have text"); | ||
if line.starts_with("title") { | ||
let start = line.find('\"').expect("Should have starting quote in toml"); | ||
let end = line.rfind('\"').expect("Should have ending quote in toml"); | ||
if start < end { | ||
title = line.clone().drain(start + 1..end).collect(); | ||
} else { | ||
bail!("Corrupted title: {}", line); | ||
} | ||
break; | ||
} | ||
} | ||
let dest_file = file_path.with_file_name(format!("{}.md", slugify(title))); | ||
std::fs::rename(&file_path, &dest_file)?; | ||
println!( | ||
"Renamed `{}` to `{}`", | ||
file_path.display(), | ||
dest_file.display() | ||
); | ||
Ok(()) | ||
} | ||
|
||
pub fn reslug(path: &std::path::Path) -> Result<()> { | ||
if path.is_file() { | ||
_reslug(&path) | ||
} else { | ||
for entry in fs::read_dir(&path)? { | ||
if let Ok(entry) = entry { | ||
let filename = entry.file_name(); | ||
let filename = filename.to_string_lossy(); | ||
if filename.starts_with("-") { | ||
_reslug(&entry.path())?; | ||
} | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.