Skip to content

Commit 2a0ce78

Browse files
authored
Merge pull request #51 from g4s8/i28
feat: add ilyakaznacheev/cleanenv support
2 parents 185c18c + b7e3a49 commit 2a0ce78

File tree

14 files changed

+584
-292
lines changed

14 files changed

+584
-292
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type Config struct {
4747
* `-dir` (path string, *optional*) - Specify the directory to search for files. Default is the file dir with `go:generate` command.
4848
* `-files` (glob string, *optional*) - File glob pattern to specify file names to process. Default is the single file with `go:generate`.
4949
* `-types` (glob string, *optional*) - Type glob pattern for type names to process. If not specified, the next type after `go:generate` is used.
50+
* `-target` (`enum(caarlos0, cleanenv)` string, optional, default `caarlos0`) - Set env library target.
5051
* `-output` (path string, **required**) - Output file name for generated documentation.
5152
* `-format` (`enum(markdown, plaintext, html, dotenv, json)` string, *optional*) - Output format for documentation. Default is `markdown`.
5253
* `-no-styles` (`bool`, *optional*) - If true, CSS styles will not be included for `html` format.
@@ -96,6 +97,7 @@ See [_examples](./_examples/) dir for more details.
9697

9798
This tool is compatible with
9899
- full compatibility: [caarlos0/env](https://github.com/caarlos0/env)
100+
- full compatibility: [ilyakaznacheev/cleanenv](https://github.com/ilyakaznacheev/cleanenv)
99101
- partial compatibility: [sethvargo/go-envconfig](https://github.com/sethvargo/go-envconfig)
100102
- partial compatibility: [joeshaw/envdecode](https://github.com/joeshaw/envdecode)
101103

_examples/cleanenv/config.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
// Config is an example configuration structure.
4+
// It is used to generate documentation for the configuration
5+
// using the commands below.
6+
//
7+
//go:generate go run ../../ -output doc.txt -target cleanenv -format plaintext
8+
type Config struct {
9+
// Hosts name of hosts to listen on.
10+
Hosts []string `env:"HOST" env-required:"true" env-separator:";"`
11+
// Port to listen on.
12+
Port int `env:"PORT"`
13+
14+
// Debug mode enabled.
15+
Debug bool `env:"DEBUG" env-default:"false"`
16+
17+
// Timeouts configuration.
18+
Timeouts struct {
19+
// Read timeout.
20+
Read int `env:"READ" env-default:"10"`
21+
// Write timeout.
22+
Write int `env:"WRITE" env-default:"10"`
23+
} `env-prefix:"TIMEOUT_"`
24+
}

_examples/cleanenv/doc.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Environment Variables
2+
3+
## Config
4+
5+
Config is an example configuration structure.
6+
It is used to generate documentation for the configuration
7+
using the commands below.
8+
9+
* `HOST` (separated by `;`, required) - Hosts name of hosts to listen on.
10+
* `PORT` - Port to listen on.
11+
* `DEBUG` (default: `false`) - Debug mode enabled.
12+
* Timeouts configuration.
13+
* `TIMEOUT_READ` (default: `10`) - Read timeout.
14+
* `TIMEOUT_WRITE` (default: `10`) - Write timeout.
15+

ast/decoder.go

Lines changed: 0 additions & 99 deletions
This file was deleted.

ast/decoder_test.go

Lines changed: 0 additions & 184 deletions
This file was deleted.

config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type Config struct {
3030
NoStyles bool
3131
// FieldNames flag enables field names usage intead of `env` tag.
3232
FieldNames bool
33+
// Target is the target type
34+
Target types.TargetType
3335

3436
// TagName sets custom tag name, `env` by default.
3537
TagName string
@@ -53,6 +55,8 @@ func (c *Config) parseFlags(f *flag.FlagSet) error {
5355
f.StringVar(&c.Dir, "dir", "", "Dir to search for files, default is the file dir with go:generate command")
5456
f.StringVar(&c.FileGlob, "files", "", "FileGlob to filter by file name")
5557
f.StringVar(&c.TypeGlob, "types", "", "Type glob to filter by type name")
58+
var target string
59+
f.StringVar(&target, "target", "caarlos0", "Target type, default `caarlos0`")
5660
// output flags
5761
f.StringVar(&c.OutFile, "output", "", "Output file path")
5862
f.StringVar((*string)(&c.OutFormat), "format", "markdown", "Output format, default `markdown`")
@@ -89,6 +93,12 @@ func (c *Config) parseFlags(f *flag.FlagSet) error {
8993
return errors.New("flags -type and -types can't be used together")
9094
}
9195

96+
targetType, err := types.ParseTargetType(target)
97+
if err != nil {
98+
return fmt.Errorf("parse target type: %w", err)
99+
}
100+
c.Target = targetType
101+
92102
// check for deprecated flags
93103
var deprecatedWarning strings.Builder
94104
if typeName != "" {

0 commit comments

Comments
 (0)