-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
300 lines (260 loc) · 8.18 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//go:generate go-bindata -pkg main -o default-templates-bindata.go templates/
package main
import (
"bufio"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/urfave/cli.v1"
"github.com/jubalh/gontributions/gontrib"
"github.com/jubalh/gontributions/util"
"github.com/jubalh/gontributions/vcs/mediawiki"
"github.com/jubalh/gontributions/vcs/obs"
)
// Summary hold the overall nr of contributions and projects
// we do it like this so we can calculate here, in templates its not supported
type Summary struct {
TotalContributions int
ProjectCount int
}
// TemplateFill is there so it can easily be extended without breaking old templates/layouts
type TemplateFill struct {
Contributions []gontrib.Contribution
Summary Summary
}
const (
templateAssetFolderName = "templates"
)
// loadConfig loads a json configuration from filename
// and creates a Configuration from it.
func loadConfig(filename string) (gontribs gontrib.Configuration, err error) {
file, err := os.OpenFile(filename, os.O_RDONLY, 0660)
if err != nil {
return
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&gontribs)
return
}
// putdate() returns the current date.
// Meant for the template.
func putdate() string {
return time.Now().Local().Format("2006-01-02")
}
// fillTemplate puts the information of a Contribution
// into a template.
func fillTemplate(contributions []gontrib.Contribution, tempContent string, writer io.Writer) {
tf := TemplateFill{Contributions: contributions}
for _, contribution := range contributions {
tf.Summary.TotalContributions += contribution.Count
}
tf.Summary.ProjectCount = len(contributions)
funcMap := template.FuncMap{
"putdate": putdate,
}
t, err := template.New("string-template").Funcs(funcMap).Parse(tempContent)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
err = t.Execute(writer, tf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
// Main will set and parse the cli options.
func main() {
app := cli.NewApp()
app.Name = "gontributions"
app.Usage = "contributions lister"
app.Author = "Michael Vetter"
app.Version = "v0.7.1"
app.Email = "[email protected]"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "gontrib.json",
Usage: "Set which config file to use",
},
cli.StringFlag{
Name: "template",
Value: "default",
Usage: "Set which template to use. If it contains a dot it will be treated as a path to a user defined template. No dot means using an internal template",
},
cli.StringFlag{
Name: "output",
Value: "output.html",
Usage: "Define name of the generated HTMl file",
},
cli.BoolFlag{
Name: "no-pull",
Usage: "Don't update VCS repositories",
},
}
app.Commands = []cli.Command{
{
Name: "exconf",
Usage: "Show an example configuration file",
Action: cmdExconf,
},
{
Name: "list-templates",
Usage: "List all built-in templates",
Action: cmdListTemplates,
},
{
Name: "show-template",
Usage: "Display the content of a template",
Action: cmdShowTemplate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Value: "default",
Usage: "Display the content of a template",
},
},
},
}
app.Action = run
err := app.Run(os.Args)
if err != nil {
fmt.Println(err.Error())
}
}
// getTemplateOrExitError gets the template, if the templatename doesnt contain
// a dot, from the internal assets. if it contains a dot it is treated as a user
// defined template and is treated like a path to the file.
// it returns the template as a string
func getTemplateOrExitError(ctx *cli.Context) (string, error) {
// Get users template selection
templateName := ctx.GlobalString("template")
// if it does not contain a dot it is an internal template, from the assets
if strings.Contains(templateName, ".") == false {
// Use asset
data, err := Asset(filepath.Join(templateAssetFolderName, templateName) + ".html")
if err != nil {
return "", cli.NewExitError(err.Error(), 1)
}
return string(data), nil
}
// user defined template, use templateName as path to file
if !util.FileExists(templateName) {
s := fmt.Sprintf("Template file %s does not exist\n", templateName)
return "", cli.NewExitError(s, 1)
}
data, err := ioutil.ReadFile(templateName)
if err != nil {
return "", cli.NewExitError(err.Error(), 1)
}
return string(data), nil
}
// notifyOnErrors; in case of a pull, update or other error regarding the checkout and
// processing of contributions; will print a message and the path to the error log
// otherwise if will remove any existing 'errors.log' (old errors from last run).
func notifyOnErrors() {
errorfile, err := os.Open("errors.log")
if err != nil {
fmt.Println(err.Error())
return
}
defer errorfile.Close()
fi, err := errorfile.Stat()
if err != nil {
fmt.Println(err.Error())
return
}
if fi.Size() > 0 {
util.PrintInfoF(os.Stderr, "Some contributions could not be checked. See: errors.log", util.PiError)
} else {
os.Remove("errors.log")
}
}
// Run will handle the functionality.
func run(ctx *cli.Context) error {
// Load specified json configuration file
configPath := ctx.GlobalString("config")
configuration, err := loadConfig(configPath)
if err != nil {
// if config cant be loaded because default one is used
// (set in StringFlag) and is not available, then show the usage.
if !ctx.IsSet("config") {
cli.ShowAppHelp(ctx)
return nil
}
return cli.NewExitError(err.Error(), 1)
}
// get template
templateData, err := getTemplateOrExitError(ctx)
if err != nil {
return err
}
gontrib.PullSources = !ctx.GlobalBool("no-pull")
// scan
contributions, err := gontrib.ScanContributions(configuration)
if err != nil {
util.PrintInfo(os.Stderr, err.Error(), util.PiError)
return cli.NewExitError(err.Error(), 1)
}
// define output
outputPath := ctx.GlobalString("output")
f, err := os.Create(outputPath)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
defer f.Close()
writer := bufio.NewWriter(f)
fillTemplate(contributions, templateData, writer)
if err := writer.Flush(); err != nil {
return cli.NewExitError(err.Error(), 1)
}
util.PrintInfoF(os.Stdout, "\nReport saved in: %s", util.PiInfo, outputPath)
notifyOnErrors()
return nil
}
// Create an example configuration file which the user can
// adapt to his own needs.
func cmdExconf(c *cli.Context) error {
configuration := gontrib.Configuration{
Emails: []string{"[email protected]", "[email protected]"},
Projects: []gontrib.Project{
{Name: "nudoku", Description: "Ncurses sudoku game", Gitrepos: []string{"https://github.com/jubalh/nudoku"}, Tags: []string{"C", "Terminal", "Game"}, Role: "maintainer"},
{Name: "profanity", Description: "Ncurses based XMPP client", URL: "http://profanity.im/", Gitrepos: []string{"https://github.com/boothj5/profanity"}, Tags: []string{"C", "XMPP", "Terminal"}},
{Name: "Funtoo", Description: "Linux distribution", URL: "http://funtoo.org/", Gitrepos: []string{"https://github.com/funtoo/ego", "https://github.com/funtoo/metro"}, MediaWikis: []mediawiki.MediaWiki{{BaseUrl: "http://funtoo.org", User: "jubalh"}}},
{Name: "openSUSE", Description: "Linux distribution", URL: "http://opensuse.org/", Obs: []obs.OpenBuildService{{Apiurl: "https://api.opensuse.org", Repo: "utilities/vifm"}}},
{Name: "prosody", Description: "A modern XMPP communication server", URL: "http://prosody.im/", Hgrepos: []string{"https://hg.prosody.im/prosody-modules"}, Tags: []string{"Lua", "XMPP"}},
{Name: "Debian", Description: "Debian Linux", Debian: []string{"nudoku"}},
},
}
text, err := json.MarshalIndent(configuration, "", " ")
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
fmt.Println(string(text))
return nil
}
func cmdListTemplates(c *cli.Context) error {
internalTemplates, err := AssetDir(templateAssetFolderName)
if err != nil {
return err
}
for _, t := range internalTemplates {
fmt.Println(t[:strings.Index(t, ".")])
}
return nil
}
func cmdShowTemplate(c *cli.Context) error {
template := c.String("name")
data, err := Asset(filepath.Join(templateAssetFolderName, template) + ".html")
if err != nil {
return err
}
fmt.Print(string(data))
return nil
}