Skip to content

Commit

Permalink
Add plain text template
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhammond committed Sep 7, 2021
1 parent ace4b74 commit 9abb016
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ For example:
//go:generate go run github.com/paulhammond/licensepack -tmpl string-const .
```

If you’d prefer to generate plain text output, perhaps because you’d like to
embed licensing information using a different mechanism, there is a plain text
template available. To use this you’ll need to disable gofmt. For example:

```
//go:generate go run github.com/paulhammond/licensepack -file licenses.txt -nofmt -tmpl text ./cmd
```

### Custom templates

If you’d like to customize the template you can provide the path to a template
Expand Down
18 changes: 10 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
pkg = flag.String("pkg", "main", "package for generated code")
file = flag.String("file", "licenses.go", "filename for generated code")
variable = flag.String("var", "Licenses", "variable name")
nofmt = flag.Bool("nofmt", false, "run output through go fmt")
quiet = flag.Bool("quiet", false, "only output error messages")
credits = flag.Bool("credits", false, "show open source credits")
help = flag.Bool("help", false, "show help")
Expand Down Expand Up @@ -197,10 +198,6 @@ func main() {
})

var src bytes.Buffer
_, err = src.Write([]byte("// " + comment + "\n"))
if err != nil {
log.Fatal(err)
}

t, err := parseTemplate(*tmpl)
if err != nil {
Expand All @@ -210,22 +207,27 @@ func main() {
err = t.Execute(&src, struct {
Pkg string
Var string
Comment string
Modules []license.Module
}{
Pkg: *pkg,
Var: *variable,
Modules: modules,
Comment: comment,
})
if err != nil {
log.Fatal(err)
}

formatted, err := format.Source(src.Bytes())
if err != nil {
log.Fatal(err)
output := src.Bytes()
if !*nofmt {
output, err = format.Source(src.Bytes())
if err != nil {
log.Fatal(err)
}
}

err = os.WriteFile(*file, formatted, 0666)
err = os.WriteFile(*file, output, 0666)
if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions tmpl/string-const.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

const {{ .Var }} = {{ (eval "licenses" .) | wrapquote "\t\t" }}
Expand Down
1 change: 1 addition & 0 deletions tmpl/string-func.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

func {{ .Var }}() string {
Expand Down
1 change: 1 addition & 0 deletions tmpl/string-var.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

var {{ .Var }} = {{ (eval "licenses" .) | wrapquote "\t\t" }}
Expand Down
1 change: 1 addition & 0 deletions tmpl/string.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

func init() {
Expand Down
1 change: 1 addition & 0 deletions tmpl/struct-func.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

import "github.com/paulhammond/licensepack/license"
Expand Down
1 change: 1 addition & 0 deletions tmpl/struct-var.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

import "github.com/paulhammond/licensepack/license"
Expand Down
1 change: 1 addition & 0 deletions tmpl/struct.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// {{ .Comment }}
package {{ .Pkg }}

import "github.com/paulhammond/licensepack/license"
Expand Down
10 changes: 10 additions & 0 deletions tmpl/text.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ range .Modules -}}
## {{ .Name }}

{{ range .Licenses -}}
{{ .Path }}:

{{ .Contents }}

{{ end -}}
{{ end -}}

0 comments on commit 9abb016

Please sign in to comment.