From 31ffcfe1f70613edd87c7762944dfbc90966cf7c Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Thu, 16 Jun 2022 16:40:30 +0800 Subject: [PATCH] Support to render data (#17) Co-authored-by: rick --- README.md | 5 +++-- go.mod | 2 +- main.go | 15 +++++++++++++++ main_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 76cf993..264ce05 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/go.mod b/go.mod index ad39abf..6d9f78b 100644 --- a/go.mod +++ b/go.mod @@ -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 ( @@ -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 ) diff --git a/main.go b/main.go index e3b92f1..824ef34 100644 --- a/main.go +++ b/main.go @@ -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, } } @@ -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 := .}} diff --git a/main_test.go b/main_test.go index cfcc143..44ae179 100644 --- a/main_test.go +++ b/main_test.go @@ -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) + }) + } +}