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() {
|
fn main() {
|
||||||
let opts = Opts::from_args();
|
let opts = Opts::from_args();
|
||||||
let comments = Comments::for_user(&opts.redditor);
|
let comments = Comments::for_user(&opts.redditor);
|
||||||
let all = comments
|
|
||||||
.take(opts.fetch)
|
|
||||||
.filter_map(|c| c.ok())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Hello, world! {:?}",
|
"Hello, world! {:?}",
|
||||||
update(
|
update(
|
||||||
&opts.repo.unwrap(),
|
&opts.repo.unwrap(),
|
||||||
&comments.take(opts.fetch).filter_map(|c| c.ok()),
|
comments.take(opts.fetch).filter_map(|c| c.ok()),
|
||||||
&opts.redditor,
|
&opts.redditor,
|
||||||
&("reddit.com/u/".to_owned() + &opts.redditor),
|
&("reddit.com/u/".to_owned() + &opts.redditor),
|
||||||
(opts.threshold, opts.thresholdp)
|
(opts.threshold, opts.thresholdp)
|
||||||
@ -43,7 +39,7 @@ fn main() {
|
|||||||
|
|
||||||
fn update<'a>(
|
fn update<'a>(
|
||||||
repo: &PathBuf,
|
repo: &PathBuf,
|
||||||
current: impl IntoIterator<Item = &'a Comment>,
|
current: impl IntoIterator<Item = Comment>,
|
||||||
redditor: &str,
|
redditor: &str,
|
||||||
email: &str,
|
email: &str,
|
||||||
threshold: (u32, u8),
|
threshold: (u32, u8),
|
||||||
@ -87,14 +83,13 @@ fn update<'a>(
|
|||||||
if delta.len() > 0 {
|
if delta.len() > 0 {
|
||||||
fs_write(&path, to_string_pretty(&comment)?)?;
|
fs_write(&path, to_string_pretty(&comment)?)?;
|
||||||
commit_msg = delta.iter().map(|d| d.to_string()).collect::<Vec<_>>()[..].join("\n");
|
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;
|
updated += 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
create_dir_all((&path).parent().unwrap())?;
|
create_dir_all((&path).parent().unwrap())?;
|
||||||
fs_write(&path, to_string_pretty(&comment)?)?;
|
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();
|
commit_msg = CommentDelta::New.to_string();
|
||||||
updated += 1;
|
updated += 1;
|
||||||
}
|
}
|
||||||
@ -135,7 +130,7 @@ fn update<'a>(
|
|||||||
git.commit(
|
git.commit(
|
||||||
Some("HEAD"),
|
Some("HEAD"),
|
||||||
&sig_backdate,
|
&sig_backdate,
|
||||||
&sig_backdate,
|
&sig,
|
||||||
&commit_msg,
|
&commit_msg,
|
||||||
&tree,
|
&tree,
|
||||||
&[&parent],
|
&[&parent],
|
||||||
|
63
src/model.rs
63
src/model.rs
@ -77,7 +77,7 @@ pub struct ListItem<T: for<'a> Deserialize<'a>> {
|
|||||||
pub struct Comments {
|
pub struct Comments {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
continuation: Option<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 {
|
impl Comments {
|
||||||
@ -126,34 +126,41 @@ impl Iterator for Comments {
|
|||||||
//return Ok((comments.iter().map(|li| li.data).collect(), continuation));
|
//return Ok((comments.iter().map(|li| li.data).collect(), continuation));
|
||||||
}
|
}
|
||||||
|
|
||||||
if let (Some(ref mut buffer), ref mut continuation, ref url) =
|
match (&mut self.buffer, &mut self.continuation, &self.url) {
|
||||||
(&mut self.buffer, &mut self.continuation, &self.url)
|
(Some(Ok(ref mut buffer)), ref mut continuation, ref url) => {
|
||||||
{
|
if let Some(comment) = buffer.next() {
|
||||||
if let Some(comment) = buffer.next() {
|
Some(comment)
|
||||||
Some(comment)
|
} else {
|
||||||
} else {
|
continuation.clone().and_then(|cont| {
|
||||||
continuation.clone().and_then(|cont| {
|
let page = request_paged(&(url.to_string() + "?after=" + &cont[..]));
|
||||||
let page = request_paged(&(url.to_string() + "?after=" + &cont[..]));
|
match page {
|
||||||
match page {
|
Ok((comments, cont)) => {
|
||||||
Ok((comments, cont)) => {
|
**continuation = cont;
|
||||||
**continuation = cont;
|
*buffer = Box::new(comments.into_iter().map(Ok))
|
||||||
*buffer = Box::new(comments.into_iter().map(Ok))
|
}
|
||||||
}
|
Err(e) => *buffer = Box::new(iter::once(Err(e))),
|
||||||
Err(e) => *buffer = Box::new(iter::once(Err(e))),
|
};
|
||||||
};
|
buffer.next()
|
||||||
buffer.next()
|
})
|
||||||
})
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
let page = request_paged(&self.url);
|
|
||||||
match page {
|
|
||||||
Ok((comments, cont)) => {
|
|
||||||
self.continuation = cont;
|
|
||||||
self.buffer = Some(Box::new(comments.into_iter().map(Ok)))
|
|
||||||
}
|
}
|
||||||
Err(e) => self.buffer = Some(Box::new(iter::once(Err(e)))),
|
}
|
||||||
};
|
(None, _, _) => {
|
||||||
self.buffer.as_mut().and_then(|it| it.next())
|
let page = request_paged(&self.url);
|
||||||
|
match page {
|
||||||
|
Ok((comments, cont)) => {
|
||||||
|
self.continuation = cont;
|
||||||
|
self.buffer = Some(Ok(Box::new(comments.into_iter().map(Ok))))
|
||||||
|
}
|
||||||
|
Err(e) => self.buffer = Some(Err(e)),
|
||||||
|
};
|
||||||
|
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)]
|
#[derive(StructOpt, Debug)]
|
||||||
#[structopt(name = "gitredditor")]
|
#[structopt(name = "gitredditor")]
|
||||||
pub struct Opts {
|
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,
|
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,
|
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,
|
pub thresholdp: u8,
|
||||||
|
|
||||||
#[structopt(short = "r", long = "redditor", env = "GITREDDITOR_U")]
|
#[structopt(short = "r", long = "redditor", env = "GITREDDITOR_U")]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user