-
Notifications
You must be signed in to change notification settings - Fork 1
/
files.go
125 lines (117 loc) · 3.5 KB
/
files.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
// Copyright 2018 Fedir RYKHTIK. All rights reserved.
// Use of this source code is governed by the GNU GPL 3.0
// license that can be found in the LICENSE file.
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
)
func clearHTTPCacheFolder(tmpFolderPath string, dryRun bool) error {
d, err := os.Open(tmpFolderPath)
if err != nil {
log.Fatalf("Could not open %s", tmpFolderPath)
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
log.Fatalf("Could not read from %s", tmpFolderPath)
}
for _, name := range names {
fp := filepath.Join(tmpFolderPath, name)
if dryRun {
fmt.Printf("Deleted %s\n", fp)
} else {
err = os.RemoveAll(fp)
if err != nil {
log.Fatalf("Could not remove %s", fp)
}
fmt.Printf("Deleted %s\n", fp)
}
}
return nil
}
func writeCSVStatistics(ghData []Repository, csvFilePath string) {
var csvData [][]string
csvData = append(csvData, headersFromStructTags())
for _, r := range ghData {
csvData = append(csvData, formatRepositoryDataForCSV(r))
}
writeCsv(csvFilePath, csvData)
}
func formatRepositoryDataForCSV(r Repository) []string {
ghProjectData := []string{
r.Name,
fmt.Sprintf("%s", r.URL),
fmt.Sprintf("%s", r.Author),
fmt.Sprintf("%s", r.AuthorLocation),
fmt.Sprintf("%s", r.MainLanguage),
fmt.Sprintf("%s", r.AllLanguages),
fmt.Sprintf("%s", r.Description),
fmt.Sprintf("%d", r.TotalCodeSize),
fmt.Sprintf("%s", r.License),
fmt.Sprintf("%d", r.AuthorsFollowers),
fmt.Sprintf("%d", r.Top10ContributorsFollowers),
fmt.Sprintf("%d/%02d", r.CreatedAt.Year(), r.CreatedAt.Month()),
fmt.Sprintf("%d", r.Age),
fmt.Sprintf("%d", r.TotalCommits),
fmt.Sprintf("%d", r.TotalAdditions),
fmt.Sprintf("%d", r.TotalDeletions),
fmt.Sprintf("%d", r.TotalCodeChanges),
fmt.Sprintf(r.LastCommitDate.Format("2006-01-02 15:04:05")),
fmt.Sprintf("%.4f", r.CommitsByDay),
fmt.Sprintf("%d", r.AverageContributionPeriod),
fmt.Sprintf("%d", r.MediCommitSize),
fmt.Sprintf("%d", r.TotalTags),
fmt.Sprintf("%d", r.Watchers),
fmt.Sprintf("%d", r.Forks),
fmt.Sprintf("%d", r.Contributors),
fmt.Sprintf("%.2f", r.ActiveForkersPercentage),
fmt.Sprintf("%d", r.ReturningContributors),
fmt.Sprintf("%d", r.OpenIssues),
fmt.Sprintf("%d", r.ClosedIssues),
fmt.Sprintf("%d", r.TotalIssues),
fmt.Sprintf("%.4f", r.IssueByDay),
fmt.Sprintf("%.2f", r.ClosedIssuesPercentage),
fmt.Sprintf("%d", r.PlacementPopularity),
fmt.Sprintf("%d", r.PlacementAge),
fmt.Sprintf("%d", r.PlacementTotalCommits),
fmt.Sprintf("%d", r.PlacementTotalTags),
fmt.Sprintf("%d", r.PlacementTop10ContributorsFollowers),
fmt.Sprintf("%d", r.PlacementClosedIssuesPercentage),
fmt.Sprintf("%d", r.PlacementCommitsByDay),
fmt.Sprintf("%d", r.PlacementActiveForkersColumn),
fmt.Sprintf("%d", r.PlacementOverall),
}
return ghProjectData
}
func headersFromStructTags() []string {
r := new(Repository)
return r.reflectRepositoryHeaders()
}
func (f *Repository) reflectRepositoryHeaders() []string {
var headers []string
val := reflect.ValueOf(f).Elem()
for i := 0; i < val.NumField(); i++ {
headers = append(headers, val.Type().Field(i).Tag.Get("header"))
}
return headers
}
func writeCsv(csvFilePath string, ghDataCSV [][]string) {
file, err := os.Create(csvFilePath)
if err != nil {
log.Fatal("Cannot create file", err)
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, value := range ghDataCSV {
err := writer.Write(value)
if err != nil {
log.Fatal("Cannot write to file", err)
}
}
}