config

package
v0.0.1-pre-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2020 License: MIT Imports: 14 Imported by: 8

Documentation

Overview

Package config allows Altid services to interact withe the ndb-formatted configuration files used by Altid

go get github.com/altid/libs/config

The ndb format is described in http://man.cat-v.org/plan_9/6/ndb

Usage

This library has some special usage notes. Namely, on error it will return a string indicating the user should run `myservice -conf`. Doing so will call the Configurator which was passed in to config.New() and exit the program.

Repl

The included Repl is meant to make designing Configurators much easier. To use, simply pass a struct to Repl with all the entries you wish described as type string|int|bool The struct tags will be used verbatim as the prompt message to the client

func myConfigurator(rw io.ReadCloser) (*config.Config, error) {
	repl := struct {
		Address string `IP Address of service`
		Port int `Port to use`
		UseTLS bool `Do you wish to use TLS?`
	}{"localhost", 564, "false"}

	return config.Repl(rw, repl, false)
}

This will start a repl with the client over the io.ReadWriter, using the struct tags as the prompt

$ IP Address of service [localhost]: (enter to use default)
$ Port to use [564]: (enter to use default)
$ Do you wish to use TLS? [no]: (enter to use default)

Index

Examples

Constants

View Source
const (
	ErrBadConfigurator = "configurator nil or invalid, cannot continue"
	ErrNoConfigure     = "unable to find or create config for this service. To create one, please run %s -conf"
	ErrNoSuchKey       = "no such key"
	ErrNoEntries       = "unable to find config entry for this service."
	ErrMultiEntries    = "config contains duplicate entries for this service"
)

Errors

Variables

This section is empty.

Functions

func GetLogDir

func GetLogDir(service string) string

GetLogDir returns a canonical directory for a user log, searching first altid/config If no entry is found or the file is missing, it will return "none"

Types

type Config added in v0.1.0

type Config struct {
	Name   string
	Values []*Entry
	// contains filtered or unexported fields
}

Config defines a services' configuration in a given config file

func ListAll

func ListAll() ([]*Config, error)

ListAll returns every config

func Mock

func Mock(c Configurator, service string, debug bool) (*Config, error)

Mock returns a mock config for a given service for testing It always calls Configurator to create a Config, and will not write to file

func New added in v0.1.0

func New(c Configurator, service string, debug bool) (*Config, error)

New returns a valid config for a given service. If one is not found, the Configurator will be called to interactively create one. On success, the program will exit

func Repl

func Repl(rw io.ReadWriter, req interface{}, debug bool) (*Config, error)

Repl accepts a struct with String/Int/Bool types The fields should contain the default values, which will be presented to the client during the repl

Example
package main

import (
	"io"
	"log"

	"github.com/altid/libs/config"
)

func main() {
	buildConfig := func(rw io.ReadWriter) (*config.Config, error) {
		repl := struct {
			Address  string `IP address to dial`
			Password string `password for service`
			UseSSL   bool   `Use SSL?`
			Foo      string // Will use default
		}{"127.0.0.1", "password", false, "bar"}

		return config.Repl(rw, repl, false)
	}

	if _, e := config.Mock(buildConfig, "zzyzx", false); e != nil {
		log.Fatal(e)
	}
}

func (*Config) Log added in v0.1.0

func (c *Config) Log() string

Log returns the configured Log, or "none"

func (*Config) MustSearch added in v0.1.0

func (c *Config) MustSearch(key string) string

MustSearch returns a value or an empty string, if not found

func (*Config) Password added in v0.1.0

func (c *Config) Password() (string, error)

Password queries the database for a password the format in the config is auth=pass=mypassword or auth=factotum when auth=factotum is found, it will attempt to query the factotum for a response

func (*Config) SSLCert added in v0.1.0

func (c *Config) SSLCert() (tls.Certificate, error)

SSLCert returns a tls.Certificate based on successfully finding a cert and key file listen in the configuration

func (*Config) Search added in v0.1.0

func (c *Config) Search(key string) (string, error)

Search queries for an entry in the config matching key Returning the value if it exists, or a "no such key" error

func (*Config) String added in v0.1.0

func (c *Config) String() string

String returns our entry tuples in the form of key=value

type Configurator added in v0.1.0

type Configurator func(io.ReadWriter) (*Config, error)

Configurator is called when no entry is found for a given service It should query the user for each required value, returning a complete and usable Config

type Entry added in v0.1.0

type Entry struct {
	Key   string
	Value string
}

Entry is a single tuple in a services configuration

Jump to

Keyboard shortcuts

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