-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
121 lines (102 loc) · 2.99 KB
/
output.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
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strings"
)
// By is a less function to define the ordering of the Repository arguments.
type By func(r1, r2 *Repository) bool
// Join the By function and the slice to be sorted.
type repoSorter struct {
repositories []Repository
by func(r1, r2 *Repository) bool // Closure.
}
// Sort is a function for sorting the argument slice.
func (by By) Sort(repositories []Repository) {
rs := &repoSorter{
repositories: repositories,
by: by,
}
sort.Sort(rs)
}
// Len is part of sort.Interface.
func (s *repoSorter) Len() int {
return len(s.repositories)
}
// Swap is part of sort.Interface.
func (s *repoSorter) Swap(i, j int) {
s.repositories[i], s.repositories[j] = s.repositories[j], s.repositories[i]
}
// Less is part of sort.Interface. It is implemented by calling the By closure.
func (s *repoSorter) Less(i, j int) bool {
return s.by(&s.repositories[i], &s.repositories[j])
}
// getMarkdownTemplate reads and returns the contents of the markdown template file.
func getMarkdownTemplate() string {
// Read the template data from the file.
data, err := ioutil.ReadFile("template.md")
check(err)
return string(data)
}
// printRow outputs the contents of each repository statistics to the README.
func outputTable(r []Repository) string {
var table string
var repos, size, authors int
var commits, additions, deletions int64
for _, item := range r {
// Any forked repos that weren't added would have empty allocation slots at the end of the slice, so ignore these in the output.
if item.Name != "" {
table += fmt.Sprintf(
"| %s | %s | %d | %d | %d | %d | %d |\n",
item.Name,
item.Visibility,
item.Size,
item.TotalStats.Commits,
item.TotalStats.Additions,
item.TotalStats.Deletions,
item.TotalStats.Authors)
// Calculate totals.
repos += 1
size += item.Size
commits += item.TotalStats.Commits
additions += item.TotalStats.Additions
deletions += item.TotalStats.Deletions
if authors < item.TotalStats.Authors {
authors = item.TotalStats.Authors
}
}
}
// Add totals to the end of the table.
table += fmt.Sprint("| | | | | | | |\n")
table += fmt.Sprintf(
"| **Totals** | **%d** | **%d** | **%d** | **%d** | **%d** | **%d** |\n",
repos,
size,
commits,
additions,
deletions,
authors)
return table
}
// outputMarkdown sends the repository list to the markdown file.
func outputMarkdown(repositories []Repository) {
// Create the output file.
readme, err := os.Create("README.md")
check(err)
defer readme.Close()
// Add the headers.
template := fmt.Sprint(getMarkdownTemplate())
// Closures to order the Repository structure.
size := func(r1, r2 *Repository) bool {
return r2.Size < r1.Size
}
// Sort the repositories by Size.
By(size).Sort(repositories)
// Print out all of the rows to the README.md
output := strings.Replace(template, "{{ table }}", outputTable(repositories), 1)
_, err = fmt.Fprint(readme, output)
check(err)
check(readme.Sync())
}