Skip to content

Commit

Permalink
issue-38: add --disable-sync-date command (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
lowitea authored Jan 13, 2023
1 parent 1c46ace commit a8d83a5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.RU.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/cloner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand All @@ -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
Expand Down
34 changes: 24 additions & 10 deletions src/gitlab/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@ 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<u32>) -> Result<Self> {
pub fn new(
token: &str,
mut url: Url,
opp: Option<u32>,
disable_sync_date: bool,
) -> Result<Self> {
let http = reqwest::Client::new();
let opp = if let Some(opp) = opp { opp } else { 1000 };

let query = format!("access_token={}&per_page={}", token, opp);
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<S: Into<String>>(&self, m: Method, path: S) -> RequestBuilder {
Expand Down Expand Up @@ -109,12 +119,16 @@ impl Client {
Ok(projects)
}

fn make_project_description(new_description: Option<String>) -> String {
format!(
"{} 🦞 Synced: {}",
new_description.unwrap_or_default(),
Utc::now().to_rfc3339()
)
fn make_project_description(&self, new_description: Option<String>) -> 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(
Expand All @@ -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 {
Expand All @@ -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 })
Expand Down

0 comments on commit a8d83a5

Please sign in to comment.