github can list an indefinite amount of a user's repositories
This commit is contained in:
parent
261a6fcae2
commit
1a5502a48f
1 changed files with 79 additions and 10 deletions
|
@ -1,9 +1,11 @@
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
@ -15,7 +17,10 @@ var githubCmd = &cobra.Command{
|
||||||
Short: "Backup your Github account",
|
Short: "Backup your Github account",
|
||||||
Long: `Backup your Github account to local storage`,
|
Long: `Backup your Github account to local storage`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
githubList()
|
err := ghList()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,24 +37,88 @@ func init() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func githubList() {
|
type repo struct {
|
||||||
if Verbose {
|
FullName string `json:"full_name"`
|
||||||
fmt.Println("Listing", C.Green("Github"), "Repos for", C.Magenta(viper.Get("github.user")))
|
CloneHTTPS string `json:"clone_url"`
|
||||||
|
UpdatedDate string `json:"updated_at"`
|
||||||
|
CreatedDate string `json:"created_at"`
|
||||||
|
ArchiveURL string `json:"archive_url"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func ghQuery(url string) ([]byte, string, error) {
|
||||||
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
githubUrl := "https://api.github.com/users/" + viper.GetString("github.user") + "/repos"
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", githubUrl, nil)
|
|
||||||
|
|
||||||
req.Header.Add("Authorization", "Bearer "+viper.GetString("github.token"))
|
req.Header.Add("Authorization", "Bearer "+viper.GetString("github.token"))
|
||||||
|
req.Header.Add("Accept", "application/vnd.github.v3+json")
|
||||||
|
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(C.Red("[ERROR]"), err)
|
return nil, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, _ := ioutil.ReadAll(resp.Body)
|
body, _ := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Printf(string([]byte(body)))
|
|
||||||
|
next := ""
|
||||||
|
//links := strings.Split(resp.Header["Link"], ",")
|
||||||
|
for _, l := range strings.Split(resp.Header["Link"][0], ",") {
|
||||||
|
if strings.Contains(l, `rel="next"`) {
|
||||||
|
str := strings.Split(l, ";")[0]
|
||||||
|
next = strings.Trim(str, " <>")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return body, next, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ghGetRepos() ([]repo, error) {
|
||||||
|
next := "https://api.github.com/user/repos"
|
||||||
|
var repos []repo
|
||||||
|
|
||||||
|
for {
|
||||||
|
var body []byte
|
||||||
|
var err error
|
||||||
|
body, next, err = ghQuery(next)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var temp []repo
|
||||||
|
err = json.Unmarshal(body, &temp)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
repos = append(repos, temp...)
|
||||||
|
if next == "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return repos, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ghList() error {
|
||||||
|
if Verbose {
|
||||||
|
fmt.Println("Listing", C.Green("Github"), "Repos for", C.Magenta(viper.Get("github.user")))
|
||||||
|
}
|
||||||
|
|
||||||
|
repos, err := ghGetRepos()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(viper.GetString("github.user"), "has", len(repos), "repositories.")
|
||||||
|
|
||||||
|
for _, r := range repos {
|
||||||
|
|
||||||
|
fmt.Println(r.FullName)
|
||||||
|
fmt.Println("\tUpdated:", r.UpdatedDate)
|
||||||
|
fmt.Println("\tCreated:", r.CreatedDate)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Reference in a new issue