package main import ( "encoding/json" "fmt" "log" "net/http" "os" "time" ) type Repo struct { Id int `json:"id"` Name string `json:"name"` Url string `json:"url"` } type Payload struct { Ref string `json:"ref"` RefType string `json:"ref_type"` FullRef string `json:"full_ref"` MasterBranch string `json:"master_branch"` Desc string `json:"description"` PusherType string `json:"pusher_type"` Size int `json:"size"` Action string `json:"action"` } type Objects struct { UserId string `json:"id"` Type string `json:"type"` Repo Repo `json:"repo"` Payload Payload `json:"payload"` Public bool `json:"public"` CreatedAt string `json:"created_at"` } type Output struct { Name string Key string Count int } func indexObjects(table []Objects) []Output { var res []Output data := make(map[string]string) data_count := make(map[string]int) for _, j := range table { data[j.Repo.Name] = j.Type data_count[j.Type]++ } for repoName, repoType := range data { count := data_count[repoType] res = append(res, Output{Name: repoName, Key: repoType, Count: count}) } return res } func findKeyOutput(output []Output, targetKey string) *Output { for i := range output { if output[i].Key == targetKey { return &output[i] } } return nil } func main() { var user string args := os.Args[1:] if len(args) > 0 { user = args[0] } client := &http.Client{Timeout: 10 * time.Second} resp, err := client.Get("https://api.github.com/users/" + user + "/events") if err != nil { panic(err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { panic(fmt.Errorf("%s", "err: statuscode failure: "+resp.Status)) } var obj []Objects if err := json.NewDecoder(resp.Body).Decode(&obj); err != nil { log.Fatalf("Failed to decode json: %v", err) } for _, j := range obj { switch j.Type { case "PushEvent": fmt.Printf("- Pushed %d commit(s) to %s\n", j.Payload.Size, j.Repo.Name) case "IssuesEvent": fmt.Printf("- %s an issue in %s\n", j.Payload.Action, j.Repo.Name) case "WatchEvent": fmt.Printf("- Starred %s\n", j.Repo.Name) case "ForkEvent": fmt.Printf("- Forked %s\n", j.Repo.Name) case "CreateEvent": fmt.Printf("- Created %s %s in %s\n", j.Payload.RefType, j.Payload.Ref, j.Repo.Name) case "PullRequestEvent": fmt.Printf("- %s a pull request in %s\n", j.Payload.Action, j.Repo.Name) default: fmt.Printf("- %s in %s\n", j.Type, j.Repo.Name) } } }