Skip to content

Commit

Permalink
refactor backup command into submodule to clean up cmd namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tempor1s committed Mar 10, 2020
1 parent fe1596e commit 1444616
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 95 deletions.
99 changes: 99 additions & 0 deletions backup/backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package backup

import (
"context"
"fmt"
"log"
"os"
"path"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

func GitHub(token string, args []string) {
if len(args) == 0 {
fmt.Println("Please enter a URL for a GitHub user. Example: `gobackup backup github.com/tempor1s`")
return
}

if token == "" {
// TODO: Support non-access token based request
fmt.Println("WARNING: Personal token was not passed in. Please pass in a token using --token - Support for NON-token download coming soon.")
return
}

// Get the URL to clone
repoURL := args[0]

// Get the users github name for the directory
dirName := path.Base(repoURL)

repos := getRepos(token)

// Clone all repos
cloneRepos(repos, dirName, token)
}

func getRepos(token string) []string {

// Set up OAuth token stuff
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)

tc := oauth2.NewClient(ctx, ts)

// Create a new github client using the OAuth2 token
client := github.NewClient(tc)

// Get all repos that the user owns
opt := &github.RepositoryListOptions{
Affiliation: "owner",
ListOptions: github.ListOptions{
PerPage: 100000,
},
}

repos, _, err := client.Repositories.List(ctx, "", opt)

if err != nil {
log.Fatal(err)
}

var ret []string
for _, repo := range repos {
ret = append(ret, *repo.HTMLURL)
}

return ret
}

func cloneRepos(repos []string, dirName, token string) {
// Create username dir to put all cloned repos in.
err := os.MkdirAll(dirName, os.ModePerm)

if err != nil {
log.Fatal(err)
}

for _, repo := range repos {
fmt.Printf("[gobackup] cloning %s\n", repo)
repoName := path.Base(repo)
_, err := git.PlainClone(dirName+"/"+repoName, false, &git.CloneOptions{
Auth: &http.BasicAuth{
Username: "gobackup",
Password: token,
},
URL: repo,
Progress: os.Stdout,
})

if err != nil {
log.Fatal(err)
}
}
}
99 changes: 4 additions & 95 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
"path"

"github.com/google/go-github/github"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
"github.com/tempor1s/gobackup/backup"
)

func init() {
Expand All @@ -26,91 +17,9 @@ var backupCommand = &cobra.Command{
Short: "Backup all of the repos at a given github URL",
Long: "This command will backup all of the repos from a user into your CURRENT directory.",
// Run the backup command
Run: backup,
Run: backupCmd,
}

// TODO: Put this into another file as to not muddy up the cmd package that should only be used for managing commands
func backup(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("Please enter a URL for a GitHub user. Example: `gobackup backup github.com/tempor1s`")
return
}

// Get the URL to clone
repoURL := args[0]

// Get the users github name for the directory
dirName := path.Base(repoURL)

repos := getRepos()

// Clone all repos
cloneRepos(repos, dirName)
}

func getRepos() []string {
if Token == "" {
// TODO: Support non-access token based request
fmt.Println("WARNING: Personal Token was not passed in. Please pass in a token using --token - Support for NON-Token download coming soon.")
return []string{}
}

// Set up OAuth Token stuff
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: Token},
)

tc := oauth2.NewClient(ctx, ts)

// Create a new github client using the OAuth2 token
client := github.NewClient(tc)

// Get all repos that the user owns
opt := &github.RepositoryListOptions{
Affiliation: "owner",
ListOptions: github.ListOptions{
PerPage: 100000,
},
}

repos, _, err := client.Repositories.List(ctx, "", opt)

if err != nil {
log.Fatal(err)
}

var ret []string
for _, repo := range repos {
ret = append(ret, *repo.HTMLURL)
}

return ret
}

func cloneRepos(repos []string, dirName string) {

// Create username dir to put all cloned repos in.
err := os.MkdirAll(dirName, os.ModePerm)

if err != nil {
log.Fatal(err)
}

for _, repo := range repos {
fmt.Printf("[gobackup] cloning %s\n", repo)
repoName := path.Base(repo)
_, err := git.PlainClone(dirName+"/"+repoName, false, &git.CloneOptions{
Auth: &http.BasicAuth{
Username: "gobackup",
Password: Token,
},
URL: repo,
Progress: os.Stdout,
})

if err != nil {
log.Fatal(err)
}
}
func backupCmd(cmd *cobra.Command, args []string) {
backup.GitHub(Token, args)
}

0 comments on commit 1444616

Please sign in to comment.