Skip to content

Commit

Permalink
add cli bump to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jan 1, 2025
1 parent 3726562 commit d4a5693
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 50 deletions.
55 changes: 54 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "git-mover"
version = "0.1.1"
version = "1.0.0"
edition = "2021"
keywords = ["utility", "git", "cli"]
categories = ["command-line-utilities"]
Expand All @@ -11,6 +11,7 @@ readme = "./README.md"
repository = "https://github.com/Its-Just-Nans/git-mover"

[dependencies]
clap = { version = "4.5.23", features = ["derive"] }
dotenv = "0.15.0"
env_logger = "0.11.5"
git2 = "0.19.0"
Expand Down
34 changes: 34 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::path::PathBuf;

use clap::Parser;
use serde::Deserialize;

use crate::{config::Config, utils::main_sync};

#[derive(Parser, Deserialize, Default, Clone, Debug)]
pub struct GitMoverCli {
#[arg(short, long, visible_alias = "from")]
pub source: Option<String>,
#[arg(short, long, visible_alias = "to")]
pub destination: Option<String>,
#[arg(short, long = "no-forks")]
pub no_forks: bool,
#[arg(short, long)]
pub config: Option<String>,
#[arg(short, long, action = clap::ArgAction::Count)]
pub verbose: u8,
}

pub async fn cli_main() {
let args = GitMoverCli::parse();
let mut config = match &args.config {
Some(path_str) => {
let path = PathBuf::from(path_str);
Config::new_from_path(&path)
}
None => Config::new(),
}
.set_debug(args.verbose)
.with_cli_args(args);
main_sync(&mut config).await;
}
13 changes: 8 additions & 5 deletions src/codeberg/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Platform for CodebergPlatform {
let url = format!("https://{}/api/v1/user/repos", CODEBERG_URL);
let mut page: usize = 1;
let limit = 100;
let mut repos = Vec::new();
let mut all_repos = Vec::new();
loop {
let request = client
.get(&url)
Expand All @@ -183,17 +183,20 @@ impl Platform for CodebergPlatform {

let response = request.await?;
if !response.status().is_success() {
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos));
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos).with_text(&text));
}
let mut page_repos: Vec<Repo> = response.json().await?;
let text = response.text().await?;
let repos: Vec<CodebergRepo> = serde_json::from_str(&text)?;
let mut page_repos: Vec<Repo> = repos.into_iter().map(|r| r.into()).collect();
if page_repos.is_empty() {
break;
}
println!("Requested codeberg (page {}): {}", page, page_repos.len());
repos.append(&mut page_repos);
all_repos.append(&mut page_repos);
page += 1;
}
Ok(repos)
Ok(all_repos)
})
}

Expand Down
15 changes: 13 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::{
use home::home_dir;
use serde::{Deserialize, Serialize};

use crate::{codeberg::CodebergConfig, github::GithubConfig, gitlab::GitlabConfig};
use crate::{
cli::GitMoverCli, codeberg::CodebergConfig, github::GithubConfig, gitlab::GitlabConfig,
};

#[derive(Deserialize, Default, Clone, Debug)]
pub struct Config {
Expand All @@ -19,6 +21,8 @@ pub struct Config {

/// actual configuration data
pub config_data: ConfigData,

pub cli_args: Option<GitMoverCli>,
}

#[derive(Deserialize, Serialize, Default, Clone, Debug)]
Expand All @@ -36,6 +40,7 @@ impl Config {
Config {
debug: 0,
config_path: path_config,
cli_args: None,
config_data: match toml::from_str(str_config) {
Ok(config) => config,
Err(e) => {
Expand All @@ -47,6 +52,11 @@ impl Config {
}
}

pub fn with_cli_args(mut self, cli_args: GitMoverCli) -> Self {
self.cli_args = Some(cli_args);
self
}

/// Create a new Config object from the default path
pub fn new() -> Config {
let config_path = Config::get_config_path();
Expand All @@ -71,8 +81,9 @@ impl Config {
}

/// Set the debug value
pub fn set_debug(&mut self, value: u8) {
pub fn set_debug(mut self, value: u8) -> Self {
self.debug = value;
self
}

/// Get the path to the config file
Expand Down
8 changes: 5 additions & 3 deletions src/github/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
utils::{Platform, Repo},
};
use reqwest::{
header::{ACCEPT, AUTHORIZATION},
header::{ACCEPT, AUTHORIZATION, USER_AGENT},
Client,
};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -74,11 +74,13 @@ impl Platform for GithubPlatform {
])
.header(AUTHORIZATION, format!("Bearer {}", token))
.header(ACCEPT, "application/vnd.github+json")
.header(USER_AGENT, "reqwest")
.header("X-GitHub-Api-Version", "2022-11-28")
.send();
let response = request.await?;
if !response.status().is_success() {
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos));
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos).with_text(&text));
}
let text = response.text().await?;
let repos: Vec<RepoGithub> = serde_json::from_str(&text)?;
Expand All @@ -96,7 +98,7 @@ impl Platform for GithubPlatform {

fn delete_repo(
&self,
name: &str,
_name: &str,
) -> Pin<Box<dyn std::future::Future<Output = Result<(), GitMoverError>> + Send + '_>> {
unimplemented!("GitlabConfig::delete_repo");
Box::pin(async { Err(GitMoverError::new(GitMoverErrorKind::Unimplemented)) })
Expand Down
3 changes: 2 additions & 1 deletion src/gitlab/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ impl Platform for GitlabPlatform {

let response = request.await?;
if !response.status().is_success() {
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos));
let text = response.text().await?;
return Err(GitMoverError::new(GitMoverErrorKind::GetAllRepos).with_text(&text));
}
let text = response.text().await?;
let repos: Vec<GitlabRepo> = match serde_json::from_str(&text) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub(crate) mod cli;
pub(crate) mod config;
pub(crate) mod errors;
pub(crate) mod macros;
Expand All @@ -10,4 +11,4 @@ mod codeberg;
mod github;
mod gitlab;

pub use utils::{cli_main, PlatformType};
pub use cli::cli_main;
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async fn main() {
env!("CARGO_PKG_VERSION")
));
setup();
cli_main(None).await;
cli_main().await;
}

#[cfg(test)]
Expand All @@ -26,6 +26,6 @@ mod tests {
#[tokio::test]
async fn test_main() {
setup();
cli_main(None).await;
cli_main().await;
}
}
6 changes: 0 additions & 6 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ pub(crate) async fn sync_repos(
let mut set = JoinSet::new();

let mut private_repos = vec![];
let mut fork_repos = vec![];
for one_repo in repos {
if one_repo.fork {
fork_repos.push(one_repo);
continue;
}
if one_repo.private {
private_repos.push(one_repo);
continue;
Expand All @@ -46,7 +41,6 @@ pub(crate) async fn sync_repos(
}
});
}
println!("Skipping {} forked repos", fork_repos.len());
let temp_folder_priv = temp_folder.clone();
set.spawn(async move {
match sync_private_repos(
Expand Down
Loading

0 comments on commit d4a5693

Please sign in to comment.