Skip to content

Instantly share code, notes, and snippets.

@obradovic
Last active September 9, 2020 15:59
Show Gist options
  • Save obradovic/9de30964153876cd4e71 to your computer and use it in GitHub Desktop.
Save obradovic/9de30964153876cd4e71 to your computer and use it in GitHub Desktop.
List Github repos on the command line. Suitable for Unixy pipey things
package main
import (
"flag"
"fmt"
"os"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func main() {
// get the oauth token and org name, either from command line or env variable
authToken := flag.String("auth", os.Getenv("GITHUB_AUTH"), "GitHub OAuth token")
orgName := flag.String("org", os.Getenv("GITHUB_ORG"), "GitHub org")
flag.Parse()
if *authToken == "" {
fmt.Println("\n No Github OAuth token specified. Please use -auth or $GITHUB_AUTH\n")
os.Exit(1)
}
if *orgName == "" {
fmt.Println("\n No Github organization specified. Please use -org or $GITHUB_ORG\n")
os.Exit(1)
}
// create the oauth context
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: *authToken},
)
oAuthContext := oauth2.NewClient(oauth2.NoContext, tokenSource)
// connect to and query github
client := github.NewClient(oAuthContext)
opt := &github.RepositoryListByOrgOptions{ Type: "all" }
var allRepos []github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg(*orgName, opt)
if err != nil {
panic(err)
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
// print the results
for i := range allRepos {
fmt.Println(*allRepos[i].Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment