mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-16 19:33:47 +00:00
90 lines
1.7 KiB
Go
90 lines
1.7 KiB
Go
|
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||
|
|
||
|
// See page 113.
|
||
|
|
||
|
// Issuesreport prints a report of issues matching the search terms.
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"os"
|
||
|
"text/template"
|
||
|
"time"
|
||
|
|
||
|
"gopl.io/ch4/github"
|
||
|
)
|
||
|
|
||
|
//!+template
|
||
|
const templ = `{{.TotalCount}} issues:
|
||
|
{{range .Items}}----------------------------------------
|
||
|
Number: {{.Number}}
|
||
|
User: {{.User.Login}}
|
||
|
Title: {{.Title | printf "%.64s"}}
|
||
|
Age: {{.CreatedAt | daysAgo}} days
|
||
|
{{end}}`
|
||
|
|
||
|
//!-template
|
||
|
|
||
|
//!+daysAgo
|
||
|
func daysAgo(t time.Time) int {
|
||
|
return int(time.Since(t).Hours() / 24)
|
||
|
}
|
||
|
|
||
|
//!-daysAgo
|
||
|
|
||
|
//!+exec
|
||
|
var report = template.Must(template.New("issuelist").
|
||
|
Funcs(template.FuncMap{"daysAgo": daysAgo}).
|
||
|
Parse(templ))
|
||
|
|
||
|
func main() {
|
||
|
result, err := github.SearchIssues(os.Args[1:])
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if err := report.Execute(os.Stdout, result); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//!-exec
|
||
|
|
||
|
func noMust() {
|
||
|
//!+parse
|
||
|
report, err := template.New("report").
|
||
|
Funcs(template.FuncMap{"daysAgo": daysAgo}).
|
||
|
Parse(templ)
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
//!-parse
|
||
|
result, err := github.SearchIssues(os.Args[1:])
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
if err := report.Execute(os.Stdout, result); err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
//!+output
|
||
|
$ go build gopl.io/ch4/issuesreport
|
||
|
$ ./issuesreport repo:golang/go is:open json decoder
|
||
|
13 issues:
|
||
|
----------------------------------------
|
||
|
Number: 5680
|
||
|
User: eaigner
|
||
|
Title: encoding/json: set key converter on en/decoder
|
||
|
Age: 750 days
|
||
|
----------------------------------------
|
||
|
Number: 6050
|
||
|
User: gopherbot
|
||
|
Title: encoding/json: provide tokenizer
|
||
|
Age: 695 days
|
||
|
----------------------------------------
|
||
|
...
|
||
|
//!-output
|
||
|
*/
|