-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (66 loc) · 1.43 KB
/
main.go
File metadata and controls
79 lines (66 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"time"
"github.com/urfave/cli/v2"
)
type Repo struct {
Name string
Created_at string
Html_url string
Stars int
}
func getUrl(username string) string {
return "https://api.github.com/users/" + username + "/repos"
}
func fetchRepos(url string) {
resp, err := http.Get(url)
if err != nil {
fmt.Fprint(cli.ErrWriter, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
var repos []Repo
err = json.Unmarshal(body, &repos)
if err != nil {
fmt.Fprint(cli.ErrWriter, err)
}
for l := range repos {
t, _ := time.Parse("2006-01-02T15:04:05Z0700",
repos[l].Created_at)
fmt.Printf("Name: %v \n", repos[l].Name)
fmt.Printf("⭐ %v \n", repos[l].Stars)
fmt.Printf("Created At: %v \n", t.Format(time.RFC822))
fmt.Printf("Url: %v \n", repos[l].Html_url)
fmt.Println("______________________________________________")
}
}
func main() {
app := &cli.App{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "GitHub username that you want to fetch data of.",
},
},
Action: func(c *cli.Context) error {
username := c.String("username")
url := getUrl(username)
fetchRepos(url)
return nil
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}