|
| 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