config

package
v0.0.3-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 23, 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

A service can marshall values to a struct through Marshall, as long as the entries follow these rules: - Nested structs will be ignored - Type must be bool, int, string, or one of tls.Certificate, Auth, Log, or ListenAddress - Type tls.Certificate must not have struct tags - structs must have default values set

Example:

// package mypackage

import (
	"flag"
	"log"
	"os"
	"os/user"

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

var conf = flag.Bool("conf", false, "Create configuration file")

func main() {
	flag.Parse()

	u, _ := user.Current

	mytype := struct {
		// Struct tags are used by Create to interactively fill in any missing data
		Name string `Enter a name to use on the service`
		UseTLS bool `Use TLS? (true|false)`
		TLSCert tls.Certificate // Do not use struct tags, this will be filled out using key= and cert= tuples
		Port int
		Auth config.Auth `Enter the authentication method you would like to use: password|factotum|none`
		Logdir config.Logdir
		ListenAddress config.ListenAddress
	}{u.Name, false, tls.Certificate{}, 564, "none", "", ""}

	if flag.Lookup("conf") != nil {
		if e := config.Marshall(&mytype, "myservice", false); e != nil {
			log.Fatal(e)
		}

		os.Exit(0)
	}

	// Your error message should indicate that the user re-runs with -conf to create any missing entries
	if e := config.Marshall(&mytype, "myservice", false); e != nil {
		log.Fatal("unable to create config: %v\nRun program with -conf to create missing entries")
	}

	// [...]
}

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. Please edit your altid/config file"
)

Errors

Variables

This section is empty.

Functions

func Create

func Create(requests interface{}, service, confdir string, debug bool) error

Create takes a pointer to a struct, and attempts to create a config file entry on disk based on the struct It is meant to be used with the -conf flag The semantics are the same as Marshall, but it uses the struct tags to prompt the user to fill in the data for any missing entries For example:

Name string `Username to connect with`

would prompt the user for a username, optionally offering the default value passed in On success, the user should cleanly exit the program, as requests is not filled as it is in Marshall

Example
package main

import (
	"crypto/tls"
	"log"

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

func main() {
	conf := struct {
		Address string `Enter the address you wish to connect on`
		Port    int
		Auth    config.Auth `Enter your authentication method: password|factotum|none`
		Logdir  config.Logdir
		Tls     tls.Certificate
		Listen  config.ListenAddress
	}{"irc.freenode.net", 1234, "none", "", tls.Certificate{}, ""}

	if e := config.Create(&conf, "zzyzx", "resources/create_config", true); e != nil {
		log.Fatal(e)
	}
}

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"

func ListAll

func ListAll() ([]string, error)

ListAll returns a list of available services

func Marshal

func Marshal(requests interface{}, service string, confdir string, debug bool) error

Marshal will take a pointer to a struct as input, as well as the name of the service and attempt to fill the struct. - The struct entries must be of the type string, int, bool, or tls.Certificate - The tags of the struct will be used to indicate a query sent to the user - Default entries to the struct will be used as defaults

	type myconf struct {
		Name string `Username to use for the service`
		Port int `Port to connect with`
		UseSSL bool `Do you want to connect with SSL?`
		Auth config.Auth `Auth mechanism to use: password|factotum|none`
		Logdir config.Logdir
     Address config.ListenAddress
	}{myusername, 1234, true, "none", "none"}

 err := config.Marshal(myconf, "myservice", false)
 [...]

The preceding example would search the config file for each lower case entry If it cannot fill an entry, it returns an error Idiomatically, the user should be prompted to rerun with the -conf flag and the function Create should be called, and on success, exit the program

Types

type Auth

type Auth string

Auth matches an auth= tuple in a config If the value matches factotum, it will use the factotum to return a password If the value matches password, it will return the value of a password= tuple If the value matches none, it will return an empty string

type ListenAddress

type ListenAddress string

ListenAddress is the listen_address tuple in a config If this is unset in the config, it will be filled with "localhost"

type Logdir

type Logdir string

Logdir is the directory that an Altid service can optionally store logs to If this is unset in the config, it will be filled with "none"

Jump to

Keyboard shortcuts

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