-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tobifroe/cobra
Move to Cobra
- Loading branch information
Showing
8 changed files
with
179 additions
and
117 deletions.
There are no files selected for viewing
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "starscraper", | ||
Short: "Github Stargazer data getter.", | ||
Long: `Starscraper is a simple application that returns public information for the stargazers of a given Github repo.`, | ||
} | ||
|
||
func Execute() { | ||
err := rootCmd.Execute() | ||
if err != nil { | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func init() { | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"github.com/tobifroe/starscraper/scrape" | ||
) | ||
|
||
// scrapeCmd represents the scrape command | ||
var scrapeCmd = &cobra.Command{ | ||
Use: "scrape", | ||
Short: "Scrapes stargazer data", | ||
Long: `A longer description that spans multiple lines and likely contains examples | ||
and usage of using your command. For example: | ||
Cobra is a CLI library for Go that empowers applications. | ||
This application is a tool to generate the needed files | ||
to quickly create a Cobra application.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
owner := cmd.Flag("owner").Value.String() | ||
repo := cmd.Flag("repo").Value.String() | ||
token := cmd.Flag("token").Value.String() | ||
output := cmd.Flag("output").Value.String() | ||
verbose, _ := cmd.Flags().GetBool("verbose") | ||
scrape.Scrape(token, repo, owner, output, verbose) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(scrapeCmd) | ||
|
||
scrapeCmd.Flags().String("repo", "", "Repository to scrape") | ||
scrapeCmd.Flags().String("owner", "", "Repository owner") | ||
scrapeCmd.Flags().String("token", "", "Github PAT") | ||
scrapeCmd.Flags().String("output", "output.csv", "Output file") | ||
scrapeCmd.Flags().BoolP("verbose", "v", false, "Verbose output") | ||
|
||
scrapeCmd.MarkFlagRequired("repo") | ||
scrapeCmd.MarkFlagRequired("owner") | ||
|
||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,7 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/joho/godotenv" | ||
"github.com/shurcooL/githubv4" | ||
"github.com/tobifroe/starscraper/types" | ||
"github.com/tobifroe/starscraper/util" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var query struct { | ||
Repository struct { | ||
Description string | ||
Stargazers struct { | ||
TotalCount int | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
Edges []struct { | ||
Node struct { | ||
Email string | ||
Name string | ||
Login string | ||
} | ||
} | ||
} `graphql:"stargazers(first: 100, after: $cursor)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
import "github.com/tobifroe/starscraper/cmd" | ||
|
||
func main() { | ||
|
||
// init Flags | ||
tokenFlag := flag.String("token", "", "Github Token") | ||
repoFlag := flag.String("repo", "", "Github Repo") | ||
ownerFlag := flag.String("owner", "", "Github Repo Owner") | ||
outputFlag := flag.String("output", "output.csv", "Output file name") | ||
debugFlag := flag.Bool("debug", false, "Verbose output for debugging") | ||
flag.Parse() | ||
|
||
err := godotenv.Load() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"repo": githubv4.String(*repoFlag), | ||
"owner": githubv4.String(*ownerFlag), | ||
"cursor": (*githubv4.String)(nil), // Null after argument to get first page. | ||
} | ||
|
||
if *tokenFlag == "" { | ||
*tokenFlag = os.Getenv("GH_TOKEN") | ||
} | ||
|
||
if *repoFlag == "" { | ||
fmt.Println("Please specify a repository.") | ||
return | ||
} | ||
|
||
if *ownerFlag == "" { | ||
fmt.Println("Please specify a repository owner.") | ||
return | ||
} | ||
|
||
if *tokenFlag != "" { | ||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: *tokenFlag}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
|
||
client := githubv4.NewClient(tc) | ||
|
||
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) | ||
fmt.Println("Getting stargazers...") | ||
s.Start() | ||
|
||
var allUsers []types.User | ||
for { | ||
err := client.Query(ctx, &query, variables) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
for _, v := range query.Repository.Stargazers.Edges { | ||
if v.Node.Email != "" { | ||
allUsers = append(allUsers, types.User{ | ||
Email: v.Node.Email, | ||
Name: v.Node.Name, | ||
Login: v.Node.Login, | ||
}) | ||
} | ||
if v.Node.Email != "" && *debugFlag { | ||
fmt.Printf("%s (%s) - %s\n", v.Node.Name, v.Node.Login, v.Node.Email) | ||
} | ||
} | ||
if !query.Repository.Stargazers.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["cursor"] = githubv4.NewString(query.Repository.Stargazers.PageInfo.EndCursor) | ||
} | ||
|
||
util.WriteToCSV(allUsers, *outputFlag) | ||
s.Stop() | ||
fmt.Println("Success.") | ||
fmt.Printf("Wrote stargazer data to %s \n", *outputFlag) | ||
|
||
} else { | ||
fmt.Println("No Github token supplied. Either pass the -token flag, set up a .env file or set the GH_TOKEN environment variable.") | ||
} | ||
cmd.Execute() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package scrape | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/briandowns/spinner" | ||
"github.com/joho/godotenv" | ||
"github.com/shurcooL/githubv4" | ||
"github.com/tobifroe/starscraper/types" | ||
"github.com/tobifroe/starscraper/util" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
var query struct { | ||
Repository struct { | ||
Description string | ||
Stargazers struct { | ||
TotalCount int | ||
PageInfo struct { | ||
EndCursor githubv4.String | ||
HasNextPage bool | ||
} | ||
Edges []struct { | ||
Node struct { | ||
Email string | ||
Name string | ||
Login string | ||
} | ||
} | ||
} `graphql:"stargazers(first: 100, after: $cursor)"` | ||
} `graphql:"repository(owner: $owner, name: $repo)"` | ||
} | ||
|
||
func Scrape(token string, repo string, owner string, output string, verbose bool) { | ||
|
||
err := godotenv.Load() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
variables := map[string]interface{}{ | ||
"repo": githubv4.String(repo), | ||
"owner": githubv4.String(owner), | ||
"cursor": (*githubv4.String)(nil), // Null after argument to get first page. | ||
} | ||
|
||
if token == "" { | ||
token = os.Getenv("GH_TOKEN") | ||
} | ||
|
||
if token != "" { | ||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
|
||
client := githubv4.NewClient(tc) | ||
|
||
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond) | ||
fmt.Println("Getting stargazers...") | ||
s.Start() | ||
|
||
var allUsers []types.User | ||
for { | ||
err := client.Query(ctx, &query, variables) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
for _, v := range query.Repository.Stargazers.Edges { | ||
if v.Node.Email != "" { | ||
allUsers = append(allUsers, types.User{ | ||
Email: v.Node.Email, | ||
Name: v.Node.Name, | ||
Login: v.Node.Login, | ||
}) | ||
} | ||
if v.Node.Email != "" && verbose { | ||
fmt.Printf("%s (%s) - %s\n", v.Node.Name, v.Node.Login, v.Node.Email) | ||
} | ||
} | ||
if !query.Repository.Stargazers.PageInfo.HasNextPage { | ||
break | ||
} | ||
variables["cursor"] = githubv4.NewString(query.Repository.Stargazers.PageInfo.EndCursor) | ||
} | ||
|
||
util.WriteToCSV(allUsers, output) | ||
s.Stop() | ||
fmt.Println("Success.") | ||
fmt.Printf("Wrote stargazer data to %s \n", output) | ||
|
||
} else { | ||
fmt.Println("No Github token supplied. Either pass the -token flag, set up a .env file or set the GH_TOKEN environment variable.") | ||
} | ||
} |