Skip to content

Commit

Permalink
Refactor package
Browse files Browse the repository at this point in the history
  • Loading branch information
umutphp committed May 23, 2020
1 parent 636d25b commit cdfeccc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
114 changes: 66 additions & 48 deletions lib/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,53 @@ import (
"github.com/google/go-github/v31/github"
)

func GetRepos(username string) []*github.Repository {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "07a4574f57fdbe125f37afab2264b64a9cde8d82"},
type CLI struct {
username string
token string
client *github.Client
context context.Context
accountTotalChannel chan int
accountUniqueChannel chan int
repoChannel chan *github.Repository
haltChannel chan int
}

func New() CLI {
cli := CLI{
username: "",
token: "07a4574f57fdbe125f37afab2264b64a9cde8d82",
}

cli.context = context.Background()
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: cli.token},
)

tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
tokenClient := oauth2.NewClient(cli.context, tokenSource)

cli.client = github.NewClient(tokenClient)
cli.accountTotalChannel = make(chan int)
cli.accountUniqueChannel = make(chan int)
cli.repoChannel = make(chan *github.Repository)
cli.haltChannel = make(chan int)

return cli
}

func (cli *CLI) Initialize() bool {
user, _, err := cli.client.Users.Get(cli.context, "")
if err != nil {
fmt.Printf("client.Users.Get() faled with '%s'\n", err)
return false
}

cli.username = string(user.GetLogin())
return true
}

func (cli *CLI) GetRepos() []*github.Repository {
// list all repositories for the authenticated user
repos, _, err := client.Repositories.List(ctx, username, nil)
repos, _, err := cli.client.Repositories.List(cli.context, cli.username, nil)

if err != nil {
log.Fatal(err)
Expand All @@ -35,24 +71,10 @@ func GetRepos(username string) []*github.Repository {
return repos
}

func RepoStat(
w io.Writer,
accountTotalChannel chan int,
accountUniqueChannel chan int,
repoChannel chan *github.Repository,
haltChannel chan int) {

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "07a4574f57fdbe125f37afab2264b64a9cde8d82"},
)

tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)

for repo := range repoChannel {
func (cli *CLI) RepoStat(w io.Writer) {
for repo := range cli.repoChannel {
fmt.Print(".")
stats,_,_ := client.Repositories.ListTrafficViews(ctx, "umutphp", repo.GetName(), nil)
stats,_,_ := cli.client.Repositories.ListTrafficViews(cli.context, cli.username, repo.GetName(), nil)

viewCount := 0
uniqueCount := 0
Expand All @@ -64,64 +86,60 @@ func RepoStat(
}

fmt.Fprintf(w, "%s\t%d\t%d\t\n", repo.GetFullName(), viewCount, uniqueCount)
accountTotalChannel <- viewCount
accountUniqueChannel <- uniqueCount
cli.accountTotalChannel <- viewCount
cli.accountUniqueChannel <- uniqueCount
}

haltChannel <- 1
cli.haltChannel <- 1
}

func Execute(w io.Writer) {
repos := GetRepos("umutphp")
func (cli *CLI) Execute(w io.Writer) {
repos := cli.GetRepos()
repoCount := len(repos)
accountTotalChannel := make(chan int, repoCount)
accountUniqueChannel := make(chan int, repoCount)
repoChannel := make(chan *github.Repository)
haltChannel := make(chan int)

cli.accountTotalChannel = make(chan int, repoCount)
cli.accountUniqueChannel = make(chan int, repoCount)

fmt.Fprintf(w, "Repository\tTotal View\tUnique View\t\n")

fmt.Print("Checking repositories ")
for i:=0;i<repoCount;i++ {
go RepoStat(w, accountTotalChannel, accountUniqueChannel, repoChannel, haltChannel)
go cli.RepoStat(w)
}

for _,repo := range repos {
repoChannel <- repo
cli.repoChannel <- repo
}

close(repoChannel)
close(cli.repoChannel)

Finiliaze(w, repoCount, accountTotalChannel, accountUniqueChannel, haltChannel)
cli.Finiliaze(w, repoCount)
}

func Finiliaze(
func (cli *CLI) Finiliaze(
w io.Writer,
repoCount int,
accountTotalChannel chan int,
accountUniqueChannel chan int,
haltChannel chan int) {
repoCount int) {

finishedCount := 0
for finished := range haltChannel {
for finished := range cli.haltChannel {
finishedCount = finishedCount + finished

if finishedCount == repoCount {
close(haltChannel)
close(accountTotalChannel)
close(accountUniqueChannel)
close(cli.haltChannel)
close(cli.accountTotalChannel)
close(cli.accountUniqueChannel)
fmt.Println("")
fmt.Println("")
}
}

total := 0
for t := range accountTotalChannel {
for t := range cli.accountTotalChannel {
total += t
}

unique := 0
for u := range accountUniqueChannel {
for u := range cli.accountUniqueChannel {
unique += u
}

Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
)

func main() {
CLI := cli.New()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
defer w.Flush()

cli.Execute(w)
if CLI.Initialize() == true {
CLI.Execute(w)
}
}

0 comments on commit cdfeccc

Please sign in to comment.