Skip to content

Commit

Permalink
issue-36: make --bg flag optional (#44)
Browse files Browse the repository at this point in the history
* issue-36: make --bg flag optional

* issue-36: add error if only one upload flag is specified

* issue-36: add error if only --bg flag is specified
  • Loading branch information
lowitea authored Jan 18, 2023
1 parent 6d302fa commit 07e4964
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
16 changes: 14 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,24 @@ pub fn run() -> Result<()> {
cli.include.map(FilterPatterns::Include)
};

let backup_gl = if let (Some(url), Some(token), Some(group)) = (cli.bu, cli.bt, cli.bg) {
Some(BackupGitlabOptions::new(url, token, group)?)
let upl_err = "For upload to another gitlab, you must specify both the --bt and --bu flags";
let backup_gl = if let (Some(url), Some(token)) = (&cli.bu, &cli.bt) {
Some(BackupGitlabOptions::new(
url.clone(),
token.clone(),
cli.bg.clone(),
)?)
} else {
if cli.bu.is_some() || cli.bt.is_some() {
bail!(upl_err);
};
None
};

if backup_gl.is_none() && cli.bg.is_some() {
bail!(upl_err);
}

let clone_params = CloneParams {
fetch: fetch_gl,
dst: cli.dst,
Expand Down
23 changes: 14 additions & 9 deletions src/cloner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ impl FetchGitlabOptions {
pub struct BackupGitlabOptions {
url: Url,
token: String,
group: String,
group: Option<String>,
}

impl BackupGitlabOptions {
pub fn new(url: String, token: String, group: String) -> Result<Self> {
pub fn new(url: String, token: String, group: Option<String>) -> Result<Self> {
let url = Url::parse(&url)?;
Ok(Self { url, token, group })
}
}

struct BackupData {
client: gitlab::Client,
group: types::Group,
group: Option<types::Group>,
git_http_auth: Option<String>,
}

Expand Down Expand Up @@ -193,7 +193,11 @@ 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, p.disable_sync_date)?;
let group = client.get_group(backup.group).await?;
let group = if let Some(gr) = backup.group {
Some(client.get_group(gr).await?)
} else {
None
};
let git_http_auth = if p.upload_ssh {
None
} else {
Expand All @@ -217,11 +221,12 @@ pub async fn clone(p: CloneParams) -> Result<()> {

if p.dry_run {
if let Some(backup_data) = &backup_data {
let g = &backup_data.group;
println!(
"Backup group: {} (id: {}, path: {})",
g.name, g.id, g.full_path
);
if let Some(g) = backup_data.group.as_ref() {
println!(
"Backup group: {} (id: {}, path: {})",
g.name, g.id, g.full_path
)
};
}
println!("Local out dir: {}", &dst);
println!();
Expand Down
34 changes: 25 additions & 9 deletions src/gitlab/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ impl Client {
pub async fn make_subgroup(
&self,
name: String,
parent_id: types::GroupId,
parent_id: Option<types::GroupId>,
) -> reqwest::Result<types::Group> {
#[derive(Serialize)]
struct MakeGroupRequest {
name: String,
path: String,
parent_id: types::GroupId,
parent_id: Option<types::GroupId>,
}

let path = name.clone();
Expand All @@ -227,25 +227,32 @@ impl Client {
pub async fn make_project_with_namespace(
&self,
mut path: Vec<String>,
root_group: &types::Group,
root_group: &Option<types::Group>,
project_info: &types::Project,
) -> reqwest::Result<types::Project> {
let mut parent_id = root_group.id;
let mut parent_id = root_group.as_ref().map(|gr| gr.id);

// TODO: remove unwrap
let project_name = path.pop().unwrap();

let mut current_namespace = root_group.full_path.clone();
let mut current_namespace = root_group
.as_ref()
.map(|gr| gr.full_path.clone())
.unwrap_or_default();

for group_name in path {
current_namespace = format!("{}/{}", current_namespace, group_name);
current_namespace = if current_namespace.is_empty() {
group_name.clone()
} else {
format!("{}/{}", current_namespace, group_name)
};
let group = if let Some(group) = self.group_exist(current_namespace.clone()).await? {
group
} else {
self.make_subgroup(group_name, parent_id).await?
};

parent_id = group.id;
parent_id = Some(group.id);
}

match self
Expand All @@ -254,8 +261,17 @@ impl Client {
{
Some(p) => self.update_project(&p, project_info).await,
None => {
self.make_project(project_name, parent_id, project_info)
.await
self.make_project(
project_name,
parent_id.unwrap_or_else(|| {
panic!(
"Parent group for project {} not found",
&project_info.name_with_namespace
)
}),
project_info,
)
.await
}
}
}
Expand Down

0 comments on commit 07e4964

Please sign in to comment.