docs

package
v0.0.0-...-ab9bc20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 5, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package docs implements helper functions for automatically generating documentation from envconfig configuration structs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTMLTable

func HTMLTable(w io.Writer, cinfo *envconfig.ConfInfo) error

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

func HTMLTableString(cinfo *envconfig.ConfInfo) (string, error)

HTMLTableString writes each field in the configuration struct as a row in an HTML table. HTMLTableString returns the table in a string.

func HTMLTableWithTemplate

func HTMLTableWithTemplate(t *template.Template) (*template.Template, error)

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

func TextTable(w io.Writer, cinfo *envconfig.ConfInfo)

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

func TextTableString(cinfo *envconfig.ConfInfo) string

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.

func TextTableWithWidth

func TextTableWithWidth(w io.Writer, cinfo *envconfig.ConfInfo, maxwidth int)

TextTableWithWidth 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL