mirror of
https://github.com/shimunn/gitredditor.git
synced 2023-11-17 18:42:43 +01:00
opt
This commit is contained in:
parent
a46aaff8b0
commit
990af9e484
15
src/main.rs
15
src/main.rs
@ -24,16 +24,12 @@ use crate::opts::*;
|
||||
fn main() {
|
||||
let opts = Opts::from_args();
|
||||
let comments = Comments::for_user(&opts.redditor);
|
||||
let all = comments
|
||||
.take(opts.fetch)
|
||||
.filter_map(|c| c.ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
println!(
|
||||
"Hello, world! {:?}",
|
||||
update(
|
||||
&opts.repo.unwrap(),
|
||||
&comments.take(opts.fetch).filter_map(|c| c.ok()),
|
||||
comments.take(opts.fetch).filter_map(|c| c.ok()),
|
||||
&opts.redditor,
|
||||
&("reddit.com/u/".to_owned() + &opts.redditor),
|
||||
(opts.threshold, opts.thresholdp)
|
||||
@ -43,7 +39,7 @@ fn main() {
|
||||
|
||||
fn update<'a>(
|
||||
repo: &PathBuf,
|
||||
current: impl IntoIterator<Item = &'a Comment>,
|
||||
current: impl IntoIterator<Item = Comment>,
|
||||
redditor: &str,
|
||||
email: &str,
|
||||
threshold: (u32, u8),
|
||||
@ -87,14 +83,13 @@ fn update<'a>(
|
||||
if delta.len() > 0 {
|
||||
fs_write(&path, to_string_pretty(&comment)?)?;
|
||||
commit_msg = delta.iter().map(|d| d.to_string()).collect::<Vec<_>>()[..].join("\n");
|
||||
index.update_all(vec![&path], None)?;
|
||||
|
||||
//index.update_all(vec![&path], None)?;
|
||||
updated += 1;
|
||||
}
|
||||
} else {
|
||||
create_dir_all((&path).parent().unwrap())?;
|
||||
fs_write(&path, to_string_pretty(&comment)?)?;
|
||||
index.add_all(vec![&path], git2::IndexAddOption::DEFAULT, None)?;
|
||||
//index.add_all(vec![&path], git2::IndexAddOption::DEFAULT, None)?;
|
||||
commit_msg = CommentDelta::New.to_string();
|
||||
updated += 1;
|
||||
}
|
||||
@ -135,7 +130,7 @@ fn update<'a>(
|
||||
git.commit(
|
||||
Some("HEAD"),
|
||||
&sig_backdate,
|
||||
&sig_backdate,
|
||||
&sig,
|
||||
&commit_msg,
|
||||
&tree,
|
||||
&[&parent],
|
||||
|
23
src/model.rs
23
src/model.rs
@ -77,7 +77,7 @@ pub struct ListItem<T: for<'a> Deserialize<'a>> {
|
||||
pub struct Comments {
|
||||
pub url: String,
|
||||
continuation: Option<String>,
|
||||
buffer: Option<Result<Box<Iterator<Item=Result<Comment, Box<Error>>>>, Box<Error>>>,
|
||||
buffer: Option<Result<Box<Iterator<Item = Result<Comment, Box<Error>>>>, Box<Error>>>,
|
||||
}
|
||||
|
||||
impl Comments {
|
||||
@ -126,9 +126,8 @@ impl Iterator for Comments {
|
||||
//return Ok((comments.iter().map(|li| li.data).collect(), continuation));
|
||||
}
|
||||
|
||||
if let (Some(ref mut buffer), ref mut continuation, ref url) =
|
||||
(&mut self.buffer, &mut self.continuation, &self.url)
|
||||
{
|
||||
match (&mut self.buffer, &mut self.continuation, &self.url) {
|
||||
(Some(Ok(ref mut buffer)), ref mut continuation, ref url) => {
|
||||
if let Some(comment) = buffer.next() {
|
||||
Some(comment)
|
||||
} else {
|
||||
@ -144,16 +143,24 @@ impl Iterator for Comments {
|
||||
buffer.next()
|
||||
})
|
||||
}
|
||||
} else {
|
||||
}
|
||||
(None, _, _) => {
|
||||
let page = request_paged(&self.url);
|
||||
match page {
|
||||
Ok((comments, cont)) => {
|
||||
self.continuation = cont;
|
||||
self.buffer = Some(Box::new(comments.into_iter().map(Ok)))
|
||||
self.buffer = Some(Ok(Box::new(comments.into_iter().map(Ok))))
|
||||
}
|
||||
Err(e) => self.buffer = Some(Box::new(iter::once(Err(e)))),
|
||||
Err(e) => self.buffer = Some(Err(e)),
|
||||
};
|
||||
self.buffer.as_mut().and_then(|it| it.next())
|
||||
let buf = self.buffer.as_mut()?;
|
||||
if let Ok(ref mut buf) = buf {
|
||||
buf.next()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
(Some(Err(_)), _, _) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
21
src/opts.rs
21
src/opts.rs
@ -4,13 +4,28 @@ pub use structopt::StructOpt;
|
||||
#[derive(StructOpt, Debug)]
|
||||
#[structopt(name = "gitredditor")]
|
||||
pub struct Opts {
|
||||
#[structopt(short = "f", long = "fetch", default_value = "20", env = "GITREDDITOR_CNT")]
|
||||
#[structopt(
|
||||
short = "f",
|
||||
long = "fetch",
|
||||
default_value = "20",
|
||||
env = "GITREDDITOR_CNT"
|
||||
)]
|
||||
pub fetch: usize,
|
||||
|
||||
#[structopt(short = "t", long = "threshold", default_value = "5", env = "GITREDDITOR_TH")]
|
||||
#[structopt(
|
||||
short = "t",
|
||||
long = "threshold",
|
||||
default_value = "5",
|
||||
env = "GITREDDITOR_TH"
|
||||
)]
|
||||
pub threshold: u32,
|
||||
|
||||
#[structopt(short = "p", long = "threshold-percent", default_value = "5", env = "GITREDDITOR_THP")]
|
||||
#[structopt(
|
||||
short = "p",
|
||||
long = "threshold-percent",
|
||||
default_value = "5",
|
||||
env = "GITREDDITOR_THP"
|
||||
)]
|
||||
pub thresholdp: u8,
|
||||
|
||||
#[structopt(short = "r", long = "redditor", env = "GITREDDITOR_U")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user