ocid: add TOML configuration file

The --config is merged with the default, and then is overridden by any
command-line options. Everything is organised to be in sub-tables so
that the sections are more clear.

Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
Aleksa Sarai 2016-10-10 19:20:35 +11:00
parent 7bf5110b76
commit cd9e7de108
No known key found for this signature in database
GPG key ID: 9E18AA267DDB8DB4

View file

@ -15,9 +15,10 @@ import (
)
const (
ocidRoot = "/var/lib/ocid"
conmonPath = "/usr/libexec/ocid/conmon"
pausePath = "/usr/libexec/ocid/pause"
ocidRoot = "/var/lib/ocid"
conmonPath = "/usr/libexec/ocid/conmon"
pausePath = "/usr/libexec/ocid/pause"
ociConfigPath = "/etc/ocid.conf"
)
// DefaultConfig returns the default configuration for ocid.
@ -44,6 +45,20 @@ func DefaultConfig() *server.Config {
}
func mergeConfig(config *server.Config, ctx *cli.Context) error {
// Don't parse the config if the user explicitly set it to "".
if path := ctx.GlobalString("config"); path != "" {
if err := config.FromFile(path); err != nil {
if ctx.GlobalIsSet("config") || !os.IsNotExist(err) {
return err
}
// We don't error out if --config wasn't explicitly set and the
// default doesn't exist. But we will log a warning about it, so
// the user doesn't miss it.
logrus.Warnf("default configuration file does not exist: %s", ociConfigPath)
}
}
// Override options set with the CLI.
if ctx.GlobalIsSet("conmon") {
config.Conmon = ctx.GlobalString("conmon")
@ -82,6 +97,11 @@ func main() {
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Value: ociConfigPath,
Usage: "path to configuration file",
},
cli.StringFlag{
Name: "conmon",
Usage: "path to the conmon executable",