Skip to content

Commit 230a5e2

Browse files
init: created the github email-detail finder
1 parent 0925fea commit 230a5e2

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
*.out
1313

1414
# Dependency directories (remove the comment below to include it)
15-
# vendor/
15+
vendor/

go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/loki
2+
3+
go 1.19
4+
5+
require (
6+
github.com/google/go-github/v50 v50.1.0
7+
github.com/spf13/cobra v1.6.1
8+
)
9+
10+
require (
11+
github.com/golang/protobuf v1.3.2 // indirect
12+
github.com/google/go-querystring v1.1.0 // indirect
13+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
14+
github.com/spf13/pflag v1.0.5 // indirect
15+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
16+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
17+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be // indirect
18+
google.golang.org/appengine v1.6.7 // indirect
19+
)

go.sum

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
3+
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
4+
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
7+
github.com/google/go-github/v50 v50.1.0 h1:hMUpkZjklC5GJ+c3GquSqOP/T4BNsB7XohaPhtMOzRk=
8+
github.com/google/go-github/v50 v50.1.0/go.mod h1:Ev4Tre8QoKiolvbpOSG3FIi4Mlon3S2Nt9W5JYqKiwA=
9+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
10+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
11+
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
12+
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
13+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
14+
github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA=
15+
github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY=
16+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
17+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
18+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
19+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
20+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
21+
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
22+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
23+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
24+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be h1:vEDujvNQGv4jgYKudGeI/+DAX4Jffq6hpD55MmoEvKs=
25+
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
26+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
27+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
28+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
29+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
30+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
31+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
32+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
33+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
34+
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
35+
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
36+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
37+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"sync"
8+
9+
"github.com/google/go-github/v50/github"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var user string
14+
var threads int
15+
16+
func main() {
17+
rootCmd := &cobra.Command{
18+
Use: "github",
19+
Short: "A command-line tool to retrieve GitHub user and repository details",
20+
}
21+
22+
userCmd := &cobra.Command{
23+
Use: "user",
24+
Short: "Retrieve user details",
25+
RunE: getUser,
26+
}
27+
28+
userCmd.Flags().StringVarP(&user, "user", "u", "", "GitHub username")
29+
userCmd.Flags().IntVarP(&threads, "threads", "t", 1, "Number of threads to use")
30+
31+
repoCmd := &cobra.Command{
32+
Use: "repo",
33+
Short: "Retrieve repository details",
34+
RunE: getRepos,
35+
}
36+
37+
repoCmd.Flags().StringVarP(&user, "user", "u", "", "GitHub username")
38+
repoCmd.Flags().IntVarP(&threads, "threads", "t", 1, "Number of threads to use")
39+
40+
orgCmd := &cobra.Command{
41+
Use: "org",
42+
Short: "Retrieve organization details",
43+
RunE: getOrgs,
44+
}
45+
46+
orgCmd.Flags().StringVarP(&user, "user", "u", "", "GitHub username")
47+
orgCmd.Flags().IntVarP(&threads, "threads", "t", 1, "Number of threads to use")
48+
49+
50+
rootCmd.AddCommand(userCmd, repoCmd, orgCmd)
51+
52+
if err := rootCmd.Execute(); err != nil {
53+
log.Fatal(err)
54+
}
55+
}
56+
57+
func getClient() *github.Client {
58+
return github.NewClient(nil)
59+
}
60+
61+
func getUser(cmd *cobra.Command, args []string) error {
62+
if user == "" {
63+
return fmt.Errorf("user flag is required")
64+
}
65+
66+
client := getClient()
67+
68+
u, _, err := client.Users.Get(context.Background(), user)
69+
if err != nil {
70+
return err
71+
}
72+
73+
fmt.Println("Name:", u.GetName())
74+
fmt.Println("Email:", u.GetEmail())
75+
fmt.Println("Company:", u.GetCompany())
76+
fmt.Println("Location:", u.GetLocation())
77+
fmt.Println("Bio:", u.GetBio())
78+
fmt.Println("Followers:", u.GetFollowers())
79+
fmt.Println("Following:", u.GetFollowing())
80+
fmt.Println("Created:", u.GetCreatedAt())
81+
fmt.Println("Updated:", u.GetUpdatedAt())
82+
return nil
83+
}
84+
85+
func getRepos(cmd *cobra.Command, args []string) error {
86+
if user == "" {
87+
return fmt.Errorf("user flag is required")
88+
}
89+
90+
client := getClient()
91+
92+
opt := &github.RepositoryListOptions{Type: "owner", Sort: "updated", Direction: "desc"}
93+
repos, _, err := client.Repositories.List(context.Background(), user, opt)
94+
if err != nil {
95+
return err
96+
}
97+
98+
wg := &sync.WaitGroup{}
99+
for i := 0; i < threads; i++ {
100+
wg.Add(1)
101+
go func(start, end int) {
102+
defer wg.Done()
103+
for j := start; j < end; j++ {
104+
r := repos[j]
105+
fmt.Println("Name:", r.GetName())
106+
fmt.Println("Description:", r.GetDescription())
107+
fmt.Println("Language:", r.GetLanguage())
108+
fmt.Println("Stars:", r.GetStargazersCount())
109+
fmt.Println("Forks:", r.GetForksCount())
110+
fmt.Println("Updated:", r.GetUpdatedAt())
111+
fmt.Println()
112+
}
113+
}(i*len(repos)/threads, (i+1)*len(repos)/threads)
114+
}
115+
wg.Wait()
116+
117+
return nil
118+
}
119+
120+
func getOrgs(cmd *cobra.Command, args []string) error {
121+
122+
if user == "" {
123+
return fmt.Errorf("user flag is required")
124+
}
125+
126+
client := getClient()
127+
128+
opt := &github.ListOptions{PerPage: 10}
129+
orgs, _, err := client.Organizations.List(context.Background(), user, opt)
130+
if err != nil {
131+
return err
132+
}
133+
134+
wg := &sync.WaitGroup{}
135+
for i := 0; i < threads; i++ {
136+
wg.Add(1)
137+
go func(start, end int) {
138+
defer wg.Done()
139+
for j := start; j < end; j++ {
140+
o := orgs[j]
141+
fmt.Println("Name:", o.GetName())
142+
fmt.Println("Description:", o.GetDescription())
143+
fmt.Println("Location:", o.GetLocation())
144+
fmt.Println()
145+
}
146+
}(i*len(orgs)/threads, (i+1)*len(orgs)/threads)
147+
}
148+
wg.Wait()
149+
150+
151+
return nil
152+
}

0 commit comments

Comments
 (0)