config

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2022 License: MIT Imports: 11 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 Auth, Log, or ListenAddress - structs must have default values set

Example:

// package mypackage

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

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

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)`
		Port int
		Auth types.Auth `Enter the authentication method you would like to use: password|factotum|none`
		Logdir types.Logdir
		ListenAddress config.ListenAddress
	}{u.Name, false, 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

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(requests interface{}, svc, configFile string, debug bool) error

Create takes a pointer to a struct, and will walk a user through creation of a config entry for the service Upon success, it will print the config and instructions to stdout It is meant to be used with the -conf flag

	conf := struct {
		Address string     `altid:"address,prompt:IP address to dial"`
		Auth    types.Auth `altid:"auth,prompt:Auth mechanism to use"`
		UseSSL  bool       `altid:"usessl,prompt:Use SSL?,pick:true|false"`
		Foo     string     // Will use default
	}{"127.0.0.1", "password", false, "bar"}

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

 os.Exit()

Notably, Create will parse entries for altid struct tags with the field "prompt". These will prompt a user for the value on the command line, optionally with a whitelisted array of selections to pick from Selection of an item not on a whitelist will return an error after 3 attempts The `pick` option to a types.Auth will be ignored, and will always be one of `password|factotum|none`

Example
package main

import (
	"log"

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

func main() {
	conf := struct {
		Address string              `altid:"address,prompt:IP to dial"`
		Port    int                 `altid:"port,no_prompt"`
		Auth    types.Auth          `altid:"auth,prompt:Auth mechanism to use,pick:password|factotum|none"`
		Logdir  types.Logdir        `altid:"logdir,no_prompt"`
		Listen  types.ListenAddress `altid:"listen_address,no_prompt"`
	}{"irc.freenode.net", 1234, "none", "", ""}

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

func GetListenAddress

func GetListenAddress(service string) (string, string)

GetListenAddress returns the listen_address of a server, or "" if none is found If a port is set, e.g. listen_address = 192.168.0.4:8080 it will return 8080

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, configFile 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 tags with a matching altid prefix will be marshalled, according to the defaults set The first value of the tag must be the Altid config name you wish to retrieve, such as listen_address Other fields include prompt/no_prompt:, and pick: Pick will return an error if a value other than one listed exists in the config/defaults

	conf := struct {
		Address string     `altid:"address,prompt:IP address to dial"`
		Auth    types.Auth `altid:"auth,prompt:Auth mechanism to use,pick:password|factotum|none"`
		UseSSL  bool       `altid:"usessl,prompt:Use SSL?,pick:true|false"`
		Foo     string     // Will use default
	}{"127.0.0.1", "password", false, "bar"}

	if e := config.Marshal(&conf, "zzyzx", "", false); e != nil {
		log.Fatal(e)
	}
 [...]
Example
package main

import (
	"log"

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

func main() {
	conf := struct {
		Address string     `altid:"address,prompt:IP address to dial"`
		Auth    types.Auth `altid:"auth,prompt:Auth mechanism to use,pick:password|factotum|none"`
		UseSSL  bool       `altid:"usessl,prompt:Use SSL?,pick:true|false"`
		Foo     string     // Will use default
	}{"127.0.0.1", "password", false, "bar"}

	if e := config.Marshal(&conf, "zzyzx", "resources/marshal_config", false); e != nil {
		log.Fatal(e)
	}
}
Output:

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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