Skip to content

Commit

Permalink
Merge pull request #11 from matt-dray/protocol
Browse files Browse the repository at this point in the history
Add protocol arg to allow for SSH, close #10
  • Loading branch information
matt-dray committed Jul 18, 2020
2 parents 703bc43 + 3d9d325 commit 047fcf5
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 36 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
@@ -1,6 +1,6 @@
Package: ghdump
Title: Clone Or Download All Repos For A GitHub User
Version: 0.0.0.9005
Version: 0.0.0.9006
Authors@R:
person(given = "Matt",
family = "Dray",
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
# ghdump 0.0.0.9006

* Added protocol argument for user to choose HTTPS or SSH (#10)

# ghdump 0.0.0.9005

* Altered system call when cloning to work on Windows (#9)
Expand Down
47 changes: 39 additions & 8 deletions R/03-clone-repos.R
Expand Up @@ -4,21 +4,47 @@
#'
#' Clone a repo for a specified user to a specified local repository.
#'
#' If you're using \code{protocol = "ssh"}, you need to make sure you have
#' \href{https://happygitwithr.com/ssh-keys.html}{set up your SSH keys}.
#'
#' @param gh_user Character string. A GitHub user name.
#' @param repo Character string. A GitHub repo name for the named \code{gh_user}.
#' @param protocol Character string. Either \code{"https"} or \code{"ssh"}.
#' @param dest_dir Character string. A local file path where the zipped
#' repositories will be downloaded to. Must be a full path.
#'
#' @details If you're using \code{protocol = "ssh"}, you need to make sure
#' you've \href{https://happygitwithr.com/ssh-keys.html}{set up your SSH keys}.
#'
#' @return The named user's named repo cloned to the specified location.
ghd_clone_one <- function(gh_user, repo, dest_dir) {
ghd_clone_one <- function(gh_user, repo, protocol, dest_dir) {

if (!protocol %in% c("https", "ssh")) {
stop("You must provide either 'https' or 'ssh' to the protocol argument.")
}

# Pass a system call to clone the repo to the desination
system(
paste0(
"git clone https://github.com/", gh_user, "/", repo, ".git ",
dest_dir, "/", repo
# Pass a system call to clone the repo to the destination
# Different

if (protocol == "https") {

system(
paste0(
"git clone https://github.com/", gh_user, "/", repo, ".git ",
dest_dir, "/", repo
)
)
)

} else if (protocol == "ssh") {

system(
paste0(
"git clone git@github.com:", gh_user, "/", repo, ".git ",
dest_dir, "/", repo
)
)

}

}

Expand All @@ -29,11 +55,15 @@ ghd_clone_one <- function(gh_user, repo, dest_dir) {
#'
#' @param gh_user Character string. A GitHub user name.
#' @param names_vec Character vector. Repo names for the given \code{gh_user}.
#' @param protocol Character string. Either \code{"https"} or \code{"ssh"}.
#' @param dest_dir Character string. A local file path where the zipped repositories
#' will be downloaded to. Must be a full path.
#'
#' @details If you're using \code{protocol = "ssh"}, you need to make sure
#' you've \href{https://happygitwithr.com/ssh-keys.html}{set up your SSH keys}.
#'
#' @return The named user's named repos cloned to the specified location.
ghd_clone_multi <- function(gh_user, names_vec, dest_dir) {
ghd_clone_multi <- function(gh_user, names_vec, protocol, dest_dir) {

# Ask if all the repos should be downloaded
q_clone_all <- readline(
Expand All @@ -51,6 +81,7 @@ ghd_clone_multi <- function(gh_user, names_vec, dest_dir) {
~ ghd_clone_one(
gh_user = gh_user,
repo = .x,
protocol = protocol,
dest_dir = dest_dir
)
)
Expand Down
37 changes: 29 additions & 8 deletions R/04-copy-repos.R
Expand Up @@ -8,11 +8,13 @@
#' zipped repositories will be downloaded to. Must be a full path.
#' @param copy_type Character string. Specify whether to \code{"download"}
#' or \code{"clone"} the repos to your local machine.
#' @param protocol Character string. Either \code{"https"} or \code{"ssh"}. Only
#' required if \code{copy_type = "clone"}.
#'
#' @details Make sure you've got a GitHub account and have generated a GitHub
#' PAT token and stored it your .Renviron. See
#' \href{https://happygitwithr.com/github-pat.html}{Happy Git and GitHub for the UseR}
#' for more information.
#' @details Make sure you've got a GitHub account and have
#' \href{https://happygitwithr.com/github-pat.html}{generated a GitHub PAT}
#' and stored it your .Renviron. If you're using \code{protocol = "ssh"},
#' you need to make sure you have \href{https://happygitwithr.com/ssh-keys.html}{set up your SSH keys}.
#'
#' @return GitHub repositories either (a) cloned or (b) downloaded in the
#' specified local directory.
Expand All @@ -23,11 +25,18 @@
#' \dontrun{
#' ghd_copy(
#' gh_user = "matt-dray",
#' dest_dir = "~/Documents/repos",
#' copy_type = "clone"
#' dest_dir = "~/Documents/repos-cloned",
#' copy_type = "clone",
#' protocol = "https"
#' )
#'
#' ghd_copy(
#' gh_user = "matt-dray",
#' dest_dir = "~/Documents/repos-downloaded",
#' copy_type = "download"
#' )
#' }
ghd_copy <- function(gh_user, dest_dir, copy_type = c("clone", "download")) {
ghd_copy <- function(gh_user, dest_dir, copy_type, protocol = NULL) {

if (is.character(gh_user) == FALSE) {
stop("Argument gh_user must be a character string that's a GitHub user.\n")
Expand All @@ -41,6 +50,18 @@ ghd_copy <- function(gh_user, dest_dir, copy_type = c("clone", "download")) {
stop("Argument copy_type must be 'clone' or 'download'.\n")
}

if (copy_type == "clone" & is.null(protocol)) {
stop("You must provide either 'https' or 'ssh' to the protocol argument.")
}

if (copy_type == "clone" & !protocol %in% c("https", "ssh")) {
stop("You must provide either 'https' or 'ssh' to the protocol argument.")
}

if (copy_type == "download" & !is.null(protocol)) {
warning("You don't need the protocol argument for downloads. Did you mean to clone instead?")
}

# Get repo info for the user
gh_response <- ghd_get_repos(gh_user)

Expand Down Expand Up @@ -77,7 +98,7 @@ ghd_copy <- function(gh_user, dest_dir, copy_type = c("clone", "download")) {

if (copy_type == "clone") { # if cloning

ghd_clone_multi(gh_user, names_vec, dest_dir)
ghd_clone_multi(gh_user, names_vec, protocol, dest_dir)

cat("Finished cloning\n")

Expand Down
35 changes: 25 additions & 10 deletions README.md
Expand Up @@ -18,34 +18,49 @@ Learn more about this package from [an associated blog post](https://www.rostrum

Note that the package is under development, may not work in all environments and is not fully tested. Use at own risk.

### Install

You can install the developer version of {ghdump} from GitHub with:

``` r
remotes::install_github("matt-dray/ghdump")
```

### GitHub PAT

You'll need a GitHub Personal Access Token to use {ghdump}.

Assuming you have a GitHub account, generate a token for accessing the GitHub API and store this in your .Renviron file. The {usethis} package helps make this a breeze. Read more in the [Happy Git and GitHub for the useR](https://happygitwithr.com/github-pat.html) book by Jenny Bryan, the STAT 545 TAs and Jim Hester.

```
```
usethis::browse_github_pat() # opens browser to generate token
usethis::edit_r_environ() # add your token to the .Renviron
```

Make sure to restart R after these steps.

### Install
### Use `ghd_copy()`

You can install the developer version of {ghdump} from GitHub with:
{ghdump} has one exported function: `ghd_copy()`. Pass to the function a GitHub user name, a local directory to download into and whether you want to download or clone the repos. If you want to clone, you must [specify the protocol](https://docs.github.com/en/github/using-git/which-remote-url-should-i-use) (make sure [your keys are set up](https://happygitwithr.com/ssh-keys.html) if specifying SSH).

To clone:

``` r
remotes::install_github("matt-dray/ghdump")
ghdump::ghd_copy(
gh_user = "matt-dray", # user whose repos to download
dest_dir = "~/Documents/repos", # where to copy to
copy_type = "clone", # 'clone' or 'download' the repos
protocol = "https" # 'https' or 'ssh'
)
```

### Use `ghd_copy()`

{ghdump} has one exported function: `ghd_copy()`. Pass to the function a GitHub user name, a local directory to download into and whether you want to download or clone the repos.
To download:

``` r
ghdump::ghd_copy(
gh_user = "matt-dray", # user whose repos to download
dest_dir = "~/Documents/repos", # where to copy to
copy_type = "clone" # "clone" or "download" the repos
gh_user = "matt-dray",
dest_dir = "~/Documents/repos",
copy_type = "download"
)
```

Expand Down
8 changes: 7 additions & 1 deletion man/ghd_clone_multi.Rd

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

11 changes: 10 additions & 1 deletion man/ghd_clone_one.Rd

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

24 changes: 17 additions & 7 deletions man/ghd_copy.Rd

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

0 comments on commit 047fcf5

Please sign in to comment.