Skip to content

Commit

Permalink
clean up, comments, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tempor1s committed Mar 12, 2020
1 parent 014c111 commit 38e16b3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var uploadCommand = &cobra.Command{
Run: uploadCmd,
}

// uploadCmd will start the upload process
func uploadCmd(cmd *cobra.Command, args []string) {
upload.Start(Token, args)
}
8 changes: 5 additions & 3 deletions download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (

// Start will start the command, handle arguments, and dispatch the correct handler
func Start(token string, args []string) {
// Make sure they pass in a platform and a directory
if len(args) == 0 {
fmt.Println("Please pass the provider that you want to clone from. (github/gitlab) Example: `backup github`")
fmt.Println("Please pass the provider that you want to clone from. (github/gitlab) Example: `backup download github`")
return
} else if len(args) == 1 {
fmt.Println("Please pass in the username that you would like to clone from. Example: `backup github tempor1s`")
fmt.Println("Please pass in the username that you would like to clone from. Example: `backup download github tempor1s`")
return
}

// Warn the user if no personal token was provided
if token == "" {
fmt.Println("WARNING: Personal token was not passed in, only cloning public repos. If you want to clone private repos, please supply a token using --token")
fmt.Println("WARNING: Personal token was not provided, only cloning public repos. If you want to clone private repos, please supply a token using --token")
}

fmt.Printf("Backing up your repos... Please wait - Don't worry if the bar freezes, this could take a few minutes :)\n\n")
Expand Down
18 changes: 12 additions & 6 deletions upload/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func gitHub(token, directory string) {

client := github.NewClient(tc)

// Create a channel to keep all our repos
// Channel and WaitGroup setup because we use concurrency
repoChan := make(chan string)
repoCountChan := make(chan int)
var wg sync.WaitGroup
Expand All @@ -45,34 +45,38 @@ func gitHub(token, directory string) {

// Loop through all repos in the directory directory and upload them all to GitLab as new projects
for repoName := range repoChan {
go uploadGithubRepos(directory, repoName, token, client, &wg, bar, ctx)
go uploadGithubRepos(ctx, directory, repoName, token, client, &wg, bar)
}

wg.Wait()

fmt.Printf("\nUpload Complete - Uploaded %d repos to GitHub.\n", repoCount)
}

func uploadGithubRepos(directory, repoName, token string, client *github.Client, wg *sync.WaitGroup, bar *progressbar.ProgressBar, ctx context.Context) {
func uploadGithubRepos(ctx context.Context, directory, repoName, token string, client *github.Client, wg *sync.WaitGroup, bar *progressbar.ProgressBar) {
// Decrease WaitGroup and increase bar after the repo is uploaded
defer wg.Done()
defer bar.Add(1)

// The path to the repo, exa: tempor1s/gobackup
path := directory + "/" + repoName

// Create the new github repo
repo := createRepo(ctx, client, repoName)

if repo != "" {
createGithubRemotePush(path, token, repo)
}
// Create remote and push to github
createGithubRemotePush(path, token, repo)
}

// createRepo will create a new GitHub repo with the provided repoName
func createRepo(ctx context.Context, client *github.Client, repoName string) string {
isPrivate := false
repoDescription := ""

// Get the currently authenticated user
user, _, err := client.Users.Get(ctx, "")

// Settings for the repo that we are going to create
r := &github.Repository{Name: &repoName, Private: &isPrivate, Description: &repoDescription}

// Delete old repo and then create a new one
Expand All @@ -87,9 +91,11 @@ func createRepo(ctx context.Context, client *github.Client, repoName string) str
log.Fatal(err)
}

// This means the repo already exists, so we just want to find the one that exists
return user.GetHTMLURL() + "/" + repoName
}

// createGithubRemotePush will create a new 'backup' remote with the repo that we just created, and then push the repo at the path to it
func createGithubRemotePush(path, token, repoURL string) {
// Open the github repo at our current path
r, err := git.PlainOpen(path)
Expand Down
3 changes: 2 additions & 1 deletion upload/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func gitLab(token, directory string) {
go uploadGitlabRepos(directory, repoName, token, client, &wg, bar)
}

// Wait for all GoRoutines to be done
wg.Wait()

fmt.Printf("\nUpload Complete\n")
fmt.Printf("\nUpload Complete - Uploaded %d repos to GitLab.\n", repoCount)
}

// uploadGitlabRepos is designed to be a concurrent worker that will upload the current repo
Expand Down
2 changes: 2 additions & 0 deletions upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func Start(token string, args []string) {
return
}

fmt.Printf("Backing up your repos... Please wait - Don't worry if the bar freezes, this could take a few minutes :)\n\n")

// Do different things based off of the platform they want to use
switch args[0] {
case "github":
Expand Down

0 comments on commit 38e16b3

Please sign in to comment.