wire up GitHub calls

This commit is contained in:
Jay Gabriels
2018-03-29 07:05:53 -07:00
parent 01fe97fdfa
commit f9808073fc
5 changed files with 48 additions and 5 deletions

1
.env-sample Normal file
View File

@@ -0,0 +1 @@
GITHUB_TOKEN=

1
.gitignore vendored
View File

@@ -1 +1,2 @@
ghorg
.env

View File

@@ -1,3 +1,5 @@
## Ghorg
# Ghorg
### Access
- Create a personal access token, if the org is behind SSO add it to the token https://help.github.com/articles/authorizing-a-personal-access-token-for-use-with-a-saml-single-sign-on-organization/

View File

@@ -1,9 +1,40 @@
package cmd
import (
"context"
"fmt"
"os"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func Clone(arg string) {
fmt.Println("Cloning Repos", arg)
func authSetup() (*github.Client, *github.RepositoryListByOrgOptions) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opt := &github.RepositoryListByOrgOptions{
Type: "all",
ListOptions: github.ListOptions{PerPage: 1000},
}
return client, opt
}
// CloneAllReposByOrg clones all repos for a given org
func CloneAllReposByOrg() {
client, opt := authSetup()
repos, _, err := client.Repositories.ListByOrg(context.Background(), os.Args[1], opt)
if err != nil {
fmt.Print("Oh no error city: ", err)
}
fmt.Println(repos)
}
// Could clone all repos on a user
// orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil)

12
main.go
View File

@@ -2,12 +2,20 @@ package main
import (
"fmt"
"os"
"log"
"github.com/ghorg/cmd"
"github.com/joho/godotenv"
)
func main() {
fmt.Println("Hello, Ghorg")
cmd.Clone(os.Args[1])
cmd.CloneAllReposByOrg()
}
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
}