From a8d83a5883cf83003e10bc28ac7db41465e1aff4 Mon Sep 17 00:00:00 2001 From: lowitea Date: Sat, 14 Jan 2023 00:25:29 +0300 Subject: [PATCH] issue-38: add --disable-sync-date command (#41) --- README.RU.md | 1 + README.md | 1 + src/cli.rs | 5 +++++ src/cloner.rs | 5 +++-- src/gitlab/client.rs | 34 ++++++++++++++++++++++++---------- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.RU.md b/README.RU.md index 88eda79..98d4194 100644 --- a/README.RU.md +++ b/README.RU.md @@ -109,6 +109,7 @@ Options: --disable-hierarchy Disable saving the directory hierarchy [env: GTLBSTR_DISABLE_HIERARCHY=] --clear-dst Clear dst path before cloning [env: GTLBSTR_CLEAR_DST=] --only-master Download only default branch [env: GTLBSTR_ONLY_MASTER=] + --disable-sync-date Disable adding sync dates in project descriptions [env: GTLBSTR_DISABLE_SYNC_DATE=] -h, --help Print help information -V, --version Print version information ``` diff --git a/README.md b/README.md index 0a4933e..496030f 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Options: --disable-hierarchy Disable saving the directory hierarchy [env: GTLBSTR_DISABLE_HIERARCHY=] --clear-dst Clear dst path before cloning [env: GTLBSTR_CLEAR_DST=] --only-master Download only default branch [env: GTLBSTR_ONLY_MASTER=] + --disable-sync-date Disable adding sync dates in project descriptions [env: GTLBSTR_DISABLE_SYNC_DATE=] -h, --help Print help information -V, --version Print version information ``` diff --git a/src/cli.rs b/src/cli.rs index d91c599..fb77657 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -128,6 +128,10 @@ struct Cli { /// Download only default branch #[arg(long, env = "GTLBSTR_ONLY_MASTER")] only_master: bool, + + /// Disable adding sync dates in project descriptions + #[arg(long, env = "GTLBSTR_DISABLE_SYNC_DATE")] + disable_sync_date: bool, } pub fn run() -> Result<()> { @@ -174,6 +178,7 @@ pub fn run() -> Result<()> { disable_hierarchy: cli.disable_hierarchy, clear_dst: cli.clear_dst, only_master: cli.only_master, + disable_sync_date: cli.disable_sync_date, }; clone(clone_params) diff --git a/src/cloner.rs b/src/cloner.rs index 52b72ed..2c57653 100644 --- a/src/cloner.rs +++ b/src/cloner.rs @@ -167,11 +167,12 @@ pub struct CloneParams { pub disable_hierarchy: bool, pub clear_dst: bool, pub only_master: bool, + pub disable_sync_date: bool, } #[tokio::main] pub async fn clone(p: CloneParams) -> Result<()> { - let fetch_gl = gitlab::Client::new(&p.fetch.token, p.fetch.url, p.objects_per_page)?; + let fetch_gl = gitlab::Client::new(&p.fetch.token, p.fetch.url, p.objects_per_page, true)?; let mut projects = fetch_gl .get_projects(p.only_owned, p.only_membership) .await?; @@ -191,7 +192,7 @@ pub async fn clone(p: CloneParams) -> Result<()> { } let backup_data = if let Some(backup) = p.backup { - let client = gitlab::Client::new(&backup.token, backup.url, None)?; + let client = gitlab::Client::new(&backup.token, backup.url, None, p.disable_sync_date)?; let group = client.get_group(backup.group).await?; let git_http_auth = if p.upload_ssh { None diff --git a/src/gitlab/client.rs b/src/gitlab/client.rs index 3c40400..08be3a2 100644 --- a/src/gitlab/client.rs +++ b/src/gitlab/client.rs @@ -10,10 +10,16 @@ const API_VERSION: &str = "v4"; pub struct Client { url: Url, http: reqwest::Client, + disable_sync_date: bool, } impl Client { - pub fn new(token: &str, mut url: Url, opp: Option) -> Result { + pub fn new( + token: &str, + mut url: Url, + opp: Option, + disable_sync_date: bool, + ) -> Result { let http = reqwest::Client::new(); let opp = if let Some(opp) = opp { opp } else { 1000 }; @@ -21,7 +27,11 @@ impl Client { url.set_path(&format!("api/{}", API_VERSION)); url.set_query(Some(&query)); - Ok(Client { url, http }) + Ok(Client { + url, + http, + disable_sync_date, + }) } fn build_request>(&self, m: Method, path: S) -> RequestBuilder { @@ -109,12 +119,16 @@ impl Client { Ok(projects) } - fn make_project_description(new_description: Option) -> String { - format!( - "{} 🦞 Synced: {}", - new_description.unwrap_or_default(), - Utc::now().to_rfc3339() - ) + fn make_project_description(&self, new_description: Option) -> String { + if self.disable_sync_date { + new_description.unwrap_or_default() + } else { + format!( + "{} 🦞 Synced: {}", + new_description.unwrap_or_default(), + Utc::now().to_rfc3339() + ) + } } pub async fn make_project( @@ -133,7 +147,7 @@ impl Client { let path = name.clone(); let namespace_id = group_id; - let description = Client::make_project_description(info.description.clone()); + let description = self.make_project_description(info.description.clone()); self.build_request(Method::POST, "projects") .json(&MakeProjectRequest { @@ -159,7 +173,7 @@ impl Client { description: String, } - let description = Client::make_project_description(info.description.clone()); + let description = self.make_project_description(info.description.clone()); self.build_request(Method::PUT, format!("projects/{}", project.id)) .json(&UpdateProjectRequest { description })