-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoroutine-intro.go
162 lines (142 loc) · 3.31 KB
/
goroutine-intro.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"io"
"math/rand"
"net/http"
"os"
"sync"
"time"
)
func printRepoStats(repoNames []string) {
for _, repoName := range repoNames {
printRepoStat(repoName)
}
}
func printRepoStatsWaitGroup(repoNames []string) {
var wg sync.WaitGroup
for _, repoName := range repoNames {
wg.Add(1)
// new variable necessary. otherwise we just print the stat
// for the first repo
repoName := repoName
go func() {
defer wg.Done()
printRepoStat(repoName)
}()
}
wg.Wait()
}
func printRepoStatsChannel(repoNames []string) {
stats := make(chan readmeStats, 2)
go func() {
var wg sync.WaitGroup
for _, repoName := range repoNames {
wg.Add(1)
go func(repoName string) {
defer wg.Done()
fmt.Println("Checking repository named", repoName)
stat := getRepoStats(repoName)
stats <- stat
}(repoName)
}
wg.Wait()
close(stats)
}()
for stat := range stats {
fmt.Printf("Quality for %v is %v\n", stat.name, determineQuality(stat))
}
}
func main() {
arguments := os.Args[1:]
if len(arguments) == 0 {
return
}
useWaitGroup := arguments[0] == "--waitgroup"
useChannel := arguments[0] == "--channel"
if useWaitGroup {
printRepoStatsWaitGroup(arguments[1:])
} else if useChannel {
printRepoStatsChannel(arguments[1:])
} else {
printRepoStats(arguments)
}
}
func checkRepoFor(repoName string, branchName string, format *format) *readmeStats {
repoUrl := fmt.Sprintf("https://raw.githubusercontent.com/NovatecConsulting/%v/%v/README.%v", repoName, branchName, format.fileEnding)
resp, err := http.Get(repoUrl)
check(err)
if resp.StatusCode == 200 {
var size int64
if resp.ContentLength != -1 {
size = resp.ContentLength
} else {
content := resp.Body
defer content.Close()
bytes, err := io.ReadAll(content)
check(err)
size = int64(len(bytes))
}
stat := readmeStats{repoName, format.name, size, repoUrl}
return &stat
} else {
return nil
}
}
func getRepoStats(repoName string) readmeStats {
delayRandomly()
markdown := format{"markdown", "md"}
asciidoc := format{"asciidoc", "adoc"}
formats := [2]format{markdown, asciidoc}
for _, branchName := range [2]string{"master", "main"} {
for _, format := range formats {
stats := checkRepoFor(repoName, branchName, &format)
if stats != nil {
return *stats
}
}
}
return readmeStats{repoName, "", 0, ""}
}
func delayRandomly() {
rand.Seed(time.Now().UnixNano())
//randomDelay, _ := time.ParseDuration(fmt.Sprintf("%vms", rand.Intn(600 -10) + 10))
randomDelay, _ := time.ParseDuration("0ms")
fmt.Println("We will delay by", randomDelay)
time.Sleep(randomDelay)
}
func printRepoStat(repoName string) {
fmt.Println("Checking repository named " + repoName)
stats := getRepoStats(repoName)
fmt.Printf("Quality for %v is %v\n", stats.name, determineQuality(stats))
}
func determineQuality(stats readmeStats) string {
if stats.size > 3000 {
return "Impressive"
} else if stats.size > 2000 {
return "Fantastic"
} else if stats.size > 1000 {
return "Excellent"
} else if stats.size > 500 {
return "Great"
} else if stats.size > 0 {
return "Good"
} else {
return "Standard"
}
}
type readmeStats struct {
name string
format string
size int64
url string
}
type format struct {
name string
fileEnding string
}
func check(e error) {
if e != nil {
os.Exit(1)
}
}