Skip to content

add .LicensePath and licenseText func to templates#179

Merged
Bobgy merged 2 commits intogoogle:masterfrom
gwatts:add-licensetext-to-templates
Jan 16, 2023
Merged

add .LicensePath and licenseText func to templates#179
Bobgy merged 2 commits intogoogle:masterfrom
gwatts:add-licensetext-to-templates

Conversation

@gwatts
Copy link
Copy Markdown
Contributor

@gwatts gwatts commented Jan 13, 2023

Maybe resolves #144

  • Add .LicensePath to report templates reflecting the local file path of the license
  • Add a licenseText function to templates that takes a lib as an argument and renders the license text, if available

Add a small template example to the readme

* Add `.LicensePath` to report templates reflecting the local file path of
  the license
* Add a `licenseText` function to templates that takes a lib as an
  argument and renders the license text, if available

Add a small template example to the readme
report.go Outdated
funcMap := template.FuncMap{
"licenseText": func(lib libraryData) (string, error) {
if lib.LicensePath == "" {
return "", nil
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: return some error message here for debugging?

like error: license path is empty?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do; you'd have to guard your use of it then by testing .LicensePath in your templates, but that's probably a better approach.

Also occurred to me that instead of adding a function to the template, i could add a method to libraryData and then template would then use {{ call .LicenseText }} .. not sure if that's better vs just different.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could do; you'd have to guard your use of it then by testing .LicensePath in your templates, but that's probably a better approach.

Not sure I follow. I meant returning string error message that will be rendered by the template. Are our understanding the same?

Re method vs function, yes, method is better. People should not pass other objects.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i assumed you meant return "", errors.New("no license found") which would cause the entire template not to render..

I don't think I'd want to return an error string in place of the license text (e.g. return "no license found", nil)

Copy link
Copy Markdown
Collaborator

@Bobgy Bobgy Jan 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return an error string in place of the license text

That's exactly what I meant.

On second thought, I agree it is not very helpful. When you render entire license text, the file is too large to read. Most likely no one can find an error message in the middle.

Shall we log an error message here instead? This should not be silently ignored.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think it should log an error if it's documented behaviour - If LicensePath is empty, it returns an empty string. An error would be failing to read from that path.

I personally just wrap it with a check:

{{ if .LicensePath }}
```
{{ .LicenseText }}
```
{{ end }}

I pushed an update that switches to using a method instead of adding a template function in any case

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Adding clear doc on corner cases help remove unexpected behaviors.

That also reminds me .LicenseName is already a good enough indicator for license not found. So there is no need to check this case for this method.

In the future, if there could be expected cases that LicenseName is non empty but LicensePath is... we might need to revisit the decision here.

@gwatts gwatts force-pushed the add-licensetext-to-templates branch from e27cf16 to f986c4e Compare January 15, 2023 16:45
@Bobgy
Copy link
Copy Markdown
Collaborator

Bobgy commented Jan 16, 2023

Awesome! Thank you for the contribution!

@Bobgy Bobgy merged commit 5348b74 into google:master Jan 16, 2023
@gwatts
Copy link
Copy Markdown
Contributor Author

gwatts commented Jan 16, 2023

@Bobgy thanks for merging! Appreciate all your work on this project; super useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export local license paths

2 participants