Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to print star history and visitor count #14

Merged
merged 3 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Pull Request Build

on:
push:
branches:
- master
pull_request:
branches:
- master
Expand All @@ -19,7 +22,15 @@ jobs:
uses: actions/checkout@v3.0.0
- name: Test
run: |
go test ./...
go test ./... -coverprofile coverage.out
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: coverage.out
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.9.1
with:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist/
bin/
.idea/
.vscode/
coverage.out
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![codecov](https://codecov.io/gh/LinuxSuRen/yaml-readme/branch/master/graph/badge.svg?token=mnFyeD2IQ7)](https://codecov.io/gh/LinuxSuRen/yaml-readme)

A helper to generate the READE file automatically.

## Get started
Expand Down Expand Up @@ -31,11 +33,13 @@ Flags:

### Available functions

| Name | Usage | Description |
|--------------------|----------------------|-------------------------------------------------------------------------|
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| `printToc` | `{{printToc}}` | Print the [TOC](https://en.wikipedia.org/wiki/TOC) of the template file |
| Name | Usage | Description |
|---------------------|----------------------------------------------------|-------------------------------------------------------------------------|
| `printHelp` | `{{printHelp 'hd'}}` | Print the help text of a command |
| `printToc` | `{{printToc}}` | Print the [TOC](https://en.wikipedia.org/wiki/TOC) of the template file |
| `printContributors` | `{{printContributors "linuxsuren" "yaml-readme"}}` | Print all the contributors of an repository |
| `printStarHistory` | `{{printStarHistory "linuxsuren" "yaml-readme"}}` | Print the star history of an repository ` |
| `printVisitorCount` | `{{printVisitorCount "repo-id"}}` | Print the visitor count chart of an repository ` |

### Ignore particular items

Expand Down
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://docs.codecov.com/docs/ignoring-paths

# https://docs.codecov.com/docs/commit-status
# it's hard to have a coverage for some code lines
coverage:
status:
project:
default:
target: auto
threshold: 0.1%
patch:
default:
target: auto
threshold: 0%
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module github.com/linuxsuren/yaml-readme

go 1.18
go 1.17

require (
github.com/h2non/gock v1.0.9
github.com/spf13/cobra v1.4.0
github.com/stretchr/testify v1.7.1
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/h2non/gock v1.0.9 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/h2non/gock v1.0.9 h1:17gCehSo8ZOgEsFKpQgqHiR7VLyjxdAG3lkhVvO9QZU=
github.com/h2non/gock v1.0.9/go.mod h1:CZMcB0Lg5IWnr9bF79pPMg9WeV6WumxQiUJ1UvdO1iE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32 h1:W6apQkHrMkS0Muv8G/TipAy/FJl/rCYT0+EuS8+Z0z4=
github.com/nbio/st v0.0.0-20140626010706-e9e8d9816f32/go.mod h1:9wM+0iRr9ahx58uYLpLIr5fm8diHn0JbqRycJi6w0Ms=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -17,7 +19,6 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
68 changes: 57 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
Expand All @@ -23,9 +24,22 @@ type option struct {
includeHeader bool
sortBy string
groupBy string

printFunctions bool
printVariables bool
}

func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
if o.printFunctions {
printFunctions(cmd.OutOrStdout())
return
}

if o.printVariables {
printVariables(cmd.OutOrStdout())
return
}

var items []map[string]interface{}
groupData := make(map[string][]map[string]interface{})

Expand Down Expand Up @@ -110,7 +124,34 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {

// generate readme file
var tpl *template.Template
if tpl, err = template.New("readme").Funcs(template.FuncMap{
if tpl, err = template.New("readme").Funcs(getFuncMap(readmeTpl)).Parse(readmeTpl); err != nil {
return
}

// render it with grouped data
if o.groupBy != "" {
err = tpl.Execute(os.Stdout, groupData)
} else {
err = tpl.Execute(os.Stdout, items)
}
return
}

func printVariables(stdout io.Writer) {
_, _ = stdout.Write([]byte(`filename
parentname
fullpath`))
}

func printFunctions(stdout io.Writer) {
funcMap := getFuncMap("")
for k := range funcMap {
_, _ = stdout.Write([]byte(fmt.Sprintf("%s\n", k)))
}
}

func getFuncMap(readmeTpl string) template.FuncMap {
return template.FuncMap{
"printHelp": func(cmd string) (output string) {
var err error
var data []byte
Expand All @@ -129,17 +170,13 @@ func (o *option) runE(cmd *cobra.Command, args []string) (err error) {
"printContributors": func(owner, repo string) template.HTML {
return template.HTML(printContributors(owner, repo))
},
}).Parse(readmeTpl); err != nil {
return
}

// render it with grouped data
if o.groupBy != "" {
err = tpl.Execute(os.Stdout, groupData)
} else {
err = tpl.Execute(os.Stdout, items)
"printStarHistory": func(owner, repo string) string {
return printStarHistory(owner, repo)
},
"printVisitorCount": func(id string) string {
return fmt.Sprintf(`![Visitor Count](https://profile-counter.glitch.me/%s/count.svg)`, id)
},
}
return
}

func sortBy(items []map[string]interface{}, sortBy string, descending bool) {
Expand Down Expand Up @@ -242,6 +279,11 @@ func generateContributor(contributors []map[string]interface{}) (output string)
return
}

func printStarHistory(owner, repo string) string {
return fmt.Sprintf(`[![Star History Chart](https://api.star-history.com/svg?repos=%[1]s/%[2]s&type=Date)](https://star-history.com/#%[1]s/%[2]s&Date)`,
owner, repo)
}

var contributorsTpl = `{{- range $i, $val := .}}
<td align="center">
<a href="{{$val.html_url}}">
Expand Down Expand Up @@ -271,6 +313,10 @@ func main() {
"Sort the array data descending by which field, or sort it ascending with the prefix '!'. For example: --sort-by !year")
flags.StringVarP(&opt.groupBy, "group-by", "", "",
"Group the array data by which field")
flags.BoolVarP(&opt.printFunctions, "print-functions", "", false,
"Print all the functions and exit")
flags.BoolVarP(&opt.printVariables, "print-variables", "", false,
"Print all the variables and exit")

err := cmd.Execute()
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"github.com/h2non/gock"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -153,3 +154,42 @@ func Test_printContributor(t *testing.T) {
})
}
}

func Test_printStarHistory(t *testing.T) {
type args struct {
owner string
repo string
}
tests := []struct {
name string
args args
want string
}{{
name: "simple",
args: args{
owner: "linuxsuren",
repo: "yaml-readme",
},
want: `[![Star History Chart](https://api.star-history.com/svg?repos=linuxsuren/yaml-readme&type=Date)](https://star-history.com/#linuxsuren/yaml-readme&Date)`,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, printStarHistory(tt.args.owner, tt.args.repo), "printStarHistory(%v, %v)", tt.args.owner, tt.args.repo)
})
}
}

func Test_getFuncMap(t *testing.T) {
funcMap := getFuncMap("")
assert.NotNil(t, funcMap["printToc"])
assert.NotNil(t, funcMap["printHelp"])
assert.NotNil(t, funcMap["printContributors"])
assert.NotNil(t, funcMap["printStarHistory"])
assert.NotNil(t, funcMap["printVisitorCount"])

buf := bytes.NewBuffer([]byte{})
printFunctions(buf)
for k := range funcMap {
assert.Contains(t, buf.String(), k)
}
}