Skip to content

Commit

Permalink
fix: org bugs and add my own loader
Browse files Browse the repository at this point in the history
  • Loading branch information
amazingandyyy committed Apr 18, 2022
1 parent 38487b7 commit 2450ac4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ bash <(curl -sL https://raw.githubusercontent.com/amazingandyyy/gofuzzyclone/mai
gofuzzyclone -help
# fastline mode
gofuzzyclone -owner amazingandyyy -search "^go.*" -out ./projects
gofuzzyclone -owner amazingandyyy -search "*-template" -mode wildcard -out ./projects
gofuzzyclone -owner amazingandyyy -search "^go.*" -output ./code
gofuzzyclone -owner amazingandyyy -search "*-template" -mode wildcard -output ./projects
# interactive mode
gofuzzyclone
Expand Down
Binary file modified bin/gofuzzyclone
Binary file not shown.
40 changes: 25 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ import (
"path"
"regexp"
"strings"
"time"

"gofuzzyclone/internal/helper"
"gofuzzyclone/internal/logger"

"github.com/briandowns/spinner"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v43/github"
Expand Down Expand Up @@ -73,13 +71,19 @@ func getOrgRepos(client *github.Client, org string) ([]*github.Repository, error
max := 1000
var allRepos []*github.Repository
opt := &github.RepositoryListByOrgOptions{
Sort: "updated",
Sort: "pushed",
ListOptions: github.ListOptions{
PerPage: 100,
PerPage: 80,
},
}
counter := 0
dotsCounter := 0
dots := []string{".", ".", ".", "."}
for {
fmt.Printf("\rScanning %d repositories %s", counter, strings.Join(dots[:dotsCounter%3+1], ""))
dotsCounter++
repos, resp, err := client.Repositories.ListByOrg(context.Background(), org, opt)
counter += len(repos)
if len(allRepos) > max {
allRepos = allRepos[:max]
break
Expand All @@ -105,13 +109,20 @@ func getPersonalRepos(client *github.Client, owner string) ([]*github.Repository
max := 1000
var allRepos []*github.Repository
opt := &github.RepositoryListOptions{
Sort: "updated",
Sort: "pushed",
Visibility: "all",
ListOptions: github.ListOptions{
PerPage: 100,
PerPage: 60,
},
}
counter := 0
dotsCounter := 0
dots := []string{".", ".", ".", ".", "."}
for {
fmt.Printf("\rScanning %d repositories %s", counter, strings.Join(dots[:dotsCounter%4], " "))
dotsCounter++
repos, resp, err := client.Repositories.List(context.Background(), owner, opt)
counter += len(repos)
if len(allRepos) > max {
allRepos = allRepos[:max]
break
Expand All @@ -120,7 +131,6 @@ func getPersonalRepos(client *github.Client, owner string) ([]*github.Repository
if len(repos) == 0 {
break
}

opt.Page = resp.NextPage
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
Expand All @@ -131,7 +141,7 @@ func getPersonalRepos(client *github.Client, owner string) ([]*github.Repository
}

func getRepos(client *github.Client, userType, owner string) ([]*github.Repository, error) {
if userType == "org" {
if userType == "Organization" {
return getOrgRepos(client, owner)
} else {
return getPersonalRepos(client, owner)
Expand All @@ -157,7 +167,7 @@ func wildCardToRegexp(pattern string) string {

func main() {
var setup = flag.Bool("setup", false, "renew github token")
var dest = flag.String("output", "", "output to which sdirectory")
var dest = flag.String("output", "", "output to which directory")
var search = flag.String("search", "", "search patern")
var owner = flag.String("owner", "", "github user/org")
var mode = flag.String("mode", "regex", "matching mechanism")
Expand Down Expand Up @@ -192,15 +202,15 @@ func main() {
fmt.Scanf("%s", owner)
}

s := spinner.New(spinner.CharSets[4], 200*time.Millisecond) // Build our new spinner
s.Prefix = fmt.Sprintf("Searching %q ", *search)
s.Start() // Start the spinner
// s := spinner.New(spinner.CharSets[4], 200*time.Millisecond) // Build our new spinner
// s.Prefix = fmt.Sprintf("Searching %q ", *search)
// s.Start() // Start the spinner

ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: ghToken})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
user, _, _ := client.Users.Get(ctx, "")
user, _, _ := client.Users.Get(ctx, *owner)

all_repos, _ := getRepos(client, *user.Type, *owner)
all_repos_matched := make([]*github.Repository, 0)
Expand All @@ -217,12 +227,12 @@ func main() {
}
}
fmt.Println("")
s.Stop()
// s.Stop()
for i, repo := range all_repos_matched {
fmt.Printf("%d %v\n", i+1, repo.GetFullName())
}
confirm := "n"
logger.Println("green", fmt.Sprintf("Result: %d repos match %q", len(all_repos_matched), *search))
logger.Println("green", fmt.Sprintf("Result: %d repos match %q in %v", len(all_repos_matched), *search, *owner))
if len(all_repos_matched) == 0 {
os.Exit(0)
}
Expand Down

0 comments on commit 2450ac4

Please sign in to comment.