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 ¶
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 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 ( "log" "github.com/altid/libs/config" "github.com/altid/libs/config/types" ) func main() { conf := struct { Address string `Enter the address you wish to connect on` Port int Auth types.Auth `Enter your authentication method: password|factotum|none` Logdir types.Logdir Listen types.ListenAddress }{"irc.freenode.net", 1234, "none", "", ""} if e := config.Create(&conf, "zzyzx", "resources/create_config", true); e != nil { log.Fatal(e) } }
Output:
func GetListenAddress ¶
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 ¶
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 Marshal ¶
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, - 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 types.Auth `Auth mechanism to use: password|factotum|none` Logdir types.Logdir Address types.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 ¶
This section is empty.