-
Notifications
You must be signed in to change notification settings - Fork 10
feat: customizable template contents using file or multiline string #22
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
Open
tuunit
wants to merge
1
commit into
urfave:main
Choose a base branch
from
tuunit:feat/customizable-templates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import ( | |
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/cpuguy83/go-md2man/v2/md2man" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/urfave/cli/v3" | ||
| ) | ||
|
|
@@ -190,6 +191,32 @@ func TestToMarkdownFull(t *testing.T) { | |
| expectFileContent(t, "testdata/expected-doc-full.md", res) | ||
| } | ||
|
|
||
| func TestBuilderToMarkdown(t *testing.T) { | ||
| cmd := buildExtendedTestCommand(t) | ||
|
|
||
| res, err := Template(`{{ .Command.Name }}|{{ .SectionNum }}|{{ len .Commands }}|{{ len .GlobalArgs }}|{{ len .SynopsisArgs }}`).ToMarkdown(cmd) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "greet|0|6|4|4", res) | ||
| } | ||
|
|
||
| func TestBuilderToMarkdownFromFile(t *testing.T) { | ||
| cmd := buildExtendedTestCommand(t) | ||
|
|
||
| tmpFile, err := os.CreateTemp("", "*.gotmpl") | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = os.Remove(tmpFile.Name()) }) | ||
|
|
||
| _, err = tmpFile.WriteString(`{{ .Command.Name }} from {{ .Command.Usage }}`) | ||
| require.NoError(t, err) | ||
| require.NoError(t, tmpFile.Close()) | ||
|
|
||
| res, err := TemplateFile(tmpFile.Name()).ToMarkdown(cmd) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "greet from Some app", res) | ||
| } | ||
|
|
||
| func TestToTabularMarkdown(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
|
|
@@ -212,6 +239,32 @@ func TestToTabularMarkdown(t *testing.T) { | |
| }) | ||
| } | ||
|
|
||
| func TestBuilderToTabularMarkdown(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
| res, err := Template(`{{ .AppPath }}|{{ .Name }}|{{ join (index .Commands 0).Aliases "," }}`).ToTabularMarkdown(app, "/usr/local/bin") | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "/usr/local/bin|greet|c\n", res) | ||
| } | ||
|
|
||
| func TestBuilderToTabularMarkdownFromFile(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
| tmpFile, err := os.CreateTemp("", "*.gotmpl") | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = os.Remove(tmpFile.Name()) }) | ||
|
|
||
| _, err = tmpFile.WriteString(`{{ .AppPath }}|{{ (index .Commands 1).Name }}`) | ||
| require.NoError(t, err) | ||
| require.NoError(t, tmpFile.Close()) | ||
|
|
||
| res, err := TemplateFile(tmpFile.Name()).ToTabularMarkdown(app, "/usr/local/bin") | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, "/usr/local/bin|info\n", res) | ||
| } | ||
|
|
||
| func TestToTabularMarkdownFailed(t *testing.T) { | ||
| tpl := MarkdownTabularDocTemplate | ||
| t.Cleanup(func() { MarkdownTabularDocTemplate = tpl }) | ||
|
|
@@ -389,6 +442,19 @@ func TestToMan(t *testing.T) { | |
| expectFileContent(t, "testdata/expected-doc-full.man", res) | ||
| } | ||
|
|
||
| func TestBuilderToMan(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
| tpl := `# NAME | ||
|
|
||
| {{ .Command.Name }} | ||
| ` | ||
|
|
||
| res, err := Template(tpl).ToMan(app) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, string(md2man.Render([]byte("# NAME\n\ngreet\n"))), res) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| func TestToManParseError(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
|
|
@@ -401,6 +467,34 @@ func TestToManParseError(t *testing.T) { | |
| require.ErrorContains(t, err, "template: cli:1: unclosed action") | ||
| } | ||
|
|
||
| func TestTemplateFileMissing(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
| _, err := TemplateFile("/missing/template.gotmpl").ToMarkdown(app) | ||
|
|
||
| require.ErrorIs(t, err, fs.ErrNotExist) | ||
| } | ||
|
|
||
| func TestBuilderToManWithSectionFromFile(t *testing.T) { | ||
| app := buildExtendedTestCommand(t) | ||
|
|
||
| tmpFile, err := os.CreateTemp("", "*.gotmpl") | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = os.Remove(tmpFile.Name()) }) | ||
|
|
||
| _, err = tmpFile.WriteString(`# NAME | ||
|
|
||
| {{ .Command.Name }} ({{ .SectionNum }}) | ||
| `) | ||
| require.NoError(t, err) | ||
| require.NoError(t, tmpFile.Close()) | ||
|
|
||
| res, err := TemplateFile(tmpFile.Name()).ToManWithSection(app, 5) | ||
|
|
||
| require.NoError(t, err) | ||
| require.Equal(t, string(md2man.Render([]byte("# NAME\n\ngreet (5)\n"))), res) | ||
| } | ||
|
|
||
| func TestToManWithSection(t *testing.T) { | ||
| cmd := buildExtendedTestCommand(t) | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace all that with
docs.Render(template, vars), with template beingdocs.TpltMarkdown,docs.TpltMarkdownTabular, etc. And provide good documentation aboutdocs.Varstemplate variables. Just thinking..There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be cleaner but would be a breaking API change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can go through deprecation cycle until v4 is ready https://github.com/urfave/cli-docs/releases ?
Or don't remove anything at all, like Go does. This project is not size critical.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my opinion are three ways to build a library of this sort.
docs.Render(Options)-> really clean but breaks the existing API surfacedocs.WithOptioms(Options).Render()or methods per optionsdocs.Template(template).DocumentPerCommand().Render()I decided to go with the third way because it can be introduced easily without breaking the existing API surface while extending the preexisting API as well. So it was a win win in my eyes.
Obviously we could go with the second approach as well, as it is quite clean to have a single options struct. Even with that we could mark the existing methods as deprecated and replace the internal logic with a proper Render call and the right options.
Let me know what you would prefer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not an expert, so I am just throwing an opinion. Constructor pattern is not unusual in Go, but it is usually used in stateful libs. Here I don't see the need to store template or the error as a state. Why maintain a long call stack if it can be reduced to a single
docs.Render(template, vars)call? Andvarscan be the state perhaps derived from*cli? But then maybe it can work with bothcli/v2andcli/v3? I have no answers. I know only that with the extending the API, we don't break anything.I would wait for @dearchap to chime in before pivoting. Maybe it is fine after all. )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer a constructor pattern as well by the by but rather than
docs.Render(template, vars)I would dodocs.Render(vars)and treat the template just as any other option field in a struct like:Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@abitrolly
If it wasn't clear from my responses. I do like this type of interaction and feedback. I appreciate it a lot when people take time looking into such seemingly mundane topics such as this PR