-
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.
Track git repo if given in config (#114)
* Adds initial git checkout, and task * Adds checkout task
- Loading branch information
1 parent
b6743b7
commit c669f74
Showing
10 changed files
with
344 additions
and
43 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
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 |
---|---|---|
@@ -1,2 +1,25 @@ | ||
pub mod file; | ||
pub mod folder; | ||
pub mod folder; | ||
|
||
/// Set all entries in a dir to permissions().set_readonly(readonly) | ||
pub fn set_dir_readonly(dir: &str, readonly: bool) -> Result<(), std::io::Error> | ||
{ | ||
let paths = std::fs::read_dir(dir.to_owned())?; | ||
|
||
for path in paths | ||
{ | ||
if path.is_err() | ||
{ | ||
return Err(path.err().unwrap()) | ||
} | ||
|
||
let path = path.unwrap(); | ||
|
||
match path.metadata() | ||
{ | ||
Ok(p) => p.permissions().set_readonly(readonly), | ||
Err(e) => return Err(e) | ||
} | ||
} | ||
Ok(()) | ||
} |
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,103 @@ | ||
use std::{path::Path, sync::Arc}; | ||
|
||
use axum::async_trait; | ||
use chrono::{DateTime, Utc}; | ||
use cron::Schedule; | ||
use git2::Repository; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::{config::{Config, CONFIG_PATH}, task::{next_job_time, schedule_from_option, Task}}; | ||
|
||
use super::{clean_and_clone, fast_forward_pull}; | ||
|
||
pub struct GitRefreshTask | ||
{ | ||
pub lock: Arc<Mutex<()>>, | ||
pub last_run: DateTime<Utc>, | ||
pub next_run: Option<DateTime<Utc>>, | ||
pub schedule: Option<Schedule> | ||
} | ||
|
||
impl GitRefreshTask | ||
{ | ||
pub fn new | ||
( | ||
lock: Arc<Mutex<()>>, | ||
schedule: Option<Schedule> | ||
) -> GitRefreshTask | ||
{ | ||
GitRefreshTask | ||
{ | ||
lock, | ||
last_run: chrono::offset::Utc::now(), | ||
next_run: if schedule.is_none() { None } else { next_job_time(schedule.clone().unwrap()) }, | ||
schedule | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Task for GitRefreshTask | ||
{ | ||
async fn run(&mut self) -> Result<(), crate::task::TaskError> | ||
{ | ||
let _ = self.lock.lock().await; | ||
let config = Config::load_or_default(CONFIG_PATH); | ||
if config.git.is_some() | ||
{ | ||
let git = config.git.unwrap(); | ||
let path = Path::new(&config.content.path); | ||
if path.is_dir() | ||
{ | ||
let result = match Repository::open(path) | ||
{ | ||
Ok(repo) => fast_forward_pull(repo, &git.branch), | ||
Err(e) => | ||
{ | ||
crate::debug(format!("{}, {:?} is not a git repo", e, path), None); | ||
match clean_and_clone(&config.content.path, git.clone()) | ||
{ | ||
Ok(repo) => fast_forward_pull(repo, &git.branch), | ||
Err(e) => Err(e) | ||
} | ||
} | ||
}; | ||
|
||
if result.is_err() | ||
{ | ||
crate::debug(format!("{:?}", result.err()), None); | ||
} | ||
} | ||
} | ||
|
||
self.schedule = schedule_from_option(config.stats.save_schedule.clone()); | ||
|
||
self.next_run = match &self.schedule | ||
{ | ||
Some(s) => next_job_time(s.clone()), | ||
None => None | ||
}; | ||
|
||
self.last_run = chrono::offset::Utc::now(); | ||
Ok(()) | ||
} | ||
|
||
fn next(&mut self) -> Option<chrono::prelude::DateTime<chrono::prelude::Utc>> | ||
{ | ||
self.next_run | ||
} | ||
|
||
fn runnable(&self) -> bool | ||
{ | ||
match self.next_run | ||
{ | ||
Some(t) => chrono::offset::Utc::now() > t, | ||
None => false | ||
} | ||
} | ||
|
||
fn info(&self) -> String | ||
{ | ||
"Git refresh".to_string() | ||
} | ||
} |
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
Oops, something went wrong.