Documentation
¶
Overview ¶
Package docs implements helper functions for automatically generating documentation from envconfig configuration structs.
Index ¶
- func HTMLTable(w io.Writer, cinfo *envconfig.ConfInfo) error
- func HTMLTableString(cinfo *envconfig.ConfInfo) (string, error)
- func HTMLTableWithTemplate(t *template.Template) (*template.Template, error)
- func TextTable(w io.Writer, cinfo *envconfig.ConfInfo)
- func TextTableString(cinfo *envconfig.ConfInfo) string
- func TextTableWithOptions(w io.Writer, cinfo *envconfig.ConfInfo, table *tablewriter.Table, maxwidth int)
- func TextTableWithWidth(w io.Writer, cinfo *envconfig.ConfInfo, maxwidth int)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTMLTable ¶
HTMLTable writes each field in the configuration struct as a row in an HTML table.
Example ¶
package main
import (
"github.com/JamesStewy/envconfig"
"github.com/JamesStewy/envconfig/docs"
"os"
)
func main() {
var conf struct {
Protocol string `envconfig:"default=https,note=Protocol to be used"`
RemoteHost string `envconfig:"note=Remote hostname"`
Port int `envconfig:"default=443"`
}
os.Setenv("REMOTE_HOST", "localhost")
os.Setenv("PORT", "80")
cinfo, err := envconfig.Parse(&conf)
if err != nil {
panic(err)
}
if err = cinfo.Read(); err != nil {
panic(err)
}
if err = docs.HTMLTable(os.Stdout, cinfo); err != nil {
panic(err)
}
}
Output: <table> <thead> <tr> <th>Keys</th> <th>Value</th> <th>Default</th> <th>Note</th> </tr> </thead> <tbody> <tr> <th>PROTOCOL</th> <th>https</th> <th>https</th> <th>Protocol to be used</th> </tr> <tr> <th>REMOTEHOST<br>REMOTE_HOST</th> <th>localhost</th> <th></th> <th>Remote hostname</th> </tr> <tr> <th>PORT</th> <th>80</th> <th>443</th> <th></th> </tr> </tbody> </table>
func HTMLTableString ¶
HTMLTableString writes each field in the configuration struct as a row in an HTML table. HTMLTableString returns the table in a string.
func HTMLTableWithTemplate ¶
HTMLTableWithTemplate defines a new template named "envconfig". The "envconfig" template must be executed with a ConfInfo object. Use HTMLTableWithTemplate if you want to embed the table within a custom template.
Example ¶
package main
import (
"github.com/JamesStewy/envconfig"
"github.com/JamesStewy/envconfig/docs"
"html/template"
"os"
)
func main() {
var conf struct {
Protocol string `envconfig:"default=https,note=Protocol to be used"`
RemoteHost string `envconfig:"note=Remote hostname"`
Port int `envconfig:"default=443"`
}
os.Setenv("REMOTE_HOST", "localhost")
os.Setenv("PORT", "80")
cinfo, err := envconfig.Parse(&conf)
if err != nil {
panic(err)
}
if err = cinfo.Read(); err != nil {
panic(err)
}
base_tmpl := template.Must(template.New("base").Parse(`<html>
<head>
<title>Test Page</title>
</head>
<body>
{{template "envconfig" .}}
</body>
</html>`))
t, err := docs.HTMLTableWithTemplate(base_tmpl)
if err != nil {
panic(err)
}
err = t.Execute(os.Stdout, cinfo)
if err != nil {
panic(err)
}
}
Output: <html> <head> <title>Test Page</title> </head> <body> <table> <thead> <tr> <th>Keys</th> <th>Value</th> <th>Default</th> <th>Note</th> </tr> </thead> <tbody> <tr> <th>PROTOCOL</th> <th>https</th> <th>https</th> <th>Protocol to be used</th> </tr> <tr> <th>REMOTEHOST<br>REMOTE_HOST</th> <th>localhost</th> <th></th> <th>Remote hostname</th> </tr> <tr> <th>PORT</th> <th>80</th> <th>443</th> <th></th> </tr> </tbody> </table> </body> </html>
func TextTable ¶
TextTable writes each field in the configuration struct as a row in a text table.
Example ¶
package main
import (
"github.com/JamesStewy/envconfig"
"github.com/JamesStewy/envconfig/docs"
"os"
)
func main() {
var conf struct {
Protocol string `envconfig:"default=https,note=Protocol to be used"`
RemoteHost string `envconfig:"note=Remote hostname"`
Port int `envconfig:"default=443"`
}
os.Setenv("REMOTE_HOST", "localhost")
os.Setenv("PORT", "80")
cinfo, err := envconfig.Parse(&conf)
if err != nil {
panic(err)
}
if err = cinfo.Read(); err != nil {
panic(err)
}
docs.TextTable(os.Stdout, cinfo)
}
Output: +------------------------+-----------+---------+---------------------+ | KEYS | VALUE | DEFAULT | NOTE | +------------------------+-----------+---------+---------------------+ | PROTOCOL | https | https | Protocol to be used | +------------------------+-----------+---------+---------------------+ | REMOTEHOST | localhost | | Remote hostname | | REMOTE_HOST | | | | +------------------------+-----------+---------+---------------------+ | PORT | 80 | 443 | | +------------------------+-----------+---------+---------------------+
func TextTableString ¶
TextTableString writes each field in the configuration struct as a row in a text table. TextTableString returns the table in a string.
func TextTableWithOptions ¶
func TextTableWithOptions(w io.Writer, cinfo *envconfig.ConfInfo, table *tablewriter.Table, maxwidth int)
TextTableWithOptions writes each field in the configuration struct as a row in a text table. maxwidth sets the maximum number of charaters wide each column in the table can be.
Types ¶
This section is empty.