Created
June 5, 2017 19:55
-
-
Save barthr/c01019efcc3eac773775a278d1ffd361 to your computer and use it in GitHub Desktop.
Backup all your github repositories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"golang.org/x/oauth2" | |
"github.com/google/go-github/github" | |
) | |
func main() { | |
token := flag.String("token", "", "acces-token to access github") | |
flag.Parse() | |
var tc *http.Client | |
if *token != "" { | |
ctx := context.Background() | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: *token}, | |
) | |
tc = oauth2.NewClient(ctx, ts) | |
} | |
client := github.NewClient(tc) | |
repositories, _, err := client.Repositories.List("", &github.RepositoryListOptions{ | |
Visibility: "all", | |
Affiliation: "owner", | |
}) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
for _, repo := range repositories { | |
downloadURL := fmt.Sprintf("%s/archive/master.zip", *repo.HTMLURL) | |
resp, err := http.Get(downloadURL) | |
if err != nil { | |
log.Fatalf("Error when downloading %s => %v", downloadURL, err) | |
continue | |
} | |
f, err := os.Create(fmt.Sprintf("%s.zip", *repo.Name)) | |
if err != nil { | |
panic(err) | |
} | |
io.Copy(f, resp.Body) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment