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 render data #17

Merged
merged 1 commit into from
Jun 16, 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Flags:
| `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 ` |
| `printStarHistory` | `{{printStarHistory "linuxsuren" "yaml-readme"}}` | Print the star history of an repository |
| `printVisitorCount` | `{{printVisitorCount "repo-id"}}` | Print the visitor count chart of an repository |
| `render` | `{{render true}}` | Make the value be readable, turn `true` to `:white_check_mark:` |

### Ignore particular items

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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.v3 v3.0.0-20200313102051-9f266ea9e77c
)

require (
Expand All @@ -14,5 +15,4 @@ require (
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
)
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func getFuncMap(readmeTpl string) template.FuncMap {
"printVisitorCount": func(id string) string {
return fmt.Sprintf(`![Visitor Count](https://profile-counter.glitch.me/%s/count.svg)`, id)
},
"render": dataRender,
}
}

Expand Down Expand Up @@ -284,6 +285,20 @@ func printStarHistory(owner, repo string) string {
owner, repo)
}

func dataRender(data interface{}) string {
switch val := data.(type) {
case bool:
if val {
return ":white_check_mark:"
} else {
return ":x:"
}
case string:
return val
}
return ""
}

var contributorsTpl = `{{- range $i, $val := .}}
<td align="center">
<a href="{{$val.html_url}}">
Expand Down
34 changes: 34 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,37 @@ func Test_getFuncMap(t *testing.T) {
assert.Contains(t, buf.String(), k)
}
}

func Test_dataRender(t *testing.T) {
type args struct {
data interface{}
}
tests := []struct {
name string
args args
want string
}{{
name: "bool type with true value",
args: args{
data: true,
},
want: ":white_check_mark:",
}, {
name: "bool type with false value",
args: args{
data: false,
},
want: ":x:",
}, {
name: "normal string value fake",
args: args {
data:"fake",
},
want:"fake",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, dataRender(tt.args.data), "dataRender(%v)", tt.args.data)
})
}
}