package main

import (
	"Study/pkg/github"
	"log"
	"os"
	"text/template"
	"time"
)

const temple = `{{.TotalCount}} issues:
{{range .Items}}----------------------------------------
Number: {{.Number}}
User:   {{.User.Login}}
Title:  {{.Title | printf "%.64s"}}
Age:    {{.CreatedAt | daysAgo}} days
{{end}}`

func dayAgo(t time.Time) float64 {
	return time.Since(t).Hours() / 24.0
}

func main() {
	report, err := template.New("report").
		Funcs(template.FuncMap{"daysAgo": dayAgo}).
		Parse(temple)
	if err != nil {
		log.Fatal(err)
	}

	res, err := github.SearchIssues([]string{"go/golang"})
	if err != nil {
		log.Fatal(err)
	}
	//if err := report.ExecuteTemplate(os.Stdout, "temple", res); err != nil {
	if err := report.Execute(os.Stdout, res); err != nil {
		log.Fatal(err)
	}
}
