diff --git a/README.md b/README.md index ff82ad6..6916da2 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ type Config struct { * `-files` (glob string, *optional*) - File glob pattern to specify file names to process. Default is the single file with `go:generate`. * `-types` (glob string, *optional*) - Type glob pattern for type names to process. If not specified, the next type after `go:generate` is used. * `-output` (path string, **required**) - Output file name for generated documentation. - * `-format` (`enum(markdown, plaintext, html, dotenv)` string, *optional*) - Output format for documentation. Default is `markdown`. + * `-format` (`enum(markdown, plaintext, html, dotenv, json)` string, *optional*) - Output format for documentation. Default is `markdown`. * `-no-styles` (`bool`, *optional*) - If true, CSS styles will not be included for `html` format. * `-env-prefix` (`string`, *optional*) - Sets additional global prefix for all environment variables. * `-tag-name` (string, *optional*, default: `env`) - Use custom tag name instead of `env`. diff --git a/_examples/simple/config.go b/_examples/simple/config.go index a919c7e..c46104f 100644 --- a/_examples/simple/config.go +++ b/_examples/simple/config.go @@ -8,6 +8,7 @@ package main //go:generate go run ../../ -output doc.md -format markdown //go:generate go run ../../ -output doc.html -format html //go:generate go run ../../ -output doc.env -format dotenv +//go:generate go run ../../ -output doc.json -format json type Config struct { // Hosts name of hosts to listen on. Hosts []string `env:"HOST,required", envSeparator:";"` diff --git a/_examples/simple/doc.json b/_examples/simple/doc.json new file mode 100644 index 0000000..5f7bd52 --- /dev/null +++ b/_examples/simple/doc.json @@ -0,0 +1,23 @@ +[ + { + "env_name": "HOST", + "doc": "Hosts name of hosts to listen on.", + "env_separator": ";", + "required": true + }, + { + "env_name": "PORT", + "doc": "Port to listen on.", + "required": true, + "non_empty": true + }, + { + "env_name": "DEBUG", + "doc": "Debug mode enabled.", + "env_default": "false" + }, + { + "env_name": "PREFIX", + "doc": "Prefix for something." + } +] diff --git a/render/config.go b/render/config.go index de02796..47db92e 100644 --- a/render/config.go +++ b/render/config.go @@ -65,4 +65,8 @@ var configs = map[types.OutFormat]renderConfig{ }, tmpl: newTmplText("dotenv.tmpl"), }, + types.OutFormatJSON: { + Item: renderItemConfig{}, + tmpl: newTmplText("json.tmpl"), + }, } diff --git a/render/renderer.go b/render/renderer.go index 7c7a831..7e74ba9 100644 --- a/render/renderer.go +++ b/render/renderer.go @@ -41,18 +41,18 @@ type renderSection struct { } type renderItem struct { - EnvName string - Doc string - EnvDefault string - EnvSeparator string + EnvName string `json:"env_name"` + Doc string `json:"doc"` + EnvDefault string `json:"env_default,omitempty"` + EnvSeparator string `json:"env_separator,omitempty"` - Required bool - Expand bool - NonEmpty bool - FromFile bool + Required bool `json:"required,omitempty"` + Expand bool `json:"expand,omitempty"` + NonEmpty bool `json:"non_empty,omitempty"` + FromFile bool `json:"from_file,omitempty"` children []renderItem - Indent int + Indent int `json:"-"` } func (i renderItem) Children(indentInc int) []renderItem { diff --git a/render/templ/json.tmpl b/render/templ/json.tmpl new file mode 100644 index 0000000..05b2ada --- /dev/null +++ b/render/templ/json.tmpl @@ -0,0 +1,3 @@ +{{- range .Sections }} +{{- marshalIndent .Items }} +{{- end }} diff --git a/render/templates.go b/render/templates.go index 325fbe8..49d9332 100644 --- a/render/templates.go +++ b/render/templates.go @@ -2,6 +2,7 @@ package render import ( "embed" + "encoding/json" "path" "strings" @@ -31,6 +32,10 @@ var tplFuncs = map[string]any{ } return sum }, + "marshalIndent": func(v any) (string, error) { + a, err := json.MarshalIndent(v, "", " ") + return string(a), err + }, } const ( diff --git a/types/model.go b/types/model.go index 20ddb17..11879af 100644 --- a/types/model.go +++ b/types/model.go @@ -8,6 +8,7 @@ const ( OutFormatHTML OutFormat = "html" OutFormatTxt OutFormat = "plaintext" OutFormatEnv OutFormat = "dotenv" + OutFormatJSON OutFormat = "json" ) // EnvDocItem is a documentation item for one environment variable.