konghcl

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 10 Imported by: 1

README

A Kong configuration loader for HCL CircleCI

This is version 1.x of kong-hcl. Version 2 of this package uses the HCL2 library but is otherwise largely a drop-in replacement (see the README for details).

Use it like so:

var cli struct {
    Config kong.ConfigFlag `help:"Load configuration."`
}
parser, err := kong.New(&cli, kong.Configuration(konghcl.Loader, "/etc/myapp/config.hcl", "~/.myapp.hcl))

Mapping HCL fragments to a struct

More complex structures can be loaded directly into flag values by implementing the kong.MapperValue interface, and calling konghcl.DecodeValue.

The value can either be a HCL(/JSON) fragment, or a path to a HCL file that will be loaded. Both can be specified on the command-line or config file.

eg.

type NestedConfig struct {
	Size int
	Name string
}

type ComplexConfig struct {
	Key bool
	Nested map[string]NestedConfig
}

func (c *ComplexConfig) Decode(ctx *kong.DecodeContext) error {
	return konghcl.DecodeValue(ctx, c)
}

// ...

type Config struct {
	Complex ComplexConfig
}

Then the following .hcl config fragment will be decoded into Complex:

complex {
  key = true
  nested first {
    size = 10
    name = "first name"
  }
  nested second {
    size = 12
    name = "second name"
  }
}

Configuration layout

Configuration keys are mapped directly to flags.

Additionally, HCL block keys will be used as a hyphen-separated prefix when looking up flags.

Example

The following Kong CLI:

type CLI struct {
  Debug bool
  DB struct {
    DSN string
    Trace bool
  } `embed:"" prefix:"db-"`
}

Maps to the following flags:

--debug
--db-dsn=<string>
--db-trace

And can be configured via the following HCL configuration file...

debug = true

db {
    dsn = "root@/database"
    trace = true
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DumpIgnoreFlags specifies a set of flags that should not be dumped.
	DumpIgnoreFlags = map[string]bool{
		"help": true, "version": true, "dump-config": true, "env": true, "validate-config": true,
	}
)

Functions

func DecodeValue

func DecodeValue(ctx *kong.DecodeContext, dest interface{}) error

DecodeValue decodes Kong values into a Go structure.

func Loader

func Loader(r io.Reader) (kong.Resolver, error)

Loader is a Kong configuration loader for HCL.

Types

type DumpConfig

type DumpConfig bool

DumpConfig can be added as a flag to dump HCL configuration.

func (DumpConfig) BeforeApply

func (f DumpConfig) BeforeApply(app *kong.Kong) error

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver resolves kong Flags from configuration in HCL.

func (*Resolver) Resolve

func (r *Resolver) Resolve(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error)

func (*Resolver) Validate

func (r *Resolver) Validate(app *kong.Application) error

Jump to

Keyboard shortcuts

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