go-paste/config.go
2017-04-14 16:15:07 -06:00

77 lines
1.5 KiB
Go

package main
import (
"github.com/spf13/viper"
"github.com/docopt/docopt.go"
"log"
"io/ioutil"
"sour.is/x/httpsrv"
"bytes"
"fmt"
)
var APP_NAME string = "Paste API"
var APP_USAGE string = `Paste API
Usage:
mercury [-v] serve [--listen=<ListenAddress>]
Options:
-v Log to console.
-l <ListenAddress>, --listen=<ListenAddress> Address to listen on.
-c <ConfigFile>, --conf=<ConfigFile> Config file name. [Default: config.toml]
Config:
The config file is read from the following locations:
- /etc/paste/
- ~/.local/paste/
- Working Directory
`
var args map[string]interface{}
func init() {
var err error
if args, err = docopt.Parse(APP_USAGE, nil, true, APP_NAME, false); err != nil {
log.Fatal(err)
}
if args["-v"] == false {
log.SetOutput(ioutil.Discard)
}
viper.SetConfigName("config")
viper.AddConfigPath("/etc/reg42/")
viper.AddConfigPath("$HOME/.local/reg42/")
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.ReadConfig(bytes.NewBuffer(defaultConfig))
err = viper.MergeInConfig()
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
log.Print("Read config from: ", viper.ConfigFileUsed())
if args["serve"] == true {
if args["--listen"] != nil {
viper.Set("listen", args["--listen"].(string))
}
httpsrv.Config()
}
}
var defaultConfig []byte = []byte(`
listen = ":9010"
fileserver = "/public/:/"
[module.paste]
random = "4096"
store = "data/"
`)