Skip to content

ifnotnil/pkgen

Repository files navigation

pkgen

CI Status codecov Go Report Card PkgGoDev

Generate a file inside each package. Using pre-made templates or custom ones.

Install

go install github.com/ifnotnil/pkgen/cmd/pkgen@latest

Running Modes

In //go:generate

Inside a //go:generate comment line this:

//go:generate pkgen --template '<template>'

This will result running only for the current package

Project level

Running the pkgen in the project level will result running for all the packages recursively.

pkgen --template '<template>'

Or with a custom template

pkgen --template-file /path/to/template.tmpl

Templates

Built-in Templates

Template File Description
pkgpath pkgpath.tmpl Simple template that generates the full package path as a string constant
oteltrace oteltrace.tmpl Basic OpenTelemetry tracing setup with tracer only. It creates a package level tracer, using the full package path as name.
otel otel.tmpl Full OpenTelemetry setup with tracer, meter, and logger for observability. It creates a package level tracer, meter and logger, using the full package path as name.

Custom Templates

Each template is rendered provided the struct returned from golang.org/x/tools/go/packages

Config

Optionally you can define a config yaml file. By default pkgen will try to read the file .pkgen.yml in the working directory, if exists.

Additionally you can specify a config file for the pkgen to read using the --config <path to yaml> argument.

Example of config file:

templates:      # One or more templates can be selected. Pre-configured or custom templates can be selected.
  - otel
  - template_file: path/to/template.tmpl
packages_query:
  patterns:                    # package patterns that `go list` accepts. Default value is `./...`
    - './internal/app'         # single package
    - './internal/domain/...'  # recursive
    - './pkg/eventbus'

About

Generate code per package based on templates

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •